[D-runtime] foreach (dchar c; s) is too slow

Jonathan M Davis jmdavisProg at gmx.com
Wed Oct 2 01:40:04 PDT 2013


On Wednesday, October 02, 2013 07:21:28 Jacob Carlborg wrote:
> On Oct 01, 2013, at 08:13 PM, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> > foreach(c; s)
> > 
> > iterates over the code unit type of the string, which is only dchar if
> > it's an array of dchar. You only get decoding and conversion to other
> > character types if you explicitly give a different character type.
> 
> I thought this was changed when it was decided that Phobos should treat
> strings as ranges of dchars.

No. Only Phobos treats strings as ranges of dchar. The language doesn't care 
one whit about ranges save for foreach, and foreach treats all strings the 
same as any other arrays. It doesn't use the range API for them. So, it's 
arguably good practice to always give the element type when using foreach on a 
string.

- Jonathan M Davis


More information about the D-runtime mailing list