View mode: basic / threaded / horizontal-split · Log in · Help
October 14, 2012
Recommened way to iterate on a narrow string
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?
October 14, 2012
Re: Recommened way to iterate on a narrow string
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
Top | Discussion index | About this forum | D home