Fixed handling of surrogate pairs (#6332)
This commit is contained in:
parent
2ef65d5cdf
commit
d13535471b
2 changed files with 18 additions and 10 deletions
|
|
@ -1815,8 +1815,7 @@ when isMainModule:
|
||||||
doAssert(testJson["e"]["f"].bval)
|
doAssert(testJson["e"]["f"].bval)
|
||||||
|
|
||||||
# make sure UTF-16 decoding works.
|
# make sure UTF-16 decoding works.
|
||||||
when not defined(js): # TODO: The following line asserts in JS
|
doAssert(testJson["c"].str == "🎃")
|
||||||
doAssert(testJson["c"].str == "🎃")
|
|
||||||
doAssert(testJson["d"].str == "æ")
|
doAssert(testJson["d"].str == "æ")
|
||||||
|
|
||||||
# make sure no memory leek when parsing invalid string
|
# make sure no memory leek when parsing invalid string
|
||||||
|
|
|
||||||
|
|
@ -233,15 +233,24 @@ proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} =
|
||||||
if (ch < 128) {
|
if (ch < 128) {
|
||||||
result[r] = ch;
|
result[r] = ch;
|
||||||
}
|
}
|
||||||
else if((ch > 127) && (ch < 2048)) {
|
|
||||||
result[r] = (ch >> 6) | 192;
|
|
||||||
++r;
|
|
||||||
result[r] = (ch & 63) | 128;
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
result[r] = (ch >> 12) | 224;
|
if (ch < 2048) {
|
||||||
++r;
|
result[r] = (ch >> 6) | 192;
|
||||||
result[r] = ((ch >> 6) & 63) | 128;
|
}
|
||||||
|
else {
|
||||||
|
if (ch < 55296 || ch >= 57344) {
|
||||||
|
result[r] = (ch >> 12) | 224;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
++i;
|
||||||
|
ch = 65536 + (((ch & 1023) << 10) | (`c`.charCodeAt(i) & 1023));
|
||||||
|
result[r] = (ch >> 18) | 240;
|
||||||
|
++r;
|
||||||
|
result[r] = ((ch >> 12) & 63) | 128;
|
||||||
|
}
|
||||||
|
++r;
|
||||||
|
result[r] = ((ch >> 6) & 63) | 128;
|
||||||
|
}
|
||||||
++r;
|
++r;
|
||||||
result[r] = (ch & 63) | 128;
|
result[r] = (ch & 63) | 128;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue