-
-
Notifications
You must be signed in to change notification settings - Fork 705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
printing a range of ranges consumes sub-ranges #10553
Labels
Comments
schveiguy (@schveiguy) commented on 2024-05-27T14:58:33ZA further note is that an array of arrays is not consumed when printed -- because formatValue has a specialized case for that. |
salihdb commented on 2024-05-28T06:05:31Z(In reply to Steven Schveighoffer from comment #1)
> A further note is that an array of arrays is not consumed when printed --
> because formatValue has a specialized case for that.
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++;
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);
}
``` |
salihdb commented on 2024-05-28T06:33:51ZThere's no reason why this issue can't be easily fixed. Because when you include narrow string or wchar, there is no problem of not being able to save(): https://forum.dlang.org/post/qgtrcupsniezqgazkztd@forum.dlang.org |
schveiguy (@schveiguy) commented on 2024-05-28T23:29:14Z(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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
schveiguy (@schveiguy) reported this on 2024-05-27T14:57:08Z
Transfered from https://issues.dlang.org/show_bug.cgi?id=24570
CC List
Description
The text was updated successfully, but these errors were encountered: