summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2016-05-20 20:37:00 +0200
committerAndreas Rumpf <rumpf_a@web.de>2016-05-20 20:37:00 +0200
commitec7d556c208725f336ff626c7be299cc14638e92 (patch)
treecedd8ea40fe7799af0f9fd130c93fb0a6853ec06
parent268f25225abab72e197274c31ebfee7ab6dbe0d4 (diff)
parent55c3b1444db0a4a7a01f4dcc955fc07498d5547e (diff)
downloadNim-ec7d556c208725f336ff626c7be299cc14638e92.tar.gz
Merge pull request #4191 from nigredo-tori/fix_tojsstr
Fix toJSStr for control characters
-rw-r--r--lib/system/jssys.nim6
-rw-r--r--tests/js/testtojsstr.nim8
2 files changed, 13 insertions, 1 deletions
diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim
index 99997efe6..ce67373bc 100644
--- a/lib/system/jssys.nim
+++ b/lib/system/jssys.nim
@@ -248,8 +248,12 @@ proc toJSStr(s: string): cstring {.asmNoStackFrame, compilerproc.} =
     for (var i = 0; i < len; ++i) {
       if (nonAsciiPart !== null) {
         var offset = (i - nonAsciiOffset) * 2;
+        var code = `s`[i].toString(16);
+        if (code.length == 1) {
+          code = "0"+code;
+        }
         nonAsciiPart[offset] = "%";
-        nonAsciiPart[offset + 1] = `s`[i].toString(16);
+        nonAsciiPart[offset + 1] = code;
       }
       else if (`s`[i] < 128)
         asciiPart[i] = fcc(`s`[i]);
diff --git a/tests/js/testtojsstr.nim b/tests/js/testtojsstr.nim
new file mode 100644
index 000000000..03ac89e20
--- /dev/null
+++ b/tests/js/testtojsstr.nim
@@ -0,0 +1,8 @@
+discard """
+  output = "И\n"
+"""
+
+let s: string = "И\n"
+let cs = s.cstring
+
+echo $s