@@ -309,23 +309,28 @@ static String valueToQuotedStringN(const char* value, unsigned length,
309
309
// Should add a flag to allow this compatibility mode and prevent this
310
310
// sequence from occurring.
311
311
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
+
312
316
const auto appendHexChar = [&result](unsigned ch) {
313
317
result.append (" \\ u" ).append (toHex16Bit (ch));
314
318
};
315
319
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) {
318
322
codepoint = utf8ToCodepoint (c, end);
319
- if (codepoint < 0x10000 ) {
323
+ if (codepoint < FIRST_SURROGATE_PAIR_CODEPOINT ) {
320
324
// codepoint is in Basic Multilingual Plane
321
325
appendHexChar (codepoint);
322
326
} else {
323
327
// codepoint is not in Basic Multilingual Plane
324
- codepoint -= 0x10000 ;
328
+ // convert to surrogate pair first
329
+ codepoint -= FIRST_SURROGATE_PAIR_CODEPOINT;
325
330
appendHexChar (0xD800 + (codepoint >> 10 ));
326
331
appendHexChar (0xDC00 + (codepoint & 0x3FF ));
327
332
}
328
- } else if (codepoint < 0x20 ) {
333
+ } else if (codepoint < FIRST_NON_CONTROL_CODEPOINT ) {
329
334
appendHexChar (codepoint);
330
335
} else {
331
336
result += static_cast <char >(codepoint);
0 commit comments