3 ms·
> Why would you want constant time access to code points? What can you do with constant time access to code points that is actually correct? Well, it makes it
by maxlybbert 11y ago
> Why would you want constant time access to code points? What can you do with constant time access to code points that is actually correct?
Well, it makes it easier to avoid putting an ellipsis in the middle of a multi-byte character, which could crash the user's phone when something else tries to display your now-invalid string.
- lambda 11y agoNote that my question included "...that is actually correct" as a condition. Truncating between code points to insert an ellipsis is not correct. Not all characters have the same width, even in a fixed-width font; for instance, combining characters have zero width. Truncating between a base character and a following combining character makes no sense. If you're truncating the word "exposé" to fit into six characters, it makes no sense to count that as 7 characters, truncate the accent, and wind up with "expose...". (Yes, normally that will be represented as a pre-composed character, but there are base-character/diacritic combinations that have no pre-composed character, so you will necessarily get a combining diacritic). Truncating based on character count can also mean that you exceed your width requirement; CJK characters are twice the width of most alphabetic characters. Additionally, most text is displayed using variable width fonts, where truncation should depend on the precise sum of the widths of characters, but even text that isn't can have the previous two issues. These mean that to do truncation properly, you need to iterate over the characters counting width until you exceed your required width in order to truncate, then truncate at the previous grapheme boundary, which requires walking through the string in order and can be done just as efficiently in a variable width encoding as in fixed.
- maxlybbert 11y ago> Note that my question included "...that is actually correct" as a condition. I think it's valid to pick an approach that makes it more likely I'll do things correctly.