Recommened way to iterate on a narrow string

Jonathan M Davis jmdavisProg at gmx.com
Sun Oct 14 01:19:50 PDT 2012


On Sunday, October 14, 2012 10:07:22 monarch_dodra wrote:
> I'm just wondering what the "recommended" and "most efficient"
> way to do that is?
> 
> front + popFront is suboptimal, because you have to evaluate the
> code point length twice.
> 
> Some sort of fancy "stride" or "decode" scheme?
> 
> Or can I just go ahead and use "foreach", knowing that it is both
> easy to use, and super efficient?

If you're just outright iterating over a string and operating on each element 
individually, then I see no reason not to use foreach. Just remember to make 
the variable dchar:

foreach(dchar c; str) {...}

I don't think that you're going to get more efficient than that unless what 
you're doing would work operating on code units instead of code points, but in 
that case, iterating probably wouldn't be what you'd want to do anyway. Where 
it gets more interesting is where you're doing things more complicated than 
simply iterating and operating on each code point individually. Then what the 
most efficient thing is depends on what you're doing and functions like stride 
and decode can come into play.

- Jonathan M Davis


More information about the Digitalmars-d mailing list