D 语言:如何将 Unicode 字符打印到控制台?

D-language: How to print Unicode characters to the console?

我有以下简单的程序可以从 3 个 unicode 字符集的联合中生成一个随机的 Unicode 字符串。

#!/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的输出是:

$ ./teste.d
Random salt:
rw13  13437 78580112 104 3914645

这些数字是多少? Unicode代码点?如何打印实际字符?我在 Ubuntu Linux 上,语言环境设置为 UTF-8

这一行是你遇到的问题:

dstring unichars =  to!dstring(unicodechars);

它将 CodepointSet 对象 unicode returns 转换为字符串,而不是它所涵盖的字符。该集合有一个名称和字符边界,但没有字符本身。花了这个:

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]))

并从该字符串中提取随机字符!相反,你想要:

dstring unichars =  to!dstring(unicodechars.byCodepoint);

对该对象调用 byCodepoint 方法将产生范围内的实际字符(好吧,代码点,unicode 很乱),然后从中得到一个字符串并将其随机化。