string mixup problem with stdin.byLine
Seb via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Aug 8 05:29:51 PDT 2016
On Monday, 8 August 2016 at 07:09:55 UTC, torea wrote:
> On Monday, 8 August 2016 at 05:17:24 UTC, Dave Akers wrote:
>> I do believe your problem is with the line...
>> On Monday, 8 August 2016 at 02:44:20 UTC, torea wrote:
>>> string cleanLine = strip( cast(string)line );
>> It's casting a char[] to and immutable(char)[], causing the
>> mutable buffer from byLine to be used as a string. what you
>> want is...
>> string cleanLine = strip( to!string(line) );
>> which should make a copy of the mutable buffer.
>>
>> I still a beginner at D but I think that will fix your problem.
>>
>> -Dave
>
> Problem fixed!!
> Thank you very much for the solution and the explanation!
You should always carefully read the description and Notes ;-)
> Note:
> Each front will not persist after popFront is called, so the
> caller must copy its contents (e.g. by calling to!string) when
> retention is needed. If the caller needs to retain a copy of
> every line, use the byLineCopy function instead.
http://dlang.org/phobos/std_stdio.html#.File.byLine
Unfortunately you are not the first one who bumped into this
problem and this non intuitive behavior of byLine is heavily
disputed.
More information about the Digitalmars-d-learn
mailing list