From 94e336fe850587f8c571c163bc9b567ecb491f82 Mon Sep 17 00:00:00 2001 From: Simon Krauter Date: Fri, 15 Sep 2017 10:45:22 +0200 Subject: Fix wrong result of countLines() (#6371) --- tests/stdlib/tstrutil.nim | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'tests/stdlib') diff --git a/tests/stdlib/tstrutil.nim b/tests/stdlib/tstrutil.nim index b5e3db4e2..fef1b38c2 100644 --- a/tests/stdlib/tstrutil.nim +++ b/tests/stdlib/tstrutil.nim @@ -89,9 +89,21 @@ proc testRFind = assert "0123456789ABCDEFGAH".rfind({'A'..'C'}, 13) == 12 assert "0123456789ABCDEFGAH".rfind({'G'..'H'}, 13) == -1 +proc testCountLines = + proc assertCountLines(s: string) = assert s.countLines == s.splitLines.len + assertCountLines("") + assertCountLines("\n") + assertCountLines("\n\n") + assertCountLines("abc") + assertCountLines("abc\n123") + assertCountLines("abc\n123\n") + assertCountLines("\nabc\n123") + assertCountLines("\nabc\n123\n") + testDelete() testFind() testRFind() +testCountLines() assert(insertSep($1000_000) == "1_000_000") assert(insertSep($232) == "232") -- cgit 1.4.1-2-gfad0 From 7d49fc796da309afd426c62ba4c57d49e8e3a530 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Fri, 15 Sep 2017 11:53:58 +0300 Subject: Changed JSON stringification to preserve UTF (#6330) --- lib/pure/json.nim | 29 +++++++++++------------------ tests/stdlib/tmarshal.nim | 2 +- 2 files changed, 12 insertions(+), 19 deletions(-) (limited to 'tests/stdlib') diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 9dc9b51f3..097952588 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -996,24 +996,17 @@ proc nl(s: var string, ml: bool) = proc escapeJson*(s: string; result: var string) = ## Converts a string `s` to its JSON representation. ## Appends to ``result``. - const - HexChars = "0123456789ABCDEF" result.add("\"") - for x in runes(s): - var r = int(x) - if r >= 32 and r <= 126: - var c = chr(r) - case c - of '"': result.add("\\\"") - of '\\': result.add("\\\\") - else: result.add(c) - else: - # toHex inlined for more speed (saves stupid string allocations): - result.add("\\u0000") - let start = result.len - 4 - for j in countdown(3, 0): - result[j+start] = HexChars[r and 0xF] - r = r shr 4 + for c in s: + case c + of '\L': result.add("\\n") + of '\b': result.add("\\b") + of '\f': result.add("\\f") + of '\t': result.add("\\t") + of '\r': result.add("\\r") + of '"': result.add("\\\"") + of '\\': result.add("\\\\") + else: result.add(c) result.add("\"") proc escapeJson*(s: string): string = @@ -1925,7 +1918,7 @@ when isMainModule: var parsed2 = parseFile("tests/testdata/jsontest2.json") doAssert(parsed2{"repository", "description"}.str=="IRC Library for Haskell", "Couldn't fetch via multiply nested key using {}") - doAssert escapeJson("\10FoobarÄ") == "\"\\u000AFoobar\\u00C4\"" + doAssert escapeJson("\10Foo🎃barÄ") == "\"\\nFoo🎃barÄ\"" # Test with extra data when not defined(js): diff --git a/tests/stdlib/tmarshal.nim b/tests/stdlib/tmarshal.nim index 434caa281..38937590f 100644 --- a/tests/stdlib/tmarshal.nim +++ b/tests/stdlib/tmarshal.nim @@ -1,5 +1,5 @@ discard """ - output: '''{"age": 12, "bio": "\u042F Cletus", "blob": [65, 66, 67, 128], "name": "Cletus"} + output: '''{"age": 12, "bio": "Я Cletus", "blob": [65, 66, 67, 128], "name": "Cletus"} true true alpha 100 -- cgit 1.4.1-2-gfad0