D's confusing strings (was Re: D on hackernews)

Jonathan M Davis jmdavisProg at gmx.com
Wed Sep 21 09:11:28 PDT 2011


On Wednesday, September 21, 2011 15:16:33 Christophe wrote:
> Timon Gehr , dans le message (digitalmars.D:144889), a écrit :
> > unicode natively. Yet the 'D strings are strange and confusing' argument
> > comes up quite often on the web.
> 
> Well, I think they are. The ptr+length stuff is amasing, but the
> behavior of strings in phobos is weird.
> 
> mini-quiz: what should std.range.drop(some_string, 1) do ?
> hint: what it actually does is not what the documentation of phobos
> suggests*...

What do you mean? It does exactly what it says that it does.

> Strings are array of char, but they appear like a lazy range of dchar to
> phobos. I could cope with the fact that this is a little unexpected for
> beginners. But well, that creates a lot of exceptions in phobos, like
> the fact that you can't even copy a char[] to a char[] with
> std.algorithm.copy. And I don't mention all the optimization that are
> not/cannot be performed for those strings. I'll just remember to use
> ubyte[] wherever I can...

Yeah, well, as long as char is a unicode code unit, that's the way that it 
goes. In general, Phobos does a good job of using slicing and other 
optimizations on strings when it can in spite of the fact that they're not 
sliceable ranges, but there are cases where the fact that you _have_ to 
process them to be able to find the nth code point means that you just can't 
process them as efficiently as your typical array. That's life with a variable-
length encoding - and that includes std.algorithm.copy. But that's an easy one 
to get around, since if you wanted to ignore unicode safety and just copy some 
chunk of the string, you can always just slice it directly with no need for 
copy.

> * Please, someone just adds in the documentation of IsSliceable that
> narrow strings are an exception, like it was recently added to
> hasLength.

A good point.

- Jonathan M Davis


More information about the Digitalmars-d mailing list