Skip to content

Commit 4ab95fd

Browse files
committed
address review comments
1 parent 6c0116c commit 4ab95fd

File tree

1 file changed

+41
-89
lines changed

1 file changed

+41
-89
lines changed

src/uu/unexpand/src/unexpand.rs

Lines changed: 41 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,10 @@ enum ParseError {
3535

3636
impl UError for ParseError {}
3737

38-
fn parse_increment_syntax(word: &str, nums: &[usize]) -> Result<usize, ParseError> {
39-
if nums.is_empty() {
40-
return Err(ParseError::InvalidCharacter("+".to_string()));
41-
}
42-
match word.parse::<usize>() {
43-
Ok(num) => {
44-
if num == 0 {
45-
return Err(ParseError::TabSizeCannotBeZero);
46-
}
47-
Ok(num)
48-
}
49-
Err(e) => match e.kind() {
50-
IntErrorKind::PosOverflow => Err(ParseError::TabSizeTooLarge),
51-
_ => Err(ParseError::InvalidCharacter(
52-
word.trim_start_matches(char::is_numeric).to_string(),
53-
)),
54-
},
55-
}
56-
}
57-
58-
fn parse_extend_syntax(word: &str, nums: &[usize]) -> Result<usize, ParseError> {
59-
if nums.is_empty() {
60-
return Err(ParseError::InvalidCharacter("/".to_string()));
61-
}
38+
fn parse_tab_num(word: &str) -> Result<usize, ParseError> {
6239
match word.parse::<usize>() {
63-
Ok(num) => {
64-
if num == 0 {
65-
return Err(ParseError::TabSizeCannotBeZero);
66-
}
67-
Ok(num)
68-
}
40+
Ok(0) => Err(ParseError::TabSizeCannotBeZero),
41+
Ok(num) => Ok(num),
6942
Err(e) => match e.kind() {
7043
IntErrorKind::PosOverflow => Err(ParseError::TabSizeTooLarge),
7144
_ => Err(ParseError::InvalidCharacter(
@@ -75,7 +48,7 @@ fn parse_extend_syntax(word: &str, nums: &[usize]) -> Result<usize, ParseError>
7548
}
7649
}
7750

78-
fn tabstops_parse(s: &str) -> Result<TabConfig, ParseError> {
51+
fn parse_tabstops(s: &str) -> Result<TabConfig, ParseError> {
7952
let words = s.split(',');
8053

8154
let mut nums = Vec::new();
@@ -93,29 +66,25 @@ fn tabstops_parse(s: &str) -> Result<TabConfig, ParseError> {
9366
if increment_size.is_some() || extend_size.is_some() {
9467
return Err(ParseError::InvalidCharacter("+".to_string()));
9568
}
96-
increment_size = Some(parse_increment_syntax(word, &nums)?);
69+
if nums.is_empty() {
70+
return Err(ParseError::InvalidCharacter("+".to_string()));
71+
}
72+
increment_size = Some(parse_tab_num(word)?);
9773
} else if let Some(word) = word.strip_prefix('/') {
9874
// /N means repeat every N positions after the last tab stop
9975
if increment_size.is_some() || extend_size.is_some() {
10076
return Err(ParseError::InvalidCharacter("/".to_string()));
10177
}
102-
extend_size = Some(parse_extend_syntax(word, &nums)?);
78+
if nums.is_empty() {
79+
return Err(ParseError::InvalidCharacter("/".to_string()));
80+
}
81+
extend_size = Some(parse_tab_num(word)?);
10382
} else {
10483
// Regular number
10584
if increment_size.is_some() || extend_size.is_some() {
10685
return Err(ParseError::InvalidCharacter(word.to_string()));
10786
}
108-
match word.parse::<usize>() {
109-
Ok(num) => nums.push(num),
110-
Err(e) => {
111-
return match e.kind() {
112-
IntErrorKind::PosOverflow => Err(ParseError::TabSizeTooLarge),
113-
_ => Err(ParseError::InvalidCharacter(
114-
word.trim_start_matches(char::is_numeric).to_string(),
115-
)),
116-
};
117-
}
118-
}
87+
nums.push(parse_tab_num(word)?);
11988
}
12089
}
12190

@@ -127,19 +96,10 @@ fn tabstops_parse(s: &str) -> Result<TabConfig, ParseError> {
12796
});
12897
}
12998

130-
if nums.contains(&0) {
131-
return Err(ParseError::TabSizeCannotBeZero);
132-
}
133-
134-
// Now handle the increment/extend if specified
99+
// Handle the increment if specified
135100
if let Some(inc) = increment_size {
136-
if nums.is_empty() {
137-
return Err(ParseError::InvalidCharacter("+".to_string()));
138-
}
139101
let last = *nums.last().unwrap();
140102
nums.push(last + inc);
141-
} else if extend_size.is_some() && nums.is_empty() {
142-
return Err(ParseError::InvalidCharacter("/".to_string()));
143103
}
144104

145105
if let (false, _) = nums
@@ -185,7 +145,7 @@ impl Options {
185145
increment_size: None,
186146
extend_size: None,
187147
},
188-
Some(s) => tabstops_parse(&s.map(|s| s.as_str()).collect::<Vec<_>>().join(","))?,
148+
Some(s) => parse_tabstops(&s.map(|s| s.as_str()).collect::<Vec<_>>().join(","))?,
189149
};
190150

191151
let aflag = (matches.get_flag(options::ALL) || matches.contains_id(options::TABS))
@@ -610,7 +570,7 @@ fn unexpand(options: &Options) -> UResult<()> {
610570

611571
#[cfg(test)]
612572
mod tests {
613-
use crate::{ParseError, is_digit_or_comma, parse_extend_syntax, parse_increment_syntax};
573+
use crate::{ParseError, is_digit_or_comma, parse_tab_num, parse_tabstops};
614574

615575
#[test]
616576
fn test_is_digit_or_comma() {
@@ -620,64 +580,56 @@ mod tests {
620580
}
621581

622582
#[test]
623-
fn test_parse_increment_syntax() {
624-
let nums = vec![3];
625-
assert_eq!(parse_increment_syntax("6", &nums).unwrap(), 6);
626-
assert_eq!(parse_increment_syntax("12", &nums).unwrap(), 12);
583+
fn test_parse_tab_num() {
584+
assert_eq!(parse_tab_num("6").unwrap(), 6);
585+
assert_eq!(parse_tab_num("12").unwrap(), 12);
586+
assert_eq!(parse_tab_num("9").unwrap(), 9);
587+
assert_eq!(parse_tab_num("4").unwrap(), 4);
627588
}
628589

629590
#[test]
630-
fn test_parse_increment_syntax_errors() {
631-
let nums = vec![3];
632-
let empty_nums = vec![];
633-
591+
fn test_parse_tab_num_errors() {
634592
// Zero is not allowed
635593
assert!(matches!(
636-
parse_increment_syntax("0", &nums),
594+
parse_tab_num("0"),
637595
Err(ParseError::TabSizeCannotBeZero)
638596
));
639597

640-
// Need previous tab stop
598+
// Invalid character
641599
assert!(matches!(
642-
parse_increment_syntax("6", &empty_nums),
600+
parse_tab_num("6x"),
643601
Err(ParseError::InvalidCharacter(_))
644602
));
645603

646604
// Invalid character
647605
assert!(matches!(
648-
parse_increment_syntax("6x", &nums),
606+
parse_tab_num("9y"),
649607
Err(ParseError::InvalidCharacter(_))
650608
));
651609
}
652610

653611
#[test]
654-
fn test_parse_extend_syntax() {
655-
let nums = vec![3];
656-
assert_eq!(parse_extend_syntax("9", &nums).unwrap(), 9);
657-
assert_eq!(parse_extend_syntax("4", &nums).unwrap(), 4);
658-
}
659-
660-
#[test]
661-
fn test_parse_extend_syntax_errors() {
662-
let nums = vec![3];
663-
let empty_nums = vec![];
664-
665-
// Zero is not allowed
612+
fn test_parse_tabstops_extended_syntax() {
613+
// +N requires previous tab stops
666614
assert!(matches!(
667-
parse_extend_syntax("0", &nums),
668-
Err(ParseError::TabSizeCannotBeZero)
669-
));
670-
671-
// Need previous tab stop
672-
assert!(matches!(
673-
parse_extend_syntax("9", &empty_nums),
615+
parse_tabstops("+6"),
674616
Err(ParseError::InvalidCharacter(_))
675617
));
676618

677-
// Invalid character
619+
// /N requires previous tab stops
678620
assert!(matches!(
679-
parse_extend_syntax("9y", &nums),
621+
parse_tabstops("/9"),
680622
Err(ParseError::InvalidCharacter(_))
681623
));
624+
625+
// Valid +N with previous tab stop
626+
let config = parse_tabstops("3,+6").unwrap();
627+
assert_eq!(config.tabstops, vec![3, 9]);
628+
assert_eq!(config.increment_size, Some(6));
629+
630+
// Valid /N with previous tab stop
631+
let config = parse_tabstops("3,/4").unwrap();
632+
assert_eq!(config.tabstops, vec![3]);
633+
assert_eq!(config.extend_size, Some(4));
682634
}
683635
}

0 commit comments

Comments
 (0)