Skip to content

Commit df11b76

Browse files
committed
use std::any_of
Also simplified two loops. Signed-off-by: Rosen Penev <[email protected]>
1 parent f334fcb commit df11b76

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

src/lib_json/json_reader.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <json/reader.h>
1111
#include <json/value.h>
1212
#endif // if !defined(JSON_IS_AMALGAMATION)
13+
#include <algorithm>
1314
#include <cassert>
1415
#include <cstring>
1516
#include <iostream>
@@ -77,10 +78,7 @@ Features Features::strictMode() {
7778
// ////////////////////////////////
7879

7980
bool Reader::containsNewLine(Reader::Location begin, Reader::Location end) {
80-
for (; begin < end; ++begin)
81-
if (*begin == '\n' || *begin == '\r')
82-
return true;
83-
return false;
81+
return std::any_of(begin, end, [](char b) { return b == '\n' || b == '\r'; });
8482
}
8583

8684
// Class Reader
@@ -998,10 +996,7 @@ class OurReader {
998996

999997
bool OurReader::containsNewLine(OurReader::Location begin,
1000998
OurReader::Location end) {
1001-
for (; begin < end; ++begin)
1002-
if (*begin == '\n' || *begin == '\r')
1003-
return true;
1004-
return false;
999+
return std::any_of(begin, end, [](char b) { return b == '\n' || b == '\r'; });
10051000
}
10061001

10071002
OurReader::OurReader(OurFeatures const& features) : features_(features) {}

src/lib_json/json_writer.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include "json_tool.h"
88
#include <json/writer.h>
99
#endif // if !defined(JSON_IS_AMALGAMATION)
10+
#include <algorithm>
1011
#include <cassert>
12+
#include <cctype>
1113
#include <cstring>
1214
#include <iomanip>
1315
#include <memory>
@@ -176,14 +178,9 @@ String valueToString(bool value) { return value ? "true" : "false"; }
176178
static bool isAnyCharRequiredQuoting(char const* s, size_t n) {
177179
assert(s || !n);
178180

179-
char const* const end = s + n;
180-
for (char const* cur = s; cur < end; ++cur) {
181-
if (*cur == '\\' || *cur == '\"' ||
182-
static_cast<unsigned char>(*cur) < ' ' ||
183-
static_cast<unsigned char>(*cur) >= 0x80)
184-
return true;
185-
}
186-
return false;
181+
return std::any_of(s, s + n, [](int c) {
182+
return c == '\\' || c == '"' || !std::isprint(c);
183+
});
187184
}
188185

189186
static unsigned int utf8ToCodepoint(const char*& s, const char* e) {

0 commit comments

Comments
 (0)