mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 20:51:16 +00:00
Modernize: Use range-based for loops for readability (#762)
Also run clang-format on these files as requested
This commit is contained in:

committed by
Jesse Beder

parent
21d75fa4cd
commit
b650bc8287
@@ -42,9 +42,8 @@ class node {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
m_pRef->mark_defined();
|
m_pRef->mark_defined();
|
||||||
for (nodes::iterator it = m_dependencies.begin();
|
for (node* dependency : m_dependencies)
|
||||||
it != m_dependencies.end(); ++it)
|
dependency->mark_defined();
|
||||||
(*it)->mark_defined();
|
|
||||||
m_dependencies.clear();
|
m_dependencies.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,7 +159,7 @@ class node {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
shared_node_ref m_pRef;
|
shared_node_ref m_pRef;
|
||||||
using nodes = std::set<node *>;
|
using nodes = std::set<node*>;
|
||||||
nodes m_dependencies;
|
nodes m_dependencies;
|
||||||
};
|
};
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
@@ -16,8 +16,8 @@ std::string tolower(const std::string& str) {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool IsEntirely(const std::string& str, T func) {
|
bool IsEntirely(const std::string& str, T func) {
|
||||||
for (std::size_t i = 0; i < str.size(); i++)
|
for (char ch : str)
|
||||||
if (!func(str[i]))
|
if (!func(ch))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -39,7 +39,7 @@ bool IsFlexibleCase(const std::string& str) {
|
|||||||
std::string rest = str.substr(1);
|
std::string rest = str.substr(1);
|
||||||
return firstcaps && (IsEntirely(rest, IsLower) || IsEntirely(rest, IsUpper));
|
return firstcaps && (IsEntirely(rest, IsLower) || IsEntirely(rest, IsUpper));
|
||||||
}
|
}
|
||||||
}
|
} // namespace
|
||||||
|
|
||||||
namespace YAML {
|
namespace YAML {
|
||||||
bool convert<bool>::decode(const Node& node, bool& rhs) {
|
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 {
|
static const struct {
|
||||||
std::string truename, falsename;
|
std::string truename, falsename;
|
||||||
} names[] = {
|
} names[] = {
|
||||||
{"y", "n"}, {"yes", "no"}, {"true", "false"}, {"on", "off"},
|
{"y", "n"},
|
||||||
|
{"yes", "no"},
|
||||||
|
{"true", "false"},
|
||||||
|
{"on", "off"},
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!IsFlexibleCase(node.Scalar()))
|
if (!IsFlexibleCase(node.Scalar()))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (unsigned i = 0; i < sizeof(names) / sizeof(names[0]); i++) {
|
for (const auto& name : names) {
|
||||||
if (names[i].truename == tolower(node.Scalar())) {
|
if (name.truename == tolower(node.Scalar())) {
|
||||||
rhs = true;
|
rhs = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (names[i].falsename == tolower(node.Scalar())) {
|
if (name.falsename == tolower(node.Scalar())) {
|
||||||
rhs = false;
|
rhs = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -72,4 +75,4 @@ bool convert<bool>::decode(const Node& node, bool& rhs) {
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
} // namespace YAML
|
||||||
|
@@ -199,11 +199,11 @@ bool IsValidPlainScalar(const std::string& str, FlowType::value flowType,
|
|||||||
|
|
||||||
bool IsValidSingleQuotedScalar(const std::string& str, bool escapeNonAscii) {
|
bool IsValidSingleQuotedScalar(const std::string& str, bool escapeNonAscii) {
|
||||||
// TODO: check for non-printable characters?
|
// TODO: check for non-printable characters?
|
||||||
for (std::size_t i = 0; i < str.size(); i++) {
|
for (char ch : str) {
|
||||||
if (escapeNonAscii && (0x80 <= static_cast<unsigned char>(str[i]))) {
|
if (escapeNonAscii && (0x80 <= static_cast<unsigned char>(ch))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (str[i] == '\n') {
|
if (ch == '\n') {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -217,8 +217,8 @@ bool IsValidLiteralScalar(const std::string& str, FlowType::value flowType,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check for non-printable characters?
|
// TODO: check for non-printable characters?
|
||||||
for (std::size_t i = 0; i < str.size(); i++) {
|
for (char ch : str) {
|
||||||
if (escapeNonAscii && (0x80 <= static_cast<unsigned char>(str[i]))) {
|
if (escapeNonAscii && (0x80 <= static_cast<unsigned char>(ch))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -12,8 +12,7 @@ namespace YAML {
|
|||||||
namespace Exp {
|
namespace Exp {
|
||||||
unsigned ParseHex(const std::string& str, const Mark& mark) {
|
unsigned ParseHex(const std::string& str, const Mark& mark) {
|
||||||
unsigned value = 0;
|
unsigned value = 0;
|
||||||
for (std::size_t i = 0; i < str.size(); i++) {
|
for (char ch : str) {
|
||||||
char ch = str[i];
|
|
||||||
int digit = 0;
|
int digit = 0;
|
||||||
if ('a' <= ch && ch <= 'f')
|
if ('a' <= ch && ch <= 'f')
|
||||||
digit = ch - 'a' + 10;
|
digit = ch - 'a' + 10;
|
||||||
@@ -132,5 +131,5 @@ std::string Escape(Stream& in) {
|
|||||||
std::stringstream msg;
|
std::stringstream msg;
|
||||||
throw ParserException(in.mark(), std::string(ErrorMsg::INVALID_ESCAPE) + ch);
|
throw ParserException(in.mark(), std::string(ErrorMsg::INVALID_ESCAPE) + ch);
|
||||||
}
|
}
|
||||||
}
|
} // namespace Exp
|
||||||
}
|
} // namespace YAML
|
||||||
|
@@ -31,8 +31,8 @@ void ostream_wrapper::write(const std::string& str) {
|
|||||||
std::copy(str.begin(), str.end(), m_buffer.begin() + m_pos);
|
std::copy(str.begin(), str.end(), m_buffer.begin() + m_pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (std::size_t i = 0; i < str.size(); i++) {
|
for (char ch : str) {
|
||||||
update_pos(str[i]);
|
update_pos(ch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -8,8 +8,8 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "stream.h"
|
#include "stream.h"
|
||||||
#include "stringsource.h"
|
|
||||||
#include "streamcharsource.h"
|
#include "streamcharsource.h"
|
||||||
|
#include "stringsource.h"
|
||||||
|
|
||||||
namespace YAML {
|
namespace YAML {
|
||||||
// query matches
|
// query matches
|
||||||
@@ -106,9 +106,8 @@ inline int RegEx::MatchOpEmpty(const Source& source) const {
|
|||||||
template <>
|
template <>
|
||||||
inline int RegEx::MatchOpEmpty<StringCharSource>(
|
inline int RegEx::MatchOpEmpty<StringCharSource>(
|
||||||
const StringCharSource& source) const {
|
const StringCharSource& source) const {
|
||||||
return !source
|
return !source ? 0 : -1; // the empty regex only is successful on the empty
|
||||||
? 0
|
// string
|
||||||
: -1; // the empty regex only is successful on the empty string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MatchOperator
|
// MatchOperator
|
||||||
@@ -130,8 +129,8 @@ inline int RegEx::MatchOpRange(const Source& source) const {
|
|||||||
// OrOperator
|
// OrOperator
|
||||||
template <typename Source>
|
template <typename Source>
|
||||||
inline int RegEx::MatchOpOr(const Source& source) const {
|
inline int RegEx::MatchOpOr(const Source& source) const {
|
||||||
for (std::size_t i = 0; i < m_params.size(); i++) {
|
for (const RegEx& param : m_params) {
|
||||||
int n = m_params[i].MatchUnchecked(source);
|
int n = param.MatchUnchecked(source);
|
||||||
if (n >= 0)
|
if (n >= 0)
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
@@ -169,11 +168,11 @@ inline int RegEx::MatchOpNot(const Source& source) const {
|
|||||||
template <typename Source>
|
template <typename Source>
|
||||||
inline int RegEx::MatchOpSeq(const Source& source) const {
|
inline int RegEx::MatchOpSeq(const Source& source) const {
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
for (std::size_t i = 0; i < m_params.size(); i++) {
|
for (const RegEx& param : m_params) {
|
||||||
int n = m_params[i].Match(source + offset); // note Match, not
|
int n = param.Match(source + offset); // note Match, not
|
||||||
// MatchUnchecked because we
|
// MatchUnchecked because we
|
||||||
// need to check validity after
|
// need to check validity after
|
||||||
// the offset
|
// the offset
|
||||||
if (n == -1)
|
if (n == -1)
|
||||||
return -1;
|
return -1;
|
||||||
offset += n;
|
offset += n;
|
||||||
@@ -181,6 +180,6 @@ inline int RegEx::MatchOpSeq(const Source& source) const {
|
|||||||
|
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
}
|
} // namespace YAML
|
||||||
|
|
||||||
#endif // REGEXIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
#endif // REGEXIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||||
|
@@ -53,8 +53,8 @@ struct Token {
|
|||||||
|
|
||||||
friend std::ostream& operator<<(std::ostream& out, const Token& token) {
|
friend std::ostream& operator<<(std::ostream& out, const Token& token) {
|
||||||
out << TokenNames[token.type] << std::string(": ") << token.value;
|
out << TokenNames[token.type] << std::string(": ") << token.value;
|
||||||
for (std::size_t i = 0; i < token.params.size(); i++)
|
for (const std::string& param : token.params)
|
||||||
out << std::string(" ") << token.params[i];
|
out << std::string(" ") << param;
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user