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

FG home at fgda.pl
Sat Dec 29 14:55:58 PST 2012


On 2012-12-29 23:35, Vladimir Panteleev wrote:
> 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?

Probably because I like concise code. I always prefer:
     if (A) print(getMessage().getSlice(0..100));

to writing something like this:
     auto message = getMessage();
     if (A) print(message.length > 100 ? message[0..100] : message);


>> 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

True. I didn't think about all the languages out there.
Just some common European ones.


More information about the Digitalmars-d mailing list