Merge pull request #3733 from yglukhov/js-unicode

Fixed unicode handling in JS. Fixes #3714.
This commit is contained in:
Andreas Rumpf
2016-01-21 15:30:21 +01:00
2 changed files with 45 additions and 8 deletions

View File

@@ -166,14 +166,33 @@ proc SetConstr() {.varargs, asmNoStackFrame, compilerproc.} =
"""
proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} =
asm """
var result = [];
for (var i = 0; i < `c`.length; ++i) {
result[i] = `c`.charCodeAt(i);
{.emit: """
var ln = `c`.length;
var result = new Array(ln);
var r = 0;
for (var i = 0; i < ln; ++i) {
var ch = `c`.charCodeAt(i);
if (ch < 128) {
result[r] = ch;
}
result[result.length] = 0; // terminating zero
return result;
"""
else if((ch > 127) && (ch < 2048)) {
result[r] = (ch >> 6) | 192;
++r;
result[r] = (ch & 63) | 128;
}
else {
result[r] = (ch >> 12) | 224;
++r;
result[r] = ((ch >> 6) & 63) | 128;
++r;
result[r] = (ch & 63) | 128;
}
++r;
}
result[r] = 0; // terminating zero
return result;
""".}
proc toJSStr(s: string): cstring {.asmNoStackFrame, compilerproc.} =
asm """