[phobos] UTF-8 string slicing

Jonathan M Davis jmdavisProg at gmx.com
Fri Aug 19 13:22:38 PDT 2011


On Friday, August 19, 2011 11:42 Masahiro Nakagawa wrote:
> On Sat, 20 Aug 2011 02:24:41 +0900, unDEFER <undefer at gmail.com> wrote:
> 
> [snip]
> 
> > The fact which the next code
> > ----
> > writeln( arr.length );
> > arr.popFront();
> > writeln( arr.length );
> > ----
> > prints 9 after 10 for any array but for UTF-8 and UTF-16 strings may
> > print as well 8 or lesser, seems too confusing for me.
> 
> You can use std.algorithm.count to count the number of elements.
> 
> assert([1,2,3].count() == 3);
> assert("abc".count() == 3);
> assert("あいう".count() == 3);

The correct function for getting the number of elements for a range is 
std.ronge.walkLength. count will call its predicate (which defaults to "true") 
on every member of the range. walkLength, on the other hand, will call the 
range's length property if it has one (string and wstring don't have a length 
property as far as ranges are concerned, because they're ranges of dchar, not 
char or wchar) and simply iterates through the range, counting its elements 
otherwise. So, it will be more efficient to call walkLength, and that's what 
it's for. count is for counting the number of elements in the range which 
match its predicate, not for counting the number of elements in the range.

- Jonathan M Davis


More information about the phobos mailing list