3 ms·
Dealing with situations like this where you can’t guarantee you read a complete decode sequence is exactly where memory mapped files are gold. Replacing read,
by ComputerGuru 9y ago
Dealing with situations like this where you can’t guarantee you read a complete decode sequence is exactly where memory mapped files are gold.
Replacing read, test sequence end, buffer, drop buffer if it exceeds certain length without sequence end but record sequence start, seek until end sequence is found, and process that mess in a safe, passably-efficient manner with memory mapped files made my cross-platform tac a breeze to write: https://neosmart.net/blog/2017/a-high-performance-cross-platform-tac-rewrite/ https://neosmart.net/blog/2017/a-high-performance-cross-plat...
- ori_b 9y agoThe simplest thing to do there is to issue a read if you have less than 4 bytes remaining to decode. This guarantees that you always have at least one well formed character, or an EOF coming up. Mmap doesn't work with pipes, which hugely reduces the utility of this kind of program.
- ComputerGuru 9y agoYou aren’t reading a byte at a time, it’s a block. You have no guarantee that you just ended on yet another partial sequence. It usually makes more sense to just rewind to the last complete sequence (though with streams that is not always an option).
- ori_b 9y ago> You have no guarantee that you just ended on yet another partial sequence Which is why you don't read to the end (until you have an EOF). You can just sidestep the partial sequence problem by getting more data before there's potential for a partial sequence to show up. The longest possible sequence is 4 bytes, so as long as you ensure you have 4 bytes available, partial sequences are impossible on well formed input.