summary refs log tree commit diff stats
path: root/lib/system
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2015-06-17 20:50:08 +0200
committerAndreas Rumpf <rumpf_a@web.de>2015-06-17 20:50:08 +0200
commit314fe6b54a4d374c0287e239d547ef095dd45440 (patch)
tree3ff3744391f15a9d73956b17362de0cdf82b045c /lib/system
parente2bcf22a849a8093ddb19f97660a519cc8e71371 (diff)
parent5824b1c83982c53cc493129ea67047906f7ae02a (diff)
downloadNim-314fe6b54a4d374c0287e239d547ef095dd45440.tar.gz
Merge pull request #2945 from yglukhov/fix-2917
Fixes #2917
Diffstat (limited to 'lib/system')
-rw-r--r--lib/system/jssys.nim19
1 files changed, 16 insertions, 3 deletions
diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim
index 5a9333a0e..82cf18299 100644
--- a/lib/system/jssys.nim
+++ b/lib/system/jssys.nim
@@ -175,12 +175,25 @@ proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} =
 proc toJSStr(s: string): cstring {.asmNoStackFrame, compilerproc.} =
   asm """
     var len = `s`.length-1;
-    var result = new Array(len);
+    var asciiPart = new Array(len);
     var fcc = String.fromCharCode;
+    var nonAsciiPart = null;
     for (var i = 0; i < len; ++i) {
-      result[i] = fcc(`s`[i]);
+      if (nonAsciiPart !== null) {
+        nonAsciiPart[i * 2] = "%";
+        nonAsciiPart[i * 2 + 1] = `s`[i].toString(16);
+      }
+      else if (`s`[i] < 128)
+        asciiPart[i] = fcc(`s`[i]);
+      else {
+        asciiPart.length = i;
+        nonAsciiPart = new Array((len - i) * 2);
+        --i;
+      }
     }
-    return result.join("");
+    asciiPart = asciiPart.join("");
+    return (nonAsciiPart === null) ?
+        asciiPart : asciiPart + decodeURIComponent(nonAsciiPart.join(""));
   """
 
 proc mnewString(len: int): string {.asmNoStackFrame, compilerproc.} =