www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How convert String to Fixed wchar Array?

reply Marcone <marcone email.com> writes:
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);
}
Oct 16 2020
parent Steven Schveighoffer <schveiguy gmail.com> writes:
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
Oct 16 2020