clang-tidy: range loop conversions

Found with: modernize-loop-convert

Signed-off-by: Rosen Penev <rosenp@gmail.com>
main
Rosen Penev 4 years ago committed by Luis Díaz Más
parent 44256e8c5d
commit 8e52032ee0

@ -56,14 +56,9 @@ namespace Jzon
std::string GetIndentation(unsigned int level) const std::string GetIndentation(unsigned int level) const
{ {
if (!format.newline) if (format.newline)
{
return "";
}
else
{
return std::string(format.indentSize * level, indentationChar); return std::string(format.indentSize * level, indentationChar);
} return "";
} }
inline const std::string &GetNewline() const inline const std::string &GetNewline() const
@ -104,57 +99,49 @@ namespace Jzon
{ {
if (IsObject()) if (IsObject())
return static_cast<const Object&>(*this); return static_cast<const Object&>(*this);
else
throw TypeException(); throw TypeException();
} }
Array &Node::AsArray() Array &Node::AsArray()
{ {
if (IsArray()) if (IsArray())
return static_cast<Array&>(*this); return static_cast<Array &>(*this);
else
throw TypeException(); throw TypeException();
} }
const Array &Node::AsArray() const const Array &Node::AsArray() const
{ {
if (IsArray()) if (IsArray())
return static_cast<const Array&>(*this); return static_cast<const Array &>(*this);
else
throw TypeException(); throw TypeException();
} }
Value &Node::AsValue() Value &Node::AsValue()
{ {
if (IsValue()) if (IsValue())
return static_cast<Value&>(*this); return static_cast<Value &>(*this);
else
throw TypeException(); throw TypeException();
} }
const Value &Node::AsValue() const const Value &Node::AsValue() const
{ {
if (IsValue()) if (IsValue())
return static_cast<const Value&>(*this); return static_cast<const Value &>(*this);
else
throw TypeException(); throw TypeException();
} }
Node::Type Node::DetermineType(const std::string &json) Node::Type Node::DetermineType(const std::string &json)
{ {
auto jsonIt = json.begin(); auto jsonIt = std::find_if(json.begin(), json.end(), IsWhitespace);
while (jsonIt != json.end() && IsWhitespace(*jsonIt))
++jsonIt;
if (jsonIt == json.end()) if (jsonIt == json.end())
return T_VALUE; return T_VALUE;
switch (*jsonIt) switch (*jsonIt) {
{ case '{':
case '{' : return T_OBJECT; return T_OBJECT;
case '[' : return T_ARRAY; case '[':
default : return T_VALUE; return T_ARRAY;
default:
return T_VALUE;
} }
} }
Value::Value() : Node() Value::Value() : Node()
{ {
SetNull(); SetNull();
@ -213,11 +200,8 @@ namespace Jzon
{ {
return "null"; return "null";
} }
else
{
return valueStr; return valueStr;
} }
}
int Value::ToInt() const int Value::ToInt() const
{ {
if (IsNumber()) if (IsNumber())
@ -227,11 +211,8 @@ namespace Jzon
sstr >> val; sstr >> val;
return val; return val;
} }
else
{
return 0; return 0;
} }
}
float Value::ToFloat() const float Value::ToFloat() const
{ {
if (IsNumber()) if (IsNumber())
@ -241,11 +222,8 @@ namespace Jzon
sstr >> val; sstr >> val;
return val; return val;
} }
else
{
return 0.F; return 0.F;
} }
}
double Value::ToDouble() const double Value::ToDouble() const
{ {
if (IsNumber()) if (IsNumber())
@ -255,22 +233,16 @@ namespace Jzon
sstr >> val; sstr >> val;
return val; return val;
} }
else
{
return 0.0; return 0.0;
} }
}
bool Value::ToBool() const bool Value::ToBool() const
{ {
if (IsBool()) if (IsBool())
{ {
return (valueStr == "true"); return (valueStr == "true");
} }
else
{
return false; return false;
} }
}
void Value::SetNull() void Value::SetNull()
{ {
@ -323,10 +295,7 @@ namespace Jzon
} }
void Value::Set(const bool value) void Value::Set(const bool value)
{ {
if (value) valueStr = value ? "true" : "false";
valueStr = "true";
else
valueStr = "false";
type = VT_BOOL; type = VT_BOOL;
} }
@ -375,7 +344,7 @@ namespace Jzon
bool Value::operator==(const Value &other) const bool Value::operator==(const Value &other) const
{ {
return ((type == other.type)&&(valueStr == other.valueStr)); return ((type == other.type) && (valueStr == other.valueStr));
} }
bool Value::operator!=(const Value &other) const bool Value::operator!=(const Value &other) const
{ {
@ -388,35 +357,27 @@ namespace Jzon
} }
// This is not the most beautiful place for these, but it'll do // This is not the most beautiful place for these, but it'll do
static const char charsUnescaped[] = { '\\' , '/' , '\"' , '\n' , '\t' , '\b' , '\f' , '\r' }; static const char charsUnescaped[] = {'\\', '/', '\"', '\n', '\t', '\b', '\f', '\r'};
static const char *charsEscaped[] = { "\\\\", "\\/", "\\\"", "\\n", "\\t", "\\b", "\\f", "\\r" }; static const char *charsEscaped[] = {"\\\\", "\\/", "\\\"", "\\n", "\\t", "\\b", "\\f", "\\r"};
static const unsigned int numEscapeChars = 8; static const unsigned int numEscapeChars = 8;
static const char nullUnescaped = '\0'; static const char nullUnescaped = '\0';
static const char *nullEscaped = "\0\0"; static const char *nullEscaped = "\0\0";
const char *&getEscaped(const char &c) const char *&getEscaped(const char &c)
{ {
for (unsigned int i = 0; i < numEscapeChars; ++i) for (unsigned int i = 0; i < numEscapeChars; ++i) {
{ if (c == charsUnescaped[i]) {
const char &ue = charsUnescaped[i]; return charsEscaped[i];
if (c == ue)
{
const char *&e = charsEscaped[i];
return e;
} }
} }
return nullEscaped; return nullEscaped;
} }
const char &getUnescaped(const char &c1, const char &c2) const char &getUnescaped(const char &c1, const char &c2)
{ {
for (unsigned int i = 0; i < numEscapeChars; ++i) for (unsigned int i = 0; i < numEscapeChars; ++i) {
{
const char *&e = charsEscaped[i]; const char *&e = charsEscaped[i];
if (c1 == e[0] && c2 == e[1]) if (c1 == e[0] && c2 == e[1]) {
{ return charsUnescaped[i];
const char &ue = charsUnescaped[i];
return ue;
} }
} }
return nullUnescaped; return nullUnescaped;
@ -426,16 +387,12 @@ namespace Jzon
{ {
std::string escaped; std::string escaped;
for (const auto& c: value) for (auto &&c : value) {
{
const char *&a = getEscaped(c); const char *&a = getEscaped(c);
if (a[0] != '\0') if (a[0] != '\0') {
{
escaped += a[0]; escaped += a[0];
escaped += a[1]; escaped += a[1];
} } else {
else
{
escaped += c; escaped += c;
} }
} }
@ -469,30 +426,22 @@ namespace Jzon
return unescaped; return unescaped;
} }
Object::Object() : Node() Object::Object() : Node()
{ {
} }
Object::Object(const Object &other) : Node() Object::Object(const Object &other) : Node()
{ {
for (auto it = other.children.cbegin(); it != other.children.cend(); ++it) for (auto &&it : other.children) {
{ const std::string &name = it.first;
const std::string &name = (*it).first; Node &value = *it.second;
Node &value = *(*it).second;
children.push_back(NamedNodePtr(name, value.GetCopy())); children.push_back(NamedNodePtr(name, value.GetCopy()));
} }
} }
Object::Object(const Node &other) : Node() Object::Object(const Node &other) : Node()
{ {
const Object &object = other.AsObject(); for (auto &&child : other.AsObject().children) {
children.push_back(NamedNodePtr(child.first, child.second->GetCopy()));
for (auto it = object.children.cbegin(); it != object.children.cend(); ++it)
{
const std::string &name = (*it).first;
Node &value = *(*it).second;
children.push_back(NamedNodePtr(name, value.GetCopy()));
} }
} }
Object::~Object() Object::~Object()
@ -528,10 +477,9 @@ namespace Jzon
void Object::Clear() void Object::Clear()
{ {
for (auto it = children.begin(); it != children.end(); ++it) for (auto &&child : children) {
{ delete child.second;
delete (*it).second; child.second = nullptr;
(*it).second = NULL;
} }
children.clear(); children.clear();
} }
@ -540,7 +488,6 @@ namespace Jzon
{ {
if (!children.empty()) if (!children.empty())
return Object::iterator(&children.front()); return Object::iterator(&children.front());
else
return Object::iterator(NULL); return Object::iterator(NULL);
} }
@ -548,35 +495,26 @@ namespace Jzon
{ {
if (!children.empty()) if (!children.empty())
return Object::const_iterator(&children.front()); return Object::const_iterator(&children.front());
else
return Object::const_iterator(NULL); return Object::const_iterator(NULL);
} }
Object::iterator Object::end() Object::iterator Object::end()
{ {
if (!children.empty()) if (!children.empty())
return Object::iterator(&children.back()+1); return Object::iterator(&children.back() + 1);
else
return Object::iterator(NULL); return Object::iterator(NULL);
} }
Object::const_iterator Object::end() const Object::const_iterator Object::end() const
{ {
if (!children.empty()) if (!children.empty())
return Object::const_iterator(&children.back()+1); return Object::const_iterator(&children.back() + 1);
else
return Object::const_iterator(NULL); return Object::const_iterator(NULL);
} }
bool Object::Has(const std::string &name) const bool Object::Has(const std::string &name) const
{ {
for (ChildList::const_iterator it = children.begin(); it != children.end(); ++it) return std::any_of(children.begin(), children.end(),
{ [&](const NamedNodePtr &child) { return child.first == name; });
if ((*it).first == name)
{
return true;
}
}
return false;
} }
size_t Object::GetCount() const size_t Object::GetCount() const
{ {
@ -584,11 +522,9 @@ namespace Jzon
} }
Node &Object::Get(const std::string &name) const Node &Object::Get(const std::string &name) const
{ {
for (ChildList::const_iterator it = children.begin(); it != children.end(); ++it) for (auto &&child : children) {
{ if (child.first == name) {
if ((*it).first == name) return *child.second;
{
return *(*it).second;
} }
} }
@ -600,17 +536,14 @@ namespace Jzon
return new Object(*this); return new Object(*this);
} }
Array::Array() : Node() Array::Array() : Node()
{ {
} }
Array::Array(const Array &other) : Node() Array::Array(const Array &other) : Node()
{ {
for (auto it = other.children.cbegin(); it != other.children.cend(); ++it) for (auto &&value : other.children) {
{ children.push_back(value->GetCopy());
const Node &value = *(*it);
children.push_back(value.GetCopy());
} }
} }
@ -618,10 +551,8 @@ namespace Jzon
{ {
const Array &array = other.AsArray(); const Array &array = other.AsArray();
for (auto it = array.children.cbegin(); it != array.children.cend(); ++it) for (auto &&value : array.children) {
{ children.push_back(value->GetCopy());
const Node &value = *(*it);
children.push_back(value.GetCopy());
} }
} }
@ -645,19 +576,17 @@ namespace Jzon
} }
void Array::Remove(size_t index) void Array::Remove(size_t index)
{ {
if (index < children.size()) if (index < children.size()) {
{ ChildList::iterator it = children.begin() + index;
ChildList::iterator it = children.begin()+index;
delete (*it); delete (*it);
children.erase(it); children.erase(it);
} }
} }
void Array::Clear() void Array::Clear()
{ {
for (ChildList::iterator it = children.begin(); it != children.end(); ++it) for (auto &&child : children) {
{ delete child;
delete (*it); child = nullptr;
(*it) = NULL;
} }
children.clear(); children.clear();
} }
@ -666,28 +595,24 @@ namespace Jzon
{ {
if (!children.empty()) if (!children.empty())
return Array::iterator(&children.front()); return Array::iterator(&children.front());
else
return Array::iterator(NULL); return Array::iterator(NULL);
} }
Array::const_iterator Array::begin() const Array::const_iterator Array::begin() const
{ {
if (!children.empty()) if (!children.empty())
return Array::const_iterator(&children.front()); return Array::const_iterator(&children.front());
else
return Array::const_iterator(NULL); return Array::const_iterator(NULL);
} }
Array::iterator Array::end() Array::iterator Array::end()
{ {
if (!children.empty()) if (!children.empty())
return Array::iterator(&children.back()+1); return Array::iterator(&children.back() + 1);
else
return Array::iterator(NULL); return Array::iterator(NULL);
} }
Array::const_iterator Array::end() const Array::const_iterator Array::end() const
{ {
if (!children.empty()) if (!children.empty())
return Array::const_iterator(&children.back()+1); return Array::const_iterator(&children.back() + 1);
else
return Array::const_iterator(NULL); return Array::const_iterator(NULL);
} }
@ -697,8 +622,7 @@ namespace Jzon
} }
Node &Array::Get(size_t index) const Node &Array::Get(size_t index) const
{ {
if (index < children.size()) if (index < children.size()) {
{
return *children.at(index); return *children.at(index);
} }
@ -710,7 +634,6 @@ namespace Jzon
return new Array(*this); return new Array(*this);
} }
FileWriter::FileWriter(const std::string &filename) : filename(filename) FileWriter::FileWriter(const std::string &filename) : filename(filename)
{ {
} }
@ -759,11 +682,8 @@ namespace Jzon
error = parser.GetError(); error = parser.GetError();
return false; return false;
} }
else
{
return true; return true;
} }
}
Node::Type FileReader::DetermineType() Node::Type FileReader::DetermineType()
{ {
@ -1193,11 +1113,8 @@ namespace Jzon
{ {
return json.at(cursor+1); return json.at(cursor+1);
} }
else
{
return '\0'; return '\0';
} }
}
void Parser::jumpToNext(char c) void Parser::jumpToNext(char c)
{ {
++cursor; ++cursor;
@ -1265,26 +1182,12 @@ namespace Jzon
} }
else else
{ {
bool number = true; bool number = std::all_of(value.begin(), value.end(), IsNumber);
for (std::string::const_iterator it = value.begin(); it != value.end(); ++it) if (!number) {
{
if (!IsNumber(*it))
{
number = false;
break;
}
}
if (number)
{
data.push(std::make_pair(Value::VT_NUMBER, value));
}
else
{
return false; return false;
} }
data.push(std::make_pair(Value::VT_NUMBER, value));
} }
return true; return true;
} }
} // namespace Jzon } // namespace Jzon

@ -211,11 +211,8 @@ void push(Jzon::Node& node,const std::string& key,T i)
ABORT_IF_I_EMTPY ABORT_IF_I_EMTPY
Jzon::Object l ; Jzon::Object l ;
const Exiv2::LangAltValue& langs = dynamic_cast<const Exiv2::LangAltValue&>(i->value()); const Exiv2::LangAltValue& langs = dynamic_cast<const Exiv2::LangAltValue&>(i->value());
for ( Exiv2::LangAltValue::ValueType::const_iterator lang = langs.value_.begin() for (auto&& lang : langs.value_) {
; lang != langs.value_.end() l.Add(lang.first, lang.second);
; lang++
) {
l.Add(lang->first,lang->second);
} }
Jzon::Object o ; Jzon::Object o ;
o.Add("lang",l); o.Add("lang",l);
@ -350,10 +347,8 @@ int main(int argc, char* const argv[])
// create and populate a Jzon::Object for the namespaces // create and populate a Jzon::Object for the namespaces
Jzon::Object xmlns; Jzon::Object xmlns;
for ( auto it = namespaces.begin() ; it != namespaces.end() ; it++ ) { for (auto&& ns : namespaces) {
std::string ns = *it ; xmlns.Add(ns, nsDict[ns]);
std::string uri = nsDict[ns];
xmlns.Add(ns,uri);
} }
// add xmlns as Xmp.xmlns // add xmlns as Xmp.xmlns

@ -727,9 +727,8 @@ namespace Exiv2 {
assert(object != 0); assert(object != 0);
byte* p = object->start() + 2; byte* p = object->start() + 2;
for (TiffDirectory::Components::iterator i = object->components_.begin(); for (auto&& component : object->components_) {
i != object->components_.end(); ++i) { p += updateDirEntry(p, byteOrder(), component);
p += updateDirEntry(p, byteOrder(), *i);
} }
} }
@ -794,8 +793,8 @@ namespace Exiv2 {
static const char* synthesizedTags[] = { static const char* synthesizedTags[] = {
"Exif.MakerNote.Offset", "Exif.MakerNote.Offset",
}; };
for (unsigned int i = 0; i < EXV_COUNTOF(synthesizedTags); ++i) { for (auto&& synthesizedTag : synthesizedTags) {
pos = exifData_.findKey(ExifKey(synthesizedTags[i])); pos = exifData_.findKey(ExifKey(synthesizedTag));
if (pos != exifData_.end()) exifData_.erase(pos); if (pos != exifData_.end()) exifData_.erase(pos);
} }
} }
@ -1293,8 +1292,8 @@ namespace Exiv2 {
{ {
setMnState(); // All components to be post-processed must be from the Makernote setMnState(); // All components to be post-processed must be from the Makernote
postProc_ = true; postProc_ = true;
for (PostList::const_iterator pos = postList_.begin(); pos != postList_.end(); ++pos) { for (auto&& pos : postList_) {
(*pos)->accept(*this); pos->accept(*this);
} }
postProc_ = false; postProc_ = false;
setOrigState(); setOrigState();

Loading…
Cancel
Save