Iterating chars by Word
evilrat
evilrat666 at gmail.com
Fri Nov 13 06:42:24 UTC 2020
On Friday, 13 November 2020 at 05:14:08 UTC, Виталий Фадеев wrote:
> Is:
> wchar[] chars; // like a: "import
> core.sys.windows.windows;\nimport std.conv : to;\n"
>
> Goal:
> foreach ( word; chars.byWord )
> {
> // ...
> }
You can make your own range, however look at this function first
(second example)
https://dlang.org/phobos/std_algorithm_iteration.html#.splitter
// 1) might need to cast your wchar[] to wstring first
// 2) also assumes that 'the word' is separated by whitespace
foreach( word; chars.splitter(' '))
{
}
or this one, which is a bit more smarter about what "the word"
means
https://dlang.org/phobos/std_array.html#.split
import std.array : split;
wchar[] str = cast(wchar[]) "some random stuff blah blah"w;
foreach(w; str.split())
{
writeln(w);
}
Anyway in both cases using dmd -vgc flag shows no GC allocations
done.
More information about the Digitalmars-d-learn
mailing list