Copying copy?

monarch_dodra monarchdodra at gmail.com
Fri Jul 26 08:56:21 PDT 2013


On Friday, 26 July 2013 at 15:31:06 UTC, bearophile wrote:
> Dicebot:
>
>> Not possible, front is 'dchar' for char arrays:
>
> So do I have to use foreach loops to fill a char[] lazily? :-)
>
> Bye,
> bearophile

You could also use appender. Appender supports it:

//----
import std.array, std.algorithm, std.stdio;
void main()
{
   dchar[3] d = "日本語";
   string s = "I speak ";
   auto app = appender(s);
   app.put(d[]);
   writeln(app.data);
}
//----

That said, I'd suggest to not pass an array to appender when 
building it: the current implementation makes a lot of 
assumptions about the state of its internal buffer, which end up 
being wrong when you pass an array as above. These assumption can 
lead to some potentially very nasty effects, including plain old 
segfault.

Today, I'd suggest instead writing it as:

//----
import std.array, std.algorithm, std.stdio;
void main()
{
   dchar[3] d = "日本語";
   string s = "I speak ";
   auto app = Appender!(string)();
   app.put(s);
   app.put(d[]);
   s = app.data;
   writeln(s);
}
//----

Doing this is safe.


More information about the Digitalmars-d-learn mailing list