search for a given byte in the stream, and remain positioned on it
| 620 | |
| 621 | //! search for a given byte in the stream, and remain positioned on it |
| 622 | void FindByte(std::byte byte) |
| 623 | { |
| 624 | // For best performance, avoid mod operation within the loop. |
| 625 | size_t buf_offset{size_t(m_read_pos % uint64_t(vchBuf.size()))}; |
| 626 | while (true) { |
| 627 | if (m_read_pos == nSrcPos) { |
| 628 | // No more bytes available; read from the file into the buffer, |
| 629 | // setting nSrcPos to one beyond the end of the new data. |
| 630 | // Throws exception if end-of-file reached. |
| 631 | Fill(); |
| 632 | } |
| 633 | const size_t len{std::min<size_t>(vchBuf.size() - buf_offset, nSrcPos - m_read_pos)}; |
| 634 | const auto it_start{vchBuf.begin() + buf_offset}; |
| 635 | const auto it_find{std::find(it_start, it_start + len, byte)}; |
| 636 | const size_t inc{size_t(std::distance(it_start, it_find))}; |
| 637 | m_read_pos += inc; |
| 638 | if (inc < len) break; |
| 639 | buf_offset += inc; |
| 640 | if (buf_offset >= vchBuf.size()) buf_offset = 0; |
| 641 | } |
| 642 | } |
| 643 | }; |
| 644 | |
| 645 | /** |