3 ms·
Why couldn't the string implementation simply provide a string offset class? Internally, it could be represented as a byte offset but at the point of use it wou
by TheCoelacanth 11y ago
Why couldn't the string implementation simply provide a string offset class? Internally, it could be represented as a byte offset but at the point of use it wouldn't have to know that.
- lisper 11y agoYes, this is the Right Thing. But don't forget the question I was responding to: > Why would you want constant time access to code points? The answer is: because you might want to compute an offset into a string in one place in your code, and then pass that offset to another place in your code which then accesses the string at that offset in constant time. My point is that to make this work you must have string offset be an abstract class, not an integer.
- TheCoelacanth 11y agoIt only has to be an abstract class if you want to prevent the existence of invalid offsets, but the same is true if you have constant width code points. For instance, INT_MAX is not a valid offset into almost any string.
- lisper 11y ago> It only has to be an abstract class if you want to prevent the existence of invalid offsets Wouldn't you want that? I would. > the same is true if you have constant width code points True, but the validity check for the constant-width case can be done in constant time.
- TheCoelacanth 11y ago> True, but the validity check for the constant-width case can be done in constant time. The same is true of UTF-8 because it is self-synchronizing. You only have to check that the byte at the given offset is either 0xxxxxxx or 11xxxxxx.
- lisper 11y agoHm, that's a good point.