October 17, 2020 How convert String to Fixed wchar Array? | ||||
---|---|---|---|---|
| ||||
How convert String to Fixed wchar Array? import std; void main(){ string name = "Marvin"; wchar[255] wtext = name.to!(wchar[]); // Can not convert. writeln(wtext); } |
October 16, 2020 Re: How convert String to Fixed wchar Array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | On 10/16/20 8:23 PM, Marcone wrote:
> How convert String to Fixed wchar Array?
>
> import std;
>
> void main(){
> string name = "Marvin";
> wchar[255] wtext = name.to!(wchar[]); // Can not convert.
> writeln(wtext);
> }
void main()
{
string name = "Marvin";
wchar[255] wtext;
import std.range;
wchar[] buf = wtext[];
put(buf, name);
auto setLen = wtext.length - buf.length; // number of wchars set in wtext.
writeln(wtext[0 .. setLen]);
}
A little bit of explanation: wchar[] is an output range that can accept any form of text. When you use `put` on it, it writes to it, AND shrinks from the front to allow remembering where it was. So at the end, buf is pointing at what is *left* in the buffer.
There may be better ways to do this, but this way will avoid any extra GC allocation.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation