From 85c41a1398b35c7a0dc1e8d0b0bbcede1f5c3862 Mon Sep 17 00:00:00 2001 From: nigredo-tori Date: Fri, 20 May 2016 12:17:57 +0600 Subject: [PATCH] Fix toJSStr for control characters fixes #4190 Add leading zero to encoded character if it is less than 0x10 --- lib/system/jssys.nim | 6 +++++- tests/js/testtojsstr.nim | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 tests/js/testtojsstr.nim diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim index 99997efe62..ce67373bcf 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 0000000000..c73b802541 --- /dev/null +++ b/tests/js/testtojsstr.nim @@ -0,0 +1,6 @@ +discard """ + output = "" +""" + +let s: string = "И\n" +let cs = s.cstring