// 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 . pub fn read_line(r: &mut R, buf: &mut Vec) -> std::io::Result { 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); } } }