D-language: How to print Unicode characters to the console?
我有以下简单的程序从 3 个 unicode 字符集的并集生成随机 Unicode 字符串。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #!/usr/bin/env rdmd import std.uni; import std.random : randomSample; import std.stdio; import std.conv; /** * Random salt generator */ dstring get_salt(uint s) { auto unicodechars = unicode("Cyrillic") | unicode("Armenian") | unicode("Telugu"); dstring unichars = to!dstring(unicodechars); return to!dstring(randomSample(unichars, s)); } void main() { writeln("Random salt:"); writeln(get_salt(32)); } |
但是,writeln 的输出是:
1 2 3 | $ ./teste.d Random salt: rw13 13437 78580112 104 3914645 |
这些数字是多少? Unicode 代码点?如何打印实际字符?我在 Ubuntu Linux 上,区域设置为 UTF-8
这行是你遇到的问题:
1 | dstring unichars = to!dstring(unicodechars); |
它将
1 | InversionList!(GcPolicy)(CowArray!(GcPolicy)([1024, 1157, 1159, 1320, 1329, 1367, 1369, 1376, 1377, 1416, 1418, 1419, 1423, 1424, 3073, 3076, 3077, 3085, 3086, 3089, 3090, 3113, 3114, 3124, 3125, 3130, 3133, 3141, 3142, 3145, 3146, 3150, 3157, 3159, 3160, 3162, 3168, 3172, 3174, 3184, 3192, 3200, 7467, 7468, 7544, 7545, 11744, 11776, 42560, 42648, 42655, 42656, 64275, 64280, 5])) |
并从该字符串中提取随机字符!相反,你想要:
1 | dstring unichars = to!dstring(unicodechars.byCodepoint); |
在该对象上调用