mirror of
https://github.com/uutils/coreutils.git
synced 2025-12-23 08:47:37 +00:00
refactor: optimize stty save format parsing by checking for ':' first
Changed the parsing logic in the stty function to only attempt parsing saved state when the first argument contains ':', improving efficiency and avoiding unnecessary parse calls for regular settings. This refactors the code for clarity while maintaining equivalent functionality.
This commit is contained in:
parent
e08f02f337
commit
d3984f77f0
1 changed files with 14 additions and 11 deletions
|
|
@ -288,18 +288,21 @@ fn stty(opts: &Options) -> UResult<()> {
|
|||
|
||||
if let Some(args) = &opts.settings {
|
||||
if let Some((first, rest)) = args.split_first() {
|
||||
let base = get_base_termios(opts.file.as_fd())?;
|
||||
let mut restored = base.clone();
|
||||
match parse_save_format(first, &mut restored) {
|
||||
Ok(()) => {
|
||||
restored_from_save = Some(restored);
|
||||
settings_iter = Box::new(rest.iter().copied());
|
||||
}
|
||||
// GNU stty errors immediately when the first argument looks like save format but cannot be parsed
|
||||
Err(e) if first.contains(':') => return Err(e),
|
||||
Err(_) => {
|
||||
settings_iter = Box::new(args.iter().map(|s| &**s));
|
||||
if first.contains(':') {
|
||||
// Only attempt to parse saved state when the first arg actually looks like one.
|
||||
let base = get_base_termios(opts.file.as_fd())?;
|
||||
let mut restored = base.clone();
|
||||
match parse_save_format(first, &mut restored) {
|
||||
Ok(()) => {
|
||||
restored_from_save = Some(restored);
|
||||
settings_iter = Box::new(rest.iter().copied());
|
||||
}
|
||||
// GNU stty errors immediately when the first argument looks like save format but cannot be parsed
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
} else {
|
||||
// First argument is not a saved state; treat the whole list as regular settings.
|
||||
settings_iter = Box::new(args.iter().map(|s| &**s));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue