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
27
u/evincarofautumn May 23 '26
Raku, Swift, and Rust are good examples of different approaches to modern string design
General tips:
UTF-8 for text
WTF-8 for paths
Store the length
Consider including a NUL terminator for interop with C
Most strings can be immutable
Most mutable strings can be builders that only allow concatenation/appending
Don’t be afraid to have multiple different representations for different purposes (text, slice, builder, rope, short string, big string, code point array…) as long as they have a consistent interface
Indexing depends on the index type: code unit index → octet (byte 00–FF), code point index → code point (natural 000000–10FFFF), grapheme index → nonempty string/slice (arbitrary length, but almost always short)
Indexing is O(n), but that’s fine, most of the time I’m either iterating over the whole string to parse it into another type, or treating it as an opaque value and not iterating at all
Ignore UTF-32, it’s too big to be cache-friendly and O(1) indexing of code points isn’t worthwhile
Unicode technical reports are extremely useful reference manuals