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
3
u/ohkendruid May 24 '26
Some big name languages do--JavaScript and Java in particular--but it has turned out to be awful.
16 bits is still not enough to hold all Unicode characters, so UTF-16 sometimes needs 4 bytes instead of 2 to encode a character. At that point, you may as well use the more compact UTF-8.
It is true that if you need to look up character 15 in a string, you cannot find it in O(1) time, but most string processing goes through the whole string, anyway, so this isn't usually a big deal.
Also, using the same encoding as the standard external encoding can be convenient.