From a database query I get strings like that :
"\u5f9e\u6e05\u6668\u958b\u59cb\u4e00\u76f4\u4e0b\u96e8\u3002"
if this string is hardcoded then display via echo is as expected, however, if the string is passed in on command line echo fails to do its magic.
# test123.nim
# run like: ./test123 "\u5f9e\u6e05\u6668\u958b\u59cb\u4e00\u76f4\u4e0b\u96e8\u3002"
import os
let a = "\u5f9e\u6e05\u6668\u958b\u59cb\u4e00\u76f4\u4e0b\u96e8\u3002"
let b = paramstr(1)
echo a # output ok
echo b # output crap
Output in an linux terminal:
從清晨開始一直下雨。
\u5f9e\u6e05\u6668\u958b\u59cb\u4e00\u76f4\u4e0b\u96e8\u3002
How to make output of b same as a ?
Hacky way - rely on json decoding:
import os, json
echo parseJson("\"" & paramstr(1) & "\"").getStr()
Proper way - implement decoding yourself ;)Proper way - implement decoding yourself
I would guess that decoding is contained in Nim compiler source code already, as literal unicode string seems to work?
Is "u" escape sequence documented at all in compiler manual?
To make the text display correctly, you need to set the encoding of the string(byte array) and the terminal codepage correctly.
If you encoding of the string(Depending you source file or the source of the string) is unicode or utf8(\u5f9e is unicode), you need to set the terminal codepage to 65001 using command "chcp 65001".
If you encoding of the string is Big5, you need to set the terminal codepage to 950, using command "chcp 950".
Thank you all for the replies ... for the time being I go with the 'Hack way' as it resolves the issue at hand without the need to change terminal encoding or doing it the 'Proper way' :)
If there is yet another solution , I sure still would like to learn about it.
There's a utility method in the json module you can directly use.
import os, json
echo escapeJson(paramStr(1))
The implementation is quite simple and if you want special string quoting syntax you could implement it differently yourself with little effort.