4 ms·
> I think it’s good that Rust sort of forces you to recognize the complexity in this. It's not bad, but Swift still does better: * String.Index is an opaque t
by masklinn 7y ago
> I think it’s good that Rust sort of forces you to recognize the complexity in this.
It's not bad, but Swift still does better:
* String.Index is an opaque type (ideally it would be linked to a specific string too)
* it returns grapheme cluster counts by default, that's probably the least surprising / useless though not necessarily super useful either
* other lengths go through explicit lazy views e.g. string.utf8.count will provide the UTF8 code units count, string.unicodeScalars.count will provide the USVs count
> If you switch to s.chars().count(), you’ll get UTF-8 scalar values
You get unicode scalar values (which I assume is what you meant).
- theobeers 7y agoGood catch! I was still able to edit the comment.
- deleted 7y ago[deleted]
- gok 7y agoThe big problem with extended grapheme clusters by default is that it changes behavior completely as Unicode spec changes. So Swift strings are nearly guaranteed to change their default behavior between versions.
- Manishearth 7y agoI think what swift does is good for its design space. I definitely would not like it if Rust did the same thing, because you need fast ways to store and reuse indices for parsing and other things. Swift is primarily geared towards UI, and in that case using EGCs is the right design choice.
- cryptonector 7y agoTFA makes a very good case that Swift got it wrong. Perhaps string length should be more hidden though, or parametrized with a unit to count. Perhaps it should be let nu = s._smelly_length(UTF8CodeUnits); let nc = s._smelly_length(ExtendedGraphemeClusters); Stop using "zero length" to denote empty string, just have an empty string method.
- favorited 7y agoNot sure how you read "Swift’s approach to string length isn’t unambiguously the best one" and interpret it as "Swift got it wrong." The author's substantive criticism of Swift's String/Character/etc. types seems to be complications arising from the dependency on ICU (such as not necessarily being able to persist indices across icu4c versions).