Skip to content

Commit 0c6deca

Browse files
committed
Code-review fixes 3
1 parent 8a1417e commit 0c6deca

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Diff for: src/lib_json/json_writer.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -309,23 +309,28 @@ static String valueToQuotedStringN(const char* value, unsigned length,
309309
// Should add a flag to allow this compatibility mode and prevent this
310310
// sequence from occurring.
311311
default: {
312+
const unsigned int FIRST_NON_CONTROL_CODEPOINT = 0x20;
313+
const unsigned int LAST_NON_CONTROL_CODEPOINT = 0x7F;
314+
const unsigned int FIRST_SURROGATE_PAIR_CODEPOINT = 0x10000;
315+
312316
const auto appendHexChar = [&result](unsigned ch) {
313317
result.append("\\u").append(toHex16Bit(ch));
314318
};
315319

316-
unsigned codepoint = static_cast<unsigned>(*c);
317-
if (codepoint > 0x7F && !emitUTF8) {
320+
unsigned int codepoint = static_cast<unsigned>(*c);
321+
if (codepoint > LAST_NON_CONTROL_CODEPOINT && !emitUTF8) {
318322
codepoint = utf8ToCodepoint(c, end);
319-
if (codepoint < 0x10000) {
323+
if (codepoint < FIRST_SURROGATE_PAIR_CODEPOINT) {
320324
// codepoint is in Basic Multilingual Plane
321325
appendHexChar(codepoint);
322326
} else {
323327
// codepoint is not in Basic Multilingual Plane
324-
codepoint -= 0x10000;
328+
// convert to surrogate pair first
329+
codepoint -= FIRST_SURROGATE_PAIR_CODEPOINT;
325330
appendHexChar(0xD800 + (codepoint >> 10));
326331
appendHexChar(0xDC00 + (codepoint & 0x3FF));
327332
}
328-
} else if (codepoint < 0x20) {
333+
} else if (codepoint < FIRST_NON_CONTROL_CODEPOINT) {
329334
appendHexChar(codepoint);
330335
} else {
331336
result += static_cast<char>(codepoint);

0 commit comments

Comments
 (0)