r/ProgrammingLanguages • u/funcieq • May 23 '26
Discussion How to implement String?
Currently, String in my language is just value and length because it's a temporary solution, And as the language has developed, I am now able to rewrite a lot just for it, so I want to make a decent String in my language. So my question is, which String concept annoys you the least?
47
Upvotes
View all comments
Show parent comment
2
u/hgs3 May 24 '26
If you directly index a UTF-8 or UTF-16 code unit and treat it like a code point, then yes, that can result in bugs. Example: the code point U+1F60A is encoded with multiple code units in both UTF-8 and UTF-16. You can’t look at one code unit in isolation otherwise you won’t detect it. On the other hand, if you’re looking for a character in the Basic Latin block (i.e. ASCII) you can index by code unit because characters in that block are represented by a single code unit in both encodings. That’s the reason some parsers get away with code unit indexing: they’re exclusively looking for characters representable by a single code unit.