Same process to different results?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 1 10:44:47 PDT 2015


On 7/1/15 1:00 PM, Taylor Hillegeist wrote:
> When I run the code (compiled on DMD 2.067.1):
>
>
> ------------------------------------------------------
> import std.algorithm;
> import std.stdio;
> import std.range;
>
> string A="AaA";
> string B="BbBb";
> string C="CcCcC";
>
> void main(){
>      int L=25;
>
>    int seg1len=(L-B.length)/2;
>    int seg2len=B.length;
>    int seg3len=L-seg1len-seg2len;
>
>    (A.cycle.take(seg1len).array
>    ~B.cycle.take(seg2len).array
>    ~C.cycle.take(seg3len).array).writeln;
>
>    string q = cast(string)
>    (A.cycle.take(seg1len).array
>    ~B.cycle.take(seg2len).array
>    ~C.cycle.take(seg3len).array);
>
>    q.writeln;
>
> }
> -----------------------------------------------
>
> I get a weird result of
> AaAAaAAaAABbBbCcCcCCcCcCC
> A   a   A   A   a   A   A   a   A   A   B   b   B   b   C   c   C   c
> C   C   c   C   c   C   C
>
> Any ideas why?

Schizophrenia of Phobos.

Phobos thinks a string is a range of dchar instead of a range of char. 
So what cycle, take, and array all output are dchar ranges and arrays.

When you cast the dchar[] result to a string, (which is a char[]), it 
then treats all the 0's in each dchar element as '\0', printing a blank 
apparently.

-Steve


More information about the Digitalmars-d-learn mailing list