4 ms·
> Not just because you can't jump N bytes into a string to get a character offset I keep hearing people refer to this operation (mostly in HN threads), I have
by blattimwind 7y ago
> Not just because you can't jump N bytes into a string to get a character offset
I keep hearing people refer to this operation (mostly in HN threads), I have yet to see an appropriate use of it. So far the only uses I've seen were (for Unicode) misguided attempts at character iteration, which doesn't need this at all.
- magicalhippo 7y agoAt work we have to parse various different fixed-width column files. We only need data from a handful of the columns, so we use the substring function to extract the data we need. Of course these files are so old they predate proper encoding and are implicitly Windows-1252 or similar.
- ben509 7y agoThat's been my experience, I've done it but never anything that a scanning or tokenizing algorithm couldn't do. So being able to iterate over characters is important, but constant time access to a specific character seems very rare. I think the only case where I've wanted a specific character was when I wrote games that loaded maps from text files and those were ASCII regardless.
- magicalhippo 7y agoSome of the records are over 1k chars wide, and we need stuff near the end. Though if one had to decode the line from UTF-8, you're iterating anyway so...
- ben509 7y agoI could see a case for it if you were finding line endings in bytes mode, and then casting a buffer to a UTF-8 string. Then you'd benefit from a reverse iterator, which UTF-8 supports just fine. I imagine the main reason people would use indexing is to scan through strings in lockstep, e.g. to implement comparison, but iterators can cover that case as well, especially if you can copy them.