mirror of
https://github.com/uutils/coreutils
synced 2024-07-21 18:04:45 +00:00
refactor/head ~ polish spelling (comments, names, and exceptions)
This commit is contained in:
parent
a481e8230b
commit
785343db7f
|
@ -1,3 +1,5 @@
|
|||
// spell-checker:ignore (vars) zlines
|
||||
|
||||
use clap::{App, Arg};
|
||||
use std::convert::TryFrom;
|
||||
use std::ffi::OsString;
|
||||
|
@ -210,7 +212,7 @@ impl Default for HeadOptions {
|
|||
}
|
||||
}
|
||||
|
||||
fn rbuf_n_bytes<R>(input: R, n: usize) -> std::io::Result<()>
|
||||
fn read_n_bytes<R>(input: R, n: usize) -> std::io::Result<()>
|
||||
where
|
||||
R: Read,
|
||||
{
|
||||
|
@ -226,7 +228,7 @@ where
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn rbuf_n_lines(input: &mut impl std::io::BufRead, n: usize, zero: bool) -> std::io::Result<()> {
|
||||
fn read_n_lines(input: &mut impl std::io::BufRead, n: usize, zero: bool) -> std::io::Result<()> {
|
||||
if n == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
|
@ -249,18 +251,18 @@ fn rbuf_n_lines(input: &mut impl std::io::BufRead, n: usize, zero: bool) -> std:
|
|||
})
|
||||
}
|
||||
|
||||
fn rbuf_but_last_n_bytes(input: &mut impl std::io::BufRead, n: usize) -> std::io::Result<()> {
|
||||
fn read_but_last_n_bytes(input: &mut impl std::io::BufRead, n: usize) -> std::io::Result<()> {
|
||||
if n == 0 {
|
||||
//prints everything
|
||||
return rbuf_n_bytes(input, std::usize::MAX);
|
||||
return read_n_bytes(input, std::usize::MAX);
|
||||
}
|
||||
let stdout = std::io::stdout();
|
||||
let mut stdout = stdout.lock();
|
||||
|
||||
let mut ringbuf = vec![0u8; n];
|
||||
let mut ring_buffer = vec![0u8; n];
|
||||
|
||||
// first we fill the ring buffer
|
||||
if let Err(e) = input.read_exact(&mut ringbuf) {
|
||||
if let Err(e) = input.read_exact(&mut ring_buffer) {
|
||||
if e.kind() == ErrorKind::UnexpectedEof {
|
||||
return Ok(());
|
||||
} else {
|
||||
|
@ -281,22 +283,22 @@ fn rbuf_but_last_n_bytes(input: &mut impl std::io::BufRead, n: usize) -> std::io
|
|||
if read == 0 {
|
||||
return Ok(());
|
||||
} else if read >= n {
|
||||
stdout.write_all(&ringbuf)?;
|
||||
stdout.write_all(&ring_buffer)?;
|
||||
stdout.write_all(&buffer[..read - n])?;
|
||||
for i in 0..n {
|
||||
ringbuf[i] = buffer[read - n + i];
|
||||
ring_buffer[i] = buffer[read - n + i];
|
||||
}
|
||||
} else {
|
||||
stdout.write_all(&ringbuf[..read])?;
|
||||
stdout.write_all(&ring_buffer[..read])?;
|
||||
for i in 0..n - read {
|
||||
ringbuf[i] = ringbuf[read + i];
|
||||
ring_buffer[i] = ring_buffer[read + i];
|
||||
}
|
||||
ringbuf[n - read..].copy_from_slice(&buffer[..read]);
|
||||
ring_buffer[n - read..].copy_from_slice(&buffer[..read]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn rbuf_but_last_n_lines(
|
||||
fn read_but_last_n_lines(
|
||||
input: impl std::io::BufRead,
|
||||
n: usize,
|
||||
zero: bool,
|
||||
|
@ -325,7 +327,7 @@ fn head_backwards_file(input: &mut std::fs::File, options: &HeadOptions) -> std:
|
|||
return Ok(());
|
||||
} else {
|
||||
input.seek(SeekFrom::Start(0))?;
|
||||
rbuf_n_bytes(
|
||||
read_n_bytes(
|
||||
&mut std::io::BufReader::with_capacity(BUF_SIZE, input),
|
||||
size - n,
|
||||
)?;
|
||||
|
@ -364,7 +366,7 @@ fn head_backwards_file(input: &mut std::fs::File, options: &HeadOptions) -> std:
|
|||
}
|
||||
};
|
||||
input.seek(SeekFrom::Start(0))?;
|
||||
rbuf_n_bytes(
|
||||
read_n_bytes(
|
||||
&mut std::io::BufReader::with_capacity(BUF_SIZE, input),
|
||||
size - found,
|
||||
)?;
|
||||
|
@ -379,9 +381,9 @@ fn head_file(input: &mut std::fs::File, options: &HeadOptions) -> std::io::Resul
|
|||
} else {
|
||||
match options.mode {
|
||||
Modes::Bytes(n) => {
|
||||
rbuf_n_bytes(&mut std::io::BufReader::with_capacity(BUF_SIZE, input), n)
|
||||
read_n_bytes(&mut std::io::BufReader::with_capacity(BUF_SIZE, input), n)
|
||||
}
|
||||
Modes::Lines(n) => rbuf_n_lines(
|
||||
Modes::Lines(n) => read_n_lines(
|
||||
&mut std::io::BufReader::with_capacity(BUF_SIZE, input),
|
||||
n,
|
||||
options.zeroed,
|
||||
|
@ -393,8 +395,8 @@ fn head_file(input: &mut std::fs::File, options: &HeadOptions) -> std::io::Resul
|
|||
fn uu_head(options: &HeadOptions) -> Result<(), u32> {
|
||||
let mut error_count = 0;
|
||||
let mut first = true;
|
||||
for fname in &options.files {
|
||||
let res = match fname.as_str() {
|
||||
for file in &options.files {
|
||||
let res = match file.as_str() {
|
||||
"-" => {
|
||||
if (options.files.len() > 1 && !options.quiet) || options.verbose {
|
||||
if !first {
|
||||
|
@ -407,16 +409,16 @@ fn uu_head(options: &HeadOptions) -> Result<(), u32> {
|
|||
match options.mode {
|
||||
Modes::Bytes(n) => {
|
||||
if options.all_but_last {
|
||||
rbuf_but_last_n_bytes(&mut stdin, n)
|
||||
read_but_last_n_bytes(&mut stdin, n)
|
||||
} else {
|
||||
rbuf_n_bytes(&mut stdin, n)
|
||||
read_n_bytes(&mut stdin, n)
|
||||
}
|
||||
}
|
||||
Modes::Lines(n) => {
|
||||
if options.all_but_last {
|
||||
rbuf_but_last_n_lines(&mut stdin, n, options.zeroed)
|
||||
read_but_last_n_lines(&mut stdin, n, options.zeroed)
|
||||
} else {
|
||||
rbuf_n_lines(&mut stdin, n, options.zeroed)
|
||||
read_n_lines(&mut stdin, n, options.zeroed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -451,10 +453,10 @@ fn uu_head(options: &HeadOptions) -> Result<(), u32> {
|
|||
}
|
||||
};
|
||||
if res.is_err() {
|
||||
let name = if fname.as_str() == "-" {
|
||||
let name = if file.as_str() == "-" {
|
||||
"standard input"
|
||||
} else {
|
||||
fname
|
||||
file
|
||||
};
|
||||
let prefix = format!("error reading {}", name);
|
||||
show_error_custom_description!(prefix, "Input/output error");
|
||||
|
@ -502,7 +504,7 @@ mod tests {
|
|||
}
|
||||
#[test]
|
||||
fn test_gnu_compatibility() {
|
||||
let args = options("-n 1 -c 1 -n 5 -c kiB -vqvqv").unwrap();
|
||||
let args = options("-n 1 -c 1 -n 5 -c kiB -vqvqv").unwrap(); // spell-checker:disable-line
|
||||
assert!(args.mode == Modes::Bytes(1024));
|
||||
assert!(args.verbose);
|
||||
assert_eq!(options("-5").unwrap().mode, Modes::Lines(5));
|
||||
|
@ -584,7 +586,7 @@ mod tests {
|
|||
);
|
||||
//test that the obsolete syntax is unrolled
|
||||
assert_eq!(
|
||||
arg_outputs("head -123qvqvqzc"),
|
||||
arg_outputs("head -123qvqvqzc"), // spell-checker:disable-line
|
||||
Ok("head -q -z -c 123".to_owned())
|
||||
);
|
||||
//test that bad obsoletes are an error
|
||||
|
@ -604,9 +606,9 @@ mod tests {
|
|||
);
|
||||
}
|
||||
#[test]
|
||||
fn rbuf_early_exit() {
|
||||
fn read_early_exit() {
|
||||
let mut empty = std::io::BufReader::new(std::io::Cursor::new(Vec::new()));
|
||||
assert!(rbuf_n_bytes(&mut empty, 0).is_ok());
|
||||
assert!(rbuf_n_lines(&mut empty, 0, false).is_ok());
|
||||
assert!(read_n_bytes(&mut empty, 0).is_ok());
|
||||
assert!(read_n_lines(&mut empty, 0, false).is_ok());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// spell-checker:ignore (vars) zline zlines
|
||||
|
||||
//! Iterate over zero-terminated lines.
|
||||
use std::io::BufRead;
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ pub enum ParseError {
|
|||
Overflow,
|
||||
}
|
||||
/// Parses obsolete syntax
|
||||
/// head -NUM[kmzv]
|
||||
/// head -NUM[kmzv] // spell-checker:disable-line
|
||||
pub fn parse_obsolete(src: &str) -> Option<Result<impl Iterator<Item = OsString>, ParseError>> {
|
||||
let mut chars = src.char_indices();
|
||||
if let Some((_, '-')) = chars.next() {
|
||||
|
@ -242,7 +242,7 @@ mod tests {
|
|||
assert_eq!(obsolete("-1mmk"), obsolete_result(&["-c", "1024"]));
|
||||
assert_eq!(obsolete("-1vz"), obsolete_result(&["-v", "-z", "-n", "1"]));
|
||||
assert_eq!(
|
||||
obsolete("-1vzqvq"),
|
||||
obsolete("-1vzqvq"), // spell-checker:disable-line
|
||||
obsolete_result(&["-q", "-z", "-n", "1"])
|
||||
);
|
||||
assert_eq!(obsolete("-1vzc"), obsolete_result(&["-v", "-z", "-c", "1"]));
|
||||
|
@ -257,7 +257,7 @@ mod tests {
|
|||
assert_eq!(obsolete("-5c5"), Some(Err(ParseError::Syntax)));
|
||||
}
|
||||
#[test]
|
||||
fn test_parse_obsolete_nomatch() {
|
||||
fn test_parse_obsolete_no_match() {
|
||||
assert_eq!(obsolete("-k"), None);
|
||||
assert_eq!(obsolete("asd"), None);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue