Skip to content

Commit c158322

Browse files
committed
review comments
1 parent a632154 commit c158322

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

src/lib_json/json_writer.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -317,25 +317,28 @@ static String valueToQuotedStringN(const char* value, unsigned length,
317317
// Should add a flag to allow this compatibility mode and prevent this
318318
// sequence from occurring.
319319
default: {
320-
unsigned codepoint;
321320
if (emitUTF8) {
322-
codepoint = static_cast<unsigned char>(*c);
323-
} else {
324-
codepoint = utf8ToCodepoint(c, end); // modifies `c`
325-
}
326-
327-
if (codepoint < 0x20) {
328-
appendHex(result, codepoint);
329-
} else if (codepoint < 0x80 || emitUTF8) {
330-
appendRaw(result, codepoint);
331-
} else if (codepoint < 0x10000) {
332-
// Basic Multilingual Plane
333-
appendHex(result, codepoint);
321+
unsigned codepoint = static_cast<unsigned char>(*c);
322+
if (codepoint < 0x20) {
323+
appendHex(result, codepoint);
324+
} else {
325+
appendRaw(result, codepoint);
326+
}
334327
} else {
335-
// Extended Unicode. Encode 20 bits as a surrogate pair.
336-
codepoint -= 0x10000;
337-
appendHex(result, 0xd800 + ((codepoint >> 10) & 0x3ff));
338-
appendHex(result, 0xdc00 + (codepoint & 0x3ff));
328+
unsigned codepoint = utf8ToCodepoint(c, end); // modifies `c`
329+
if (codepoint < 0x20) {
330+
appendHex(result, codepoint);
331+
} else if (codepoint < 0x80) {
332+
appendRaw(result, codepoint);
333+
} else if (codepoint < 0x10000) {
334+
// Basic Multilingual Plane
335+
appendHex(result, codepoint);
336+
} else {
337+
// Extended Unicode. Encode 20 bits as a surrogate pair.
338+
codepoint -= 0x10000;
339+
appendHex(result, 0xd800 + ((codepoint >> 10) & 0x3ff));
340+
appendHex(result, 0xdc00 + (codepoint & 0x3ff));
341+
}
339342
}
340343
} break;
341344
}
@@ -866,8 +869,7 @@ struct CommentStyle {
866869
/// Decide whether to write comments.
867870
enum Enum {
868871
None, ///< Drop all comments.
869-
Most, ///< Recover odd behavior of previous versions (not implemented
870-
///< yet).
872+
Most, ///< Recover odd behavior of previous versions (not implemented yet).
871873
All ///< Keep all comments.
872874
};
873875
};

src/test_lib_json/main.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,20 +2673,26 @@ JSONTEST_FIXTURE_LOCAL(StreamWriterTest, escapeControlCharacters) {
26732673
};
26742674

26752675
Json::StreamWriterBuilder b;
2676-
b.settings_["emitUTF8"] = true;
26772676

2678-
for (unsigned i = 0; i != 0x100; ++i) {
2679-
std::string raw({static_cast<char>(i)});
2680-
std::string esc = raw;
2681-
if (i < 0x20)
2682-
esc = uEscape(i);
2683-
if (const char* shEsc = shortEscape(i))
2684-
esc = shEsc;
2685-
Json::Value root;
2686-
root["test"] = raw;
2687-
JSONTEST_ASSERT_STRING_EQUAL(
2688-
std::string("{\n\t\"test\" : \"").append(esc).append("\"\n}"),
2689-
Json::writeString(b, root));
2677+
for (bool emitUTF8 : {true, false}) {
2678+
b.settings_["emitUTF8"] = emitUTF8;
2679+
2680+
for (unsigned i = 0; i != 0x100; ++i) {
2681+
std::string raw({static_cast<char>(i)});
2682+
std::string esc = raw;
2683+
if (i < 0x20)
2684+
esc = uEscape(i);
2685+
if (const char* shEsc = shortEscape(i))
2686+
esc = shEsc;
2687+
if (!emitUTF8 && i >= 0x80)
2688+
esc = uEscape(i);
2689+
2690+
Json::Value root;
2691+
root["test"] = raw;
2692+
JSONTEST_ASSERT_STRING_EQUAL(
2693+
std::string("{\n\t\"test\" : \"").append(esc).append("\"\n}"),
2694+
Json::writeString(b, root));
2695+
}
26902696
}
26912697
}
26922698

0 commit comments

Comments
 (0)