4 ms·
It's impossible to get corrupted strings if you use wide characters. This is better than hard not to.
by kazinator 10y ago
It's impossible to get corrupted strings if you use wide characters. This is better than hard not to.
- Avernar 10y agoNot impossible. If you slice in the middle of a grapheme then you get a corrupted string as well. You'll get an alternate glyph instead of a square box but it's still corrupted.
- paulddraper 10y agoIt's not a corrupted string. You may have mangled it in a way that doesn't preserve all semantics, but no one will crash with an encoding issue. A "string" means "a sequence of characters". Wide characters (or the equivalent interface) preserve this property. Graphemes operate at a higher level than characters. You could construct a grapheme-strings, I suppose, but that has tons of edge cases, and if you don't like character-strings, I doubt you will like grapheme-strings.
- kazinator 10y agoOr, if by "string" we mean "sequence of code points" (rather than graphemes) then it doesn't get corrupted by any chopping or rearrangement which only permutes the code points. If we chop UTF-8, we can end up with bad characters, or possibly invalid overlong forms.
- Avernar 10y agoI consider changing a glyph to some other glyph(s) as corruption. Take an emoji flag character as an example. Split it between the code points and you end up with two boxed letters. If you chop in the middel of a code unit then you end up with U-FFFDs. In both cases the visual representation has been altered. As I wrote elsewhere it is easy for the slice routines of a language to check to see if the programmer tried to slice in the middle of a code unit and either return an error or just advance to the start of a code unit.
- kazinator 10y agoUTF-8 slicing destroys characters and graphemes. Slicing a code-point-character string destroys only graphemes. Clear win. A code point string has other niceties, like being indexed by simple integers. If end is the index of the last code point of a grapheme, then the next grapheme starts at end + 1. If end is the index of the last UTF-8 encoding of a code point, then the next grapheme does not start at end + 1. We can have it so that it does by making end point to the last byte of the UTF-8 encoding of the code point; but then it doesn't point at the start of the character, recovering which is awkward. The code uglification can be addressed by piling on abstractions: integer-like iteration gizmos that can be incremented and decremented thanks to function or operator overloading. I feel that that level of abstraction has no place in character-level data processing, if anywhere, whose basic operations should be expressible tersely in a few machine instructions. Also, we mustn't lose sight of what the T means in UTF-8: transfer. It's not called UPF-8 (the Unicode processing format in 8 bits). Working with UTF-8 instead of with the objects that UTF-8 denotes is like working with a textual representation of Lisp s-expressions that still contain the parentheses and whitespace delimitation, and quotes around strings and so on, refusing to parse them to obtain the object which they represent. People who do this should immediately turn in their CS degrees. All those other issues you refer to are addressed by more parsing. If you want the glyphs, the correct thing is to parse the code-point string and make a list or vector of glyph representations. With that representation you can still break the text "carpet" into "car" "pet" which destroys semantics; that is dealt with by parsing into words. Chopping lists of words destroys phrases; so parse phrases, and transform at the phrase level. And so on.
- Avernar 10y ago> Clear win. I'd call it a slight improvement. And after the major step back of using 2 to 4 times more memory for strings I'd call it a net loss. > A code point string has other niceties, like being indexed by simple integers. Again, no benefit of this. The only argument I've heard here is to prevent bad slicing and I've shown a way to prevent that. > Also, we mustn't lose sight of what the T means in UTF-8: transfer. It's not called UPF-8 (the Unicode processing format in 8 bits). By this argument we can't use UTF-16 or UTF-32 for internal processing of strings either. Back to code pages then.
- Avernar 10y agoWhy should it crash? The proper procedure when validating a UTF-8 string is to replace errors with U+FFFD. The term character has many meanings. Graphemes are characters and that's what most users expect, something that's displayed as a single graphical unit.
- paulddraper 10y agoI use "character" in the same way that the Unicode Consortium uses the word. Though "code point" would be more precise.
- Avernar 10y agoThat's what they were hoping for. Didn't tuen out thst way. From icu-project.org: "As with glyphs, there is no one-to-one relationship between characters and code points. What an end-user thinks of as a single character (grapheme) may in fact be represented by multiple code points; conversely, a single code point may correspond to multiple characters."
- imron 10y agoWhich wide characters are you talking about? Because on Windows, where wide characters are 16 bits, it's quite possible to get corrupted strings (and in fact quite a few well-known programs, written by quite well-known software companies, make this exact mistake). All you need to do is index/slice a string half-way through any character that is outside Unicode's Basic Multilingual Plane
- Avernar 10y agoThis drives me crazy. The Win32 API was designed for UCS-2. Then UTF-16 came out and the API was shoehorned to use it but as you said they still haven't caught all the places where it still thinks it's UCS-2.
- paulddraper 10y ago"wide enough" So if it's Unicode, each element is a Unicode character.
- imron 10y agoFirst of all, Unicode doesn't define characters it defines codepoints. I get that this might seem pedantic, but it's important to be pedantic about this, otherwise misconceptions and ambiguities occur e.g. 'just use wide characters' - the definition of which changes depending on the platform. Second of all, "wide enough" for all intents and purposes means 32 bits. Technically Unicode only needs 21 bits to cover the currently defined codespace, but computers don't deal well with that and so 32bits is the minimum "wide enough" character size. This creates a lot of wasted space and memory, not to mention pushes medium length strings across cache line boundaries for very little benefit - the ability to directly index/slice strings without accidentally corrupting data. Now obviously you want to avoid accidentally corrupting data, the tradeoff comes down to whether you need direct, arbitrary indexing, or if it's worth doing some processing to determine the correct place to split in order to make space gains. The technical world has come down overwhelmingly in favour of the latter, and that's why you see hardly anyone using utf-32. It's simply not as good a solution for most real world concerns.