[Issue 24570] printing a range of ranges consumes sub-ranges

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue May 28 23:29:14 UTC 2024


https://issues.dlang.org/show_bug.cgi?id=24570

--- Comment #4 from Steven Schveighoffer <schveiguy at gmail.com> ---
(In reply to Salih Dincer from comment #2)

> If you do the following instead of `auto save() => return this;`, the
> problem is solved:
> 
> ```
> struct R
> {
>   wchar* ptr;
>   size_t len;
>   
>   this(T)(T[] range)
>   {
>     ptr = cast(wchar*)range.ptr;
>     len = range.length;
>   }
>   
>   auto empty() => len == 0;
>   auto front() => *ptr++;

this is an invalid implementation. Using `front` more than once is supported.

>   auto popFront() => len--;
>   auto save()
>   {
>     auto r = R([]);
>     r.len = len;
>     r.ptr = ptr;
>     return r;
>   }
> }
> 
> void main()
> {
>   auto c = ['€', '₺', '₽'];
>   auto r = R(c);
>   auto arr = [r, r, r];
> 
>   assert(!arr.empty);
>   
>   import std.conv : text;
>   auto str = arr.text; // "€₺₽"
>   
>   assert(!arr.empty);

But it was never arr that was empty. It's just an array of empty ranges at this
point.

But the difference here is that you are using wchar and not short. format
treats character data differently (as evidenced by the printout, it's not an
array of items, but a string).

Yet another reason why this bug has gone undetected for so long.

--


More information about the Digitalmars-d-bugs mailing list