[phobos] UTF-8 string slicing

Sean Kelly sean at invisibleduck.org
Thu Aug 18 20:06:53 PDT 2011

On Aug 18, 2011, at 7:53 PM, Jonathan M Davis wrote:

> On Thursday, August 18, 2011 13:21:29 unDEFER wrote:
>> Hello!
>> D language specification says that it supports UTF-8 strings, but I can't
>> find how to slice UTF-8 string by character index, not by bytes numbers.
>> Why there is no simple slice function in std.utf like attached code?
>> Thank you in advance.
> Hmmm. Such a function isn't entirely a bad idea, but it also makes me a bit 
> nervous. Slicing is efficient. The slice function that you suggest is not. I 
> mean, it's efficient enough for what it's doing, but it's not O(1) like slicing 
> is, so having a slice function could be a bit misleading.

I need to do this from time to time, but I generally just do something like:

buf[0 .. buf.toUCSindex(n)]

A shorthand might be nice though, I suppose.

More information about the phobos mailing list