mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-12-21 09:41:34 +03:00
50 lines
1.3 KiB
Rust
50 lines
1.3 KiB
Rust
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//! IO helpers.
|
|
|
|
use std::io::BufRead;
|
|
|
|
/// Read a line breaking in both \n and \r.
|
|
///
|
|
/// Adapted from <https://doc.rust-lang.org/std/io/trait.BufRead.html#method.read_line>.
|
|
pub fn read_line<R: BufRead + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> std::io::Result<usize> {
|
|
let mut read = 0;
|
|
loop {
|
|
let (done, used) = {
|
|
let available = match r.fill_buf() {
|
|
Ok(n) => n,
|
|
Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
|
|
Err(e) => return Err(e),
|
|
};
|
|
match memchr::memchr(b'\n', available) {
|
|
Some(i) => {
|
|
let end = i + 1;
|
|
buf.extend_from_slice(&available[..end]);
|
|
(true, end)
|
|
}
|
|
None => match memchr::memchr(b'\r', available) {
|
|
Some(i) => {
|
|
let end = i + 1;
|
|
buf.extend_from_slice(&available[..end]);
|
|
(true, end)
|
|
}
|
|
None => {
|
|
buf.extend_from_slice(available);
|
|
(false, available.len())
|
|
}
|
|
},
|
|
}
|
|
};
|
|
r.consume(used);
|
|
read += used;
|
|
if done || used == 0 {
|
|
if buf.ends_with(&[b'\n']) {
|
|
buf.pop();
|
|
}
|
|
return Ok(read);
|
|
}
|
|
}
|
|
}
|