www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Iterating chars by Word

reply =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
Is:
wchar[] chars;  // like a: "import 
core.sys.windows.windows;\nimport std.conv      : to;\n"

Goal:
foreach ( word; chars.byWord )
{
     // ...
}

Iterating chars by Word...
How to ? ( simple, fast, low memory, beauty, perfect )
Nov 12 2020
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/12/20 9:14 PM, =D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9 =D0=A4=D0=
=B0=D0=B4=D0=B5=D0=B5=D0=B2 wrote:
 Is:
 wchar[] chars;=C2=A0 // like a: "import core.sys.windows.windows;\nimpo=
rt=20
 std.conv=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 : to;\n"
=20
 Goal:
 foreach ( word; chars.byWord )
 {
  =C2=A0=C2=A0=C2=A0 // ...
 }
=20
 Iterating chars by Word...
 How to ? ( simple, fast, low memory, beauty, perfect )
import std.stdio; import std.algorithm; import std.uni; void main() { auto s =3D "=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9 abc=C3=A7defg=C4= =9Fh=C4=B1 =D0=A4=D0=B0=D0=B4=D0=B5=D0=B5=D0=B2"w; auto words =3D s.splitter; words.writefln!"%-(%s\n%)"; } Note that splitter() is different from splitter!isWhite. The version I=20 used above removes empty parts. (I used multiple spaces in one place but = the output contains only three parts.) =D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9 abc=C3=A7defg=C4=9Fh=C4=B1 =D0=A4=D0=B0=D0=B4=D0=B5=D0=B5=D0=B2 Ali
Nov 12 2020
prev sibling parent reply evilrat <evilrat666 gmail.com> writes:
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.
Nov 12 2020
parent reply =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
On Friday, 13 November 2020 at 06:42:24 UTC, evilrat wrote:
 On Friday, 13 November 2020 at 05:14:08 UTC, Виталий Фадеев 
 wrote:
 [...]
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.
Thanks, Ali. Thanks, Evilrat. I taste it now: https://run.dlang.io/is/HlSFVY
Nov 12 2020
parent reply =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
On Friday, 13 November 2020 at 06:52:38 UTC, Виталий Фадеев wrote:
 On Friday, 13 November 2020 at 06:42:24 UTC, evilrat wrote:
 [...]
Thanks, Ali. Thanks, Evilrat. I taste it now: https://run.dlang.io/is/HlSFVY
Latest: https://run.dlang.io/is/dfrcYj
Nov 12 2020
parent =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
On Friday, 13 November 2020 at 07:23:13 UTC, Виталий Фадеев wrote:
 On Friday, 13 November 2020 at 06:52:38 UTC, Виталий Фадеев 
 wrote:
 On Friday, 13 November 2020 at 06:42:24 UTC, evilrat wrote:
 [...]
Thanks, Ali. Thanks, Evilrat. I taste it now: https://run.dlang.io/is/HlSFVY
Latest: https://run.dlang.io/is/riY5BI
Splitted on each White space... Thanks. I going to the Lexers/Parsers world.
Nov 12 2020