sfmld

Convert Unicode const(uint)* to a dlang character type


I'm writing an SFML app in D using bindbc.sfml. System clipboard functions for Unicode in this SFML bindings get and return Unicode clipboard text as const(uint)* where uint is a Unicode code. How could I convert it to and from dlang character types such as wchar[] or wstring? I've figured out that for SFML text input events giving single uint codes in event.text.unicode I can do the following:

wstring ws = cast(wstring)[event.text.unicode];

but what to do with such pointers like const(uint)*? Is it even a pointer to an array of uints or am I mistaken?

After several try and error approaches the best I get is

// copied text: Hello
const(uint)* ct = cfClipboard_getUnicodeString();
writeln(to!wstring(*ct)); // prints 72

which gives only the first character in the clipboard text, but as a unicode code (e.g., 97 for latin a)

and this

// copied text: Hello
const(uint)* ct = cfClipboard_getUnicodeString();
writeln(cast(wstring)[*ct]); // prints H

which also gives only the first character, but as an printable char.


Solution

  • I'm not sure where the prototype for cfClipboard_getUnicodeString is, but if it is returning a uint*, it's probably a pointer to dchar code points.

    With caution, I'd say the answer is that the uint* is a zero-terminated array of utf32 code points. What I'd try is:

    import std.string;
    auto dstr = fromStringz(cast(const(dchar)*)ct);
    

    If that doesn't work, let me know where that function is defined, and we can go from there.