2 ms·
I think the Swift String API has a lot going for it conceptually. But I have a big problem with knowing so little about the performance and memory usage charac
by fauigerzigerk 11y ago
I think the Swift String API has a lot going for it conceptually.
But I have a big problem with knowing so little about the performance and memory usage characteristics of the functions I'm calling and the strings I'm keeping in data structures.
The problem is exacerbated by the fact that the API is extremely incomplete. A ton of things can only be done by resorting to NSString functions and that, I think, requires the String being copied into a UTF-16 representation underneath. Or does it? I don't know. That's exactly the problem.
The idea of putting grapheme clusters at the center of the string universe is great when it comes to text that users see and manipulate. It's excellent for writing word processors.
But it is not convenient for analysing large amounts of text data or for parsing semi structured text formats where we have a lot of fixed length stuff that could be conveniently accessed using numeric slice indexes.
I think Swift's String class would work very well as a view on top of a UTF-8 buffer.
- mikeash 11y agoIs a String class the right type to use for parsing text formats with lots of fixed length stuff? Maybe Array<UInt8> would be more suitable.
- fauigerzigerk 11y agoThere's a lot of middle ground between parsing a purely structured format and UI oriented string manipulation. Most of what I have done in my life involved semi-structured data with both natural language text and structured parts with fixed lengths. It's extremely inconvenient to have no string functions available when working with byte arrays. I actually like the C approach a lot in principle, because it doesn't make you choose one or the other.
- mikeash 11y agoMaybe it would be best to have a lot of these things we think of as "string" functions available on arrays, like sub-sequence matching, replacement, trimming, even something like regex. Then strings can be "all that, plus unicode."
- fauigerzigerk 11y agoYes, I think that's a good idea.