mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57:44 +00:00
Merge pull request #5510 from ceteece/expand-remove-crash-macro
expand: remove crash! macro
This commit is contained in:
commit
7202d01767
1 changed files with 99 additions and 90 deletions
|
@ -15,7 +15,7 @@ use std::str::from_utf8;
|
||||||
use unicode_width::UnicodeWidthChar;
|
use unicode_width::UnicodeWidthChar;
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
use uucore::error::{FromIo, UError, UResult};
|
use uucore::error::{FromIo, UError, UResult};
|
||||||
use uucore::{crash, format_usage, help_about, help_usage};
|
use uucore::{format_usage, help_about, help_usage};
|
||||||
|
|
||||||
const ABOUT: &str = help_about!("expand.md");
|
const ABOUT: &str = help_about!("expand.md");
|
||||||
const USAGE: &str = help_usage!("expand.md");
|
const USAGE: &str = help_usage!("expand.md");
|
||||||
|
@ -265,7 +265,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
|
|
||||||
let matches = uu_app().try_get_matches_from(expand_shortcuts(&args))?;
|
let matches = uu_app().try_get_matches_from(expand_shortcuts(&args))?;
|
||||||
|
|
||||||
expand(&Options::new(&matches)?).map_err_context(|| "failed to write output".to_string())
|
expand(&Options::new(&matches)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uu_app() -> Command {
|
pub fn uu_app() -> Command {
|
||||||
|
@ -308,16 +308,13 @@ pub fn uu_app() -> Command {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open(path: &str) -> BufReader<Box<dyn Read + 'static>> {
|
fn open(path: &str) -> UResult<BufReader<Box<dyn Read + 'static>>> {
|
||||||
let file_buf;
|
let file_buf;
|
||||||
if path == "-" {
|
if path == "-" {
|
||||||
BufReader::new(Box::new(stdin()) as Box<dyn Read>)
|
Ok(BufReader::new(Box::new(stdin()) as Box<dyn Read>))
|
||||||
} else {
|
} else {
|
||||||
file_buf = match File::open(path) {
|
file_buf = File::open(path).map_err_context(|| path.to_string())?;
|
||||||
Ok(a) => a,
|
Ok(BufReader::new(Box::new(file_buf) as Box<dyn Read>))
|
||||||
Err(e) => crash!(1, "{}: {}\n", path.maybe_quote(), e),
|
|
||||||
};
|
|
||||||
BufReader::new(Box::new(file_buf) as Box<dyn Read>)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -370,20 +367,14 @@ enum CharType {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::cognitive_complexity)]
|
#[allow(clippy::cognitive_complexity)]
|
||||||
fn expand(options: &Options) -> std::io::Result<()> {
|
fn expand_line(
|
||||||
|
buf: &mut Vec<u8>,
|
||||||
|
output: &mut BufWriter<std::io::Stdout>,
|
||||||
|
tabstops: &[usize],
|
||||||
|
options: &Options,
|
||||||
|
) -> std::io::Result<()> {
|
||||||
use self::CharType::*;
|
use self::CharType::*;
|
||||||
|
|
||||||
let mut output = BufWriter::new(stdout());
|
|
||||||
let ts = options.tabstops.as_ref();
|
|
||||||
let mut buf = Vec::new();
|
|
||||||
|
|
||||||
for file in &options.files {
|
|
||||||
let mut fh = open(file);
|
|
||||||
|
|
||||||
while match fh.read_until(b'\n', &mut buf) {
|
|
||||||
Ok(s) => s > 0,
|
|
||||||
Err(_) => buf.is_empty(),
|
|
||||||
} {
|
|
||||||
let mut col = 0;
|
let mut col = 0;
|
||||||
let mut byte = 0;
|
let mut byte = 0;
|
||||||
let mut init = true;
|
let mut init = true;
|
||||||
|
@ -425,7 +416,7 @@ fn expand(options: &Options) -> std::io::Result<()> {
|
||||||
match ctype {
|
match ctype {
|
||||||
Tab => {
|
Tab => {
|
||||||
// figure out how many spaces to the next tabstop
|
// figure out how many spaces to the next tabstop
|
||||||
let nts = next_tabstop(ts, col, &options.remaining_mode);
|
let nts = next_tabstop(tabstops, col, &options.remaining_mode);
|
||||||
col += nts;
|
col += nts;
|
||||||
|
|
||||||
// now dump out either spaces if we're expanding, or a literal tab if we're not
|
// now dump out either spaces if we're expanding, or a literal tab if we're not
|
||||||
|
@ -463,6 +454,24 @@ fn expand(options: &Options) -> std::io::Result<()> {
|
||||||
|
|
||||||
output.flush()?;
|
output.flush()?;
|
||||||
buf.truncate(0); // clear the buffer
|
buf.truncate(0); // clear the buffer
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expand(options: &Options) -> UResult<()> {
|
||||||
|
let mut output = BufWriter::new(stdout());
|
||||||
|
let ts = options.tabstops.as_ref();
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
|
||||||
|
for file in &options.files {
|
||||||
|
let mut fh = open(file)?;
|
||||||
|
|
||||||
|
while match fh.read_until(b'\n', &mut buf) {
|
||||||
|
Ok(s) => s > 0,
|
||||||
|
Err(_) => buf.is_empty(),
|
||||||
|
} {
|
||||||
|
expand_line(&mut buf, &mut output, ts, options)
|
||||||
|
.map_err_context(|| "failed to write output".to_string())?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue