Modernize: Use range-based for loops for readability (#762)

Also run clang-format on these files as requested
This commit is contained in:
Andy Maloney
2019-10-05 15:20:17 -04:00
committed by Jesse Beder
parent 21d75fa4cd
commit b650bc8287
7 changed files with 37 additions and 37 deletions

View File

@@ -16,8 +16,8 @@ std::string tolower(const std::string& str) {
template <typename T>
bool IsEntirely(const std::string& str, T func) {
for (std::size_t i = 0; i < str.size(); i++)
if (!func(str[i]))
for (char ch : str)
if (!func(ch))
return false;
return true;
@@ -39,7 +39,7 @@ bool IsFlexibleCase(const std::string& str) {
std::string rest = str.substr(1);
return firstcaps && (IsEntirely(rest, IsLower) || IsEntirely(rest, IsUpper));
}
}
} // namespace
namespace YAML {
bool convert<bool>::decode(const Node& node, bool& rhs) {
@@ -52,19 +52,22 @@ bool convert<bool>::decode(const Node& node, bool& rhs) {
static const struct {
std::string truename, falsename;
} names[] = {
{"y", "n"}, {"yes", "no"}, {"true", "false"}, {"on", "off"},
{"y", "n"},
{"yes", "no"},
{"true", "false"},
{"on", "off"},
};
if (!IsFlexibleCase(node.Scalar()))
return false;
for (unsigned i = 0; i < sizeof(names) / sizeof(names[0]); i++) {
if (names[i].truename == tolower(node.Scalar())) {
for (const auto& name : names) {
if (name.truename == tolower(node.Scalar())) {
rhs = true;
return true;
}
if (names[i].falsename == tolower(node.Scalar())) {
if (name.falsename == tolower(node.Scalar())) {
rhs = false;
return true;
}
@@ -72,4 +75,4 @@ bool convert<bool>::decode(const Node& node, bool& rhs) {
return false;
}
}
} // namespace YAML

View File

@@ -199,11 +199,11 @@ bool IsValidPlainScalar(const std::string& str, FlowType::value flowType,
bool IsValidSingleQuotedScalar(const std::string& str, bool escapeNonAscii) {
// TODO: check for non-printable characters?
for (std::size_t i = 0; i < str.size(); i++) {
if (escapeNonAscii && (0x80 <= static_cast<unsigned char>(str[i]))) {
for (char ch : str) {
if (escapeNonAscii && (0x80 <= static_cast<unsigned char>(ch))) {
return false;
}
if (str[i] == '\n') {
if (ch == '\n') {
return false;
}
}
@@ -217,8 +217,8 @@ bool IsValidLiteralScalar(const std::string& str, FlowType::value flowType,
}
// TODO: check for non-printable characters?
for (std::size_t i = 0; i < str.size(); i++) {
if (escapeNonAscii && (0x80 <= static_cast<unsigned char>(str[i]))) {
for (char ch : str) {
if (escapeNonAscii && (0x80 <= static_cast<unsigned char>(ch))) {
return false;
}
}

View File

@@ -12,8 +12,7 @@ namespace YAML {
namespace Exp {
unsigned ParseHex(const std::string& str, const Mark& mark) {
unsigned value = 0;
for (std::size_t i = 0; i < str.size(); i++) {
char ch = str[i];
for (char ch : str) {
int digit = 0;
if ('a' <= ch && ch <= 'f')
digit = ch - 'a' + 10;
@@ -132,5 +131,5 @@ std::string Escape(Stream& in) {
std::stringstream msg;
throw ParserException(in.mark(), std::string(ErrorMsg::INVALID_ESCAPE) + ch);
}
}
}
} // namespace Exp
} // namespace YAML

View File

@@ -31,8 +31,8 @@ void ostream_wrapper::write(const std::string& str) {
std::copy(str.begin(), str.end(), m_buffer.begin() + m_pos);
}
for (std::size_t i = 0; i < str.size(); i++) {
update_pos(str[i]);
for (char ch : str) {
update_pos(ch);
}
}

View File

@@ -8,8 +8,8 @@
#endif
#include "stream.h"
#include "stringsource.h"
#include "streamcharsource.h"
#include "stringsource.h"
namespace YAML {
// query matches
@@ -106,9 +106,8 @@ inline int RegEx::MatchOpEmpty(const Source& source) const {
template <>
inline int RegEx::MatchOpEmpty<StringCharSource>(
const StringCharSource& source) const {
return !source
? 0
: -1; // the empty regex only is successful on the empty string
return !source ? 0 : -1; // the empty regex only is successful on the empty
// string
}
// MatchOperator
@@ -130,8 +129,8 @@ inline int RegEx::MatchOpRange(const Source& source) const {
// OrOperator
template <typename Source>
inline int RegEx::MatchOpOr(const Source& source) const {
for (std::size_t i = 0; i < m_params.size(); i++) {
int n = m_params[i].MatchUnchecked(source);
for (const RegEx& param : m_params) {
int n = param.MatchUnchecked(source);
if (n >= 0)
return n;
}
@@ -169,11 +168,11 @@ inline int RegEx::MatchOpNot(const Source& source) const {
template <typename Source>
inline int RegEx::MatchOpSeq(const Source& source) const {
int offset = 0;
for (std::size_t i = 0; i < m_params.size(); i++) {
int n = m_params[i].Match(source + offset); // note Match, not
// MatchUnchecked because we
// need to check validity after
// the offset
for (const RegEx& param : m_params) {
int n = param.Match(source + offset); // note Match, not
// MatchUnchecked because we
// need to check validity after
// the offset
if (n == -1)
return -1;
offset += n;
@@ -181,6 +180,6 @@ inline int RegEx::MatchOpSeq(const Source& source) const {
return offset;
}
}
} // namespace YAML
#endif // REGEXIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66

View File

@@ -53,8 +53,8 @@ struct Token {
friend std::ostream& operator<<(std::ostream& out, const Token& token) {
out << TokenNames[token.type] << std::string(": ") << token.value;
for (std::size_t i = 0; i < token.params.size(); i++)
out << std::string(" ") << token.params[i];
for (const std::string& param : token.params)
out << std::string(" ") << param;
return out;
}