Python-like slicing and handling UTF-8 strings as a bonus

Vladimir Panteleev vladimir at thecybershadow.net
Sat Dec 29 14:35:16 PST 2012


On Saturday, 29 December 2012 at 22:25:35 UTC, FG wrote:
> Slices are great but not really what I had expected, coming 
> from Python.
> I've seen code like s[a..$-b] used without checking the values, 
> just to end up with a Range violation. But there are 3 
> constraints to check here:
> 	a >= 0 && a + b <= s.length && b >= 0
>
> That's way too much coding for a simple program/script that 
> shortens a string, before it prints it on a screen. If I can't 
> write s[0..80] without fear, then let there at least be a 
> function that does it like Python would.

Why?

> Additionally, as strings are UTF-8-encoded, I'd like such a 
> function to give me proper substrings, without multibyte 
> characters cut in the middle, where s[0..80] would mean 80 
> characters on the screen and not 80 bytes.

This is a common fallacy when dealing with Unicode. Please see 
the linked and the following points:

http://utf8everywhere.org/#myth.utf32.o1


More information about the Digitalmars-d mailing list