Notice/Warning on narrowStrings .length

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Thu Apr 26 10:51:17 PDT 2012


"James Miller" <james at aatch.net> wrote in message 
news:qdgacdzxkhmhojqcettj at forum.dlang.org...
> I'm writing an introduction/tutorial to using strings in D, paying 
> particular attention to the complexities of UTF-8 and 16. I realised that 
> when you want the number of characters, you normally actually want to use 
> walkLength, not length. Is is reasonable for the compiler to pick this up 
> during semantic analysis and point out this situation?
>
> It's just a thought because a lot of the time, using length will get the 
> right answer, but for the wrong reasons, resulting in lurking bugs. You 
> can always cast to immutable(ubyte)[] or immutable(short)[] if you want to 
> work with the actual bytes anyway.

I find that most of the time I actually *do* want to use length. Don't know 
if that's common, though, or if it's just a reflection of my particular 
use-cases.

Also, keep in mind that (unless I'm mistaken) walkLength does *not* return 
the number of "characters" (ie, graphemes), but merely the number of code 
points - which is not the same thing (due to existence of the 
[confusingly-named] "combining characters").




More information about the Digitalmars-d mailing list