Why Strings as Classes?

Walter Bright newshound1 at digitalmars.com
Tue Aug 26 01:58:05 PDT 2008


Benji Smith wrote:
> But in this "systems language", it's a O(n) operation to get the nth 
> character from a string, to slice a string based on character offsets, 
> or to determine the number of characters in the string.
> 
> I'd gladly pay the price of a single interface vtable lookup to turn all 
> of those into O(1) operations.

I've written internationalized applications that dealt with multibyte 
utf strings. It looks like one would regularly need all those 
operations, but interestingly it just doesn't come up. It turns out that 
one needs to slice with the byte offset, or get the byte length, or get 
the nth byte. In the very rare case where one wants to do it with 
characters, one seems to already have the right offsets at hand.

If you choose to use dchar's instead, there is a 1:1 mapping between 
characters and indices, and it doesn't cost you any class overhead. It's 
also a simple conversion from UTF-8 <==> UCS-2. I can't think of a 
scenario where using classes would produce any performance advantage.




More information about the Digitalmars-d mailing list