2 releases

0.1.1 Nov 2, 2021
0.1.0 Nov 4, 2020

#972 in Text processing

Download history 188/week @ 2024-07-21 197/week @ 2024-07-28 339/week @ 2024-08-04 207/week @ 2024-08-11 313/week @ 2024-08-18 290/week @ 2024-08-25 320/week @ 2024-09-01 367/week @ 2024-09-08 242/week @ 2024-09-15 258/week @ 2024-09-22 187/week @ 2024-09-29 196/week @ 2024-10-06 298/week @ 2024-10-13 335/week @ 2024-10-20 260/week @ 2024-10-27 270/week @ 2024-11-03

1,175 downloads per month
Used in 3 crates

MIT license

15KB
209 lines

MIT Latest Version docs Chat on Miaou

BufRead's read_line may be a problem when you need performance and safety on unvetted streams:

You may wait forever or get an out of memory panic if there's no newline in the stream. And even if there's one, it may be way past what you need: you'll have to keep everything in memory just to get to the start of the following line.

CharReader is a buffered reader fixing those problems.

  • you can read lines without choking on an infinite stream without newlines
  • you can read lines and not store more than necessary if you just want the beginning
  • there's a next_char function to read only one char

It's suitable when you'd like to read UTF8 lines and aren't sure the data are kind enough.

When reading a line, you pass two parameters:

  • the max number of chars you want to get (rest of line will be dropped)
  • the max number of chars before giving out with an error (thus protecting against infinite streams)

All errors are io::Error:

  • UTF8 errors are of kind InvalidData
  • Lines exceeding your threshold are of kind Other

Alternative: If you know in advance how many lines you'd need and you always want whole lines, the standard take method of BufReader protects you against memory overflows.

No runtime deps