How to collect "some" items of a range in an array?
Dukc
ajieskola at gmail.com
Fri Jan 10 21:27:17 UTC 2025
On Monday, 6 January 2025 at 19:30:27 UTC, Renato wrote:
> My use case is to parse a file format where a line is expected
> to have 4 items. I am currently using `takeExactly(4)` but that
> doesn't let me index on it.
I suspect you might unknowingly be the victim of autodecoding. If
your line is a string, ergo an array of immutable `char`s (or a
`wstring`), Phobos functions will treat that as a range of
`dchar`s. Since reading four Unicode code points (`dchar`s) might
potentially consume more than four UTF-8 code points (`char`s)
from the line, the result of `takeExactly` can't be random access.
You could use
[`.byCodeUnit`](https://dlang.org/phobos/std_utf.html#byCodeUnit)
on your line before giving it to `takeExactly`. I think
`takeExactly` is random access if it's source range is. However,
be aware that this will split any non-ASCII Unicode symbol in the
line to multiple characters, all invalid by themselves. It
depends on your use case whether this matters or not. If it does,
the `staticArray` solution earlier in this thread is probably
what you want instead.
More information about the Digitalmars-d-learn
mailing list