且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

逐行读取文件(不是 utf-8)?

更新时间:2023-11-14 11:39:40

您不必重新实现 BufReader 本身,它为您的用例提供了所需的方法 read_until:

You do not have to re-implement BufReader itself, it provides exactly the method you need for your usecase read_until:

fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize>

您提供自己的Vec,文件的内容将被附加,直到遇到byte(0x0A 为LF).

You supply your own Vec<u8> and the content of the file will be appended until byte is encountered (0x0A being LF).

有几个潜在的问题:

  • 缓冲区不仅可以以 LF 字节结束,还可以以 CR LF 序列结束,
  • 在后续调用之间清除 buf 由您决定.
  • the buffer may end not only with a LF byte, but with a CR LF sequence,
  • it is up to you to clear buf between subsequent calls.

一个简单的while let Ok(_) = reader.read_until(0x0A as u8, buffer) 应该让你很容易阅读你的文件.

A simple while let Ok(_) = reader.read_until(0x0A as u8, buffer) should let you read your file easily enough.

你可以考虑实现一个 std::io::Lines 等价物,它从编码转换为 UTF-8 以提供一个很好的 API,尽管它会降低性能.

You may consider implement a std::io::Lines equivalent which converts from the encoding to UTF-8 to provide a nice API, though it will have a performance cost.