Re:Thoughts on Rust, a few thousand lines in
by moosingin3space on Wednesday January 09, 2019 @05:27PM
(#18869640)
In Rust, you're supposed to use `unicode-segmentation`[1] if you need to split on logical character (grapheme cluster in the Unicode standard). Otherwise, the iterators `.bytes` emits raw bytes, and `.chars` emits UTF-8 codepoints.
Basically, string indexing is a lot harder than it seems at first glance, depending on what you want.
Re:Thoughts on Rust, a few thousand lines in
by estebank on Wednesday January 09, 2019 @07:43PM
(#18870538)
One nitpick: `.chars`[1] gives you an iterable[2] of `char`s[3], each of which is always a 4 byte representation of a valid unicode character. This means that `"asdf".chars().collect()` will have a different size to `"asdf"` and `"asdf".chars().as_str()`. `.chars()` will never give you an incomplete codepoint, but it will give you incomplete characters, as you could have many c̶̼̟̏ó̷̘̉n̴̖̞̏̇t̸̡̃ĭ̸̻̬n̴̯͉̂͑ṵ̴̑a̷̛̫̳ẗ̸͕́i̷̱̫̓̋ǫ̸̑ǹ̶̼̅s̸̩̾̌ to represent what visually are a single char.
[1]: https://doc.rust-lang.org/std/string/struct.String.html#meth...
Re:Thoughts on Rust, a few thousand lines in
by moosingin3space on Wednesday January 09, 2019 @07:58PM
(#18870610)
> visually are a single char
IIRC that's what grapheme clusters are for.
Re:Thoughts on Rust, a few thousand lines in
by Skunkleton on Wednesday January 09, 2019 @05:13PM
(#18869518)
UTF-8 is at odds with efficient array indexing. I like pythons approach where bytes and strings are distinct types, though I have no idea what it is doing under the hood.
Re:Thoughts on Rust, a few thousand lines in
by int_19h on Wednesday January 09, 2019 @07:33PM
(#18870492)
Modern Python uses whatever representation is sufficient to ensure 1-unit-per-codepoint for a given string (which it can do on creation, since strings are immutable). So you get ASCII, UTF-16 sans surrogate pairs, or UTF-32.
This is great for high-level code, but painful to work with from native code, because it usually needs some specific encoding to call into other libraries, and it's usually UTF-8 - so you need to re-encode all the time.
Re:Thoughts on Rust, a few thousand lines in
by colatkinson on Wednesday January 09, 2019 @07:04PM
(#18870316)
I actually had to work with Python strings at the C level recently, and their approach is pretty clever. IIRC, the runtime can take any common form of Unicode, and will store it. When you access that string, the accessor requests a specific encoding, and the runtime will convert if need be, and then store it in the string object.
So it handles the (very) common case of needing the same encoding multiple times (e.g. for all file paths on Windows), while not introducing too much overhead in memory or speed.
I could be mistaken on exact details though, especially since I recall there being multiple implementations even within py3.x.
Re:Thoughts on Rust, a few thousand lines in
by Skunkleton on Thursday January 10, 2019 @08:48PM
(#18880337)
Any idea how it handles indexing? Does it convert everything to 32 bit chars and ignore graphemes?
Re:Thoughts on Rust, a few thousand lines in
by hu3 on Wednesday January 09, 2019 @06:42PM
(#18870183)
Go allows slicing UTF8 strings just fine: https://play.golang.org/p/eUQ5L58KwZy
https://play.golang.org/p/CkBp0w8T621