I recognize your name as a rustacean, and since I'm someone who reluctantly had to add some unsafe code to extract a substring due to graphemes being unstable, I'm surprised to see you take shots at the Python model.
(Granted, I'm still learning, so maybe I can be as efficient without relying on unsafe, but still...)
I'm a Rust user; I'm not involved with its development. I had dinner with some of the main developers once; that's about it.
I'd recommend using this crate to work with grapheme clusters in Rust: https://crates.io/crates/unicode-segmentation
Re:Moving Away from Python 2
by steveklabnik on Saturday May 21, 2016 @10:24AM
(#11744772)
... Due to graphemes being unstable? What do you mean?
This could have been done differently. I would have been tempted to store strings as UTF-8 in Python 3, but hide this from the user. Most of the string functions, including regular expressions, don't really random access a string; they're sequential and forward only. They could work on UTF-8. The ones that return a string index should return an opaque type (say, "StringPos") which has a reference to the string and a position in it. The string functions should accept that type as an index.
If someone applies "int()" to a string index, or performs arithmetic on it, or indexes a string with an actual integer, then it's necessary to scan the entire UTF-8 string and create an index of where each Unicode character starts. This should be a rare event. Especially if there's support for adding or subtracting small integers from a StringPos by walking the string forwards or backwards, rather than building the full index.
Examples:
In practice, those expensive operations are rare.This would cut Python memory consumption way down when processing mostly-ASCII Unicode data. It would also make it more efficient to input and output UTF-8, which is usually what you want.