4 ms·
You might think that, but Java had the same problem with substring and decided that it WAS actually better than the alternative of sharing the original buffer.
by haimez 8y ago
You might think that, but Java had the same problem with substring and decided that it WAS actually better than the alternative of sharing the original buffer. I might be wrong, but I think Java 6 or 7 changed the behavior to always copy after significant analysis of the trade off space. Pathological cases do occur and often enough to render a process entirely dead.
- Animats 8y agoGo has functions like Index(s) and LastIndex(s), which return a byte index into a UTF-8 string. These just replicate similar C library functions. The C library is perhaps not the place to look for good string semantics. Go could have returned a slice covering the rest of the string instead. Or maybe slices of "everything up to the match", "the matching part", and "everything after the match". That would avoid dealing with direct indices into a UTF-8 byte string. If the inputs are valid UTF-8, the outputs will be valid UTF-8. With indices, it's so easy to mess it up. Or worse, create a string parsing vunerability.