3 ms·
> Working through the string is then accomplished by repeated calls to: int utf8byte(struct utf8cursor *u8c); > This function will return the next byte in t
by saagarjha 8y ago
> Working through the string is then accomplished by repeated calls to:
int utf8byte(struct utf8cursor *u8c);
> This function will return the next byte in the normalized (and possibly case-folded) string, or zero at the end. UTF-8-encoded code points can take more than one byte, of course, so individual bytes do not, on their own, represent code points.
What's the choice behind going byte-by-byte rather than grapheme-by-grapheme? Seems like an API designed for comparisons and nothing else…
- gumby 8y ago> Seems like an API designed for comparisons and nothing else… There's your answer: that's all the kernel needs to do with strings.
- dbaupp 8y agoThere's no fixed-width/non-allocating way to return a grapheme (unless one can return a slice into the original data, which cannot happen here), because they may consist of arbitrarily many codepoints (and code units) both in theory and in practice. For instance, many non-Latin scripts compose single graphemes out of multiple code points and many emoji are multiple codepoints (e.g. the families[0], and the skin tone variants). https://manishearth.github.io/blog/2017/01/15/breaking-our-latin-1-assumptions/ https://manishearth.github.io/blog/2017/01/15/breaking-our-l... One option would to instead return (24-bit) code points, which would amortize comparison and iteration costs slightly, but may accidentally encourage people to forget about multiple-codepoint characters. [0]: https://r12a.github.io/uniview/?charlist=%F0%9F%91%A9%E2%80%8D%F0%9F%91%A9%E2%80%8D%F0%9F%91%A7%E2%80%8D%F0%9F%91%A6 https://r12a.github.io/uniview/?charlist=%F0%9F%91%A9%E2%80%...
- saagarjha 8y ago> There's no fixed-width/non-allocating way to return a grapheme (unless one can return a slice into the original data, which cannot happen here) I take it's not possible to return a pointer into the data because normalization is being performed?
- dbaupp 8y agoYeah, normalising and case folding will both potentially result in a new sequence of graphemes (and code points and code units), so slicing can't work.
- chrisseaton 8y agoSeems like a primitive you could build a grapheme-by-grapheme abstraction on top of?