mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
expand: improve handing of +
This commit is contained in:
parent
3ad36a49cb
commit
cbab8677e6
2 changed files with 73 additions and 11 deletions
|
@ -38,7 +38,7 @@ static DEFAULT_TABSTOP: usize = 8;
|
||||||
|
|
||||||
/// The mode to use when replacing tabs beyond the last one specified in
|
/// The mode to use when replacing tabs beyond the last one specified in
|
||||||
/// the `--tabs` argument.
|
/// the `--tabs` argument.
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq, Debug)]
|
||||||
enum RemainingMode {
|
enum RemainingMode {
|
||||||
None,
|
None,
|
||||||
Slash,
|
Slash,
|
||||||
|
@ -126,12 +126,8 @@ fn tabstops_parse(s: &str) -> Result<(RemainingMode, Vec<usize>), ParseError> {
|
||||||
let bytes = word.as_bytes();
|
let bytes = word.as_bytes();
|
||||||
for i in 0..bytes.len() {
|
for i in 0..bytes.len() {
|
||||||
match bytes[i] {
|
match bytes[i] {
|
||||||
b'+' => {
|
b'+' => remaining_mode = RemainingMode::Plus,
|
||||||
remaining_mode = RemainingMode::Plus;
|
b'/' => remaining_mode = RemainingMode::Slash,
|
||||||
}
|
|
||||||
b'/' => {
|
|
||||||
remaining_mode = RemainingMode::Slash;
|
|
||||||
}
|
|
||||||
_ => {
|
_ => {
|
||||||
// Parse a number from the byte sequence.
|
// Parse a number from the byte sequence.
|
||||||
let s = from_utf8(&bytes[i..]).unwrap();
|
let s = from_utf8(&bytes[i..]).unwrap();
|
||||||
|
@ -190,10 +186,16 @@ fn tabstops_parse(s: &str) -> Result<(RemainingMode, Vec<usize>), ParseError> {
|
||||||
// then just use the default tabstops.
|
// then just use the default tabstops.
|
||||||
if nums.is_empty() {
|
if nums.is_empty() {
|
||||||
nums = vec![DEFAULT_TABSTOP];
|
nums = vec![DEFAULT_TABSTOP];
|
||||||
|
remaining_mode = RemainingMode::None;
|
||||||
|
}
|
||||||
|
|
||||||
|
if nums.len() == 1 {
|
||||||
|
remaining_mode = RemainingMode::None;
|
||||||
}
|
}
|
||||||
Ok((remaining_mode, nums))
|
Ok((remaining_mode, nums))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct Options {
|
struct Options {
|
||||||
files: Vec<String>,
|
files: Vec<String>,
|
||||||
tabstops: Vec<usize>,
|
tabstops: Vec<usize>,
|
||||||
|
@ -374,7 +376,7 @@ enum CharType {
|
||||||
|
|
||||||
fn expand(options: &Options) -> std::io::Result<()> {
|
fn expand(options: &Options) -> std::io::Result<()> {
|
||||||
use self::CharType::*;
|
use self::CharType::*;
|
||||||
|
println!("{:?}", options);
|
||||||
let mut output = BufWriter::new(stdout());
|
let mut output = BufWriter::new(stdout());
|
||||||
let ts = options.tabstops.as_ref();
|
let ts = options.tabstops.as_ref();
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
|
|
|
@ -275,7 +275,7 @@ fn test_tabs_shortcut() {
|
||||||
.args(&["-2", "-5", "-7"])
|
.args(&["-2", "-5", "-7"])
|
||||||
.pipe_in("\ta\tb\tc")
|
.pipe_in("\ta\tb\tc")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
// 01234567890
|
// 01234567890
|
||||||
.stdout_is(" a b c");
|
.stdout_is(" a b c");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -285,7 +285,7 @@ fn test_comma_separated_tabs_shortcut() {
|
||||||
.args(&["-2,5", "-7"])
|
.args(&["-2,5", "-7"])
|
||||||
.pipe_in("\ta\tb\tc")
|
.pipe_in("\ta\tb\tc")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
// 01234567890
|
// 01234567890
|
||||||
.stdout_is(" a b c");
|
.stdout_is(" a b c");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -295,6 +295,66 @@ fn test_tabs_and_tabs_shortcut_mixed() {
|
||||||
.args(&["-2", "--tabs=5", "-7"])
|
.args(&["-2", "--tabs=5", "-7"])
|
||||||
.pipe_in("\ta\tb\tc")
|
.pipe_in("\ta\tb\tc")
|
||||||
.succeeds()
|
.succeeds()
|
||||||
// 01234567890
|
// 01234567890
|
||||||
.stdout_is(" a b c");
|
.stdout_is(" a b c");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ignore_initial_plus() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["--tabs=+3"])
|
||||||
|
.pipe_in("\ta\tb\tc")
|
||||||
|
.succeeds()
|
||||||
|
// 01234567890
|
||||||
|
.stdout_is(" a b c");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ignore_initial_pluses() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["--tabs=++3"])
|
||||||
|
.pipe_in("\ta\tb\tc")
|
||||||
|
.succeeds()
|
||||||
|
// 01234567890
|
||||||
|
.stdout_is(" a b c");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ignore_initial_slash() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["--tabs=/3"])
|
||||||
|
.pipe_in("\ta\tb\tc")
|
||||||
|
.succeeds()
|
||||||
|
// 01234567890
|
||||||
|
.stdout_is(" a b c");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ignore_initial_slashes() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["--tabs=//3"])
|
||||||
|
.pipe_in("\ta\tb\tc")
|
||||||
|
.succeeds()
|
||||||
|
// 01234567890
|
||||||
|
.stdout_is(" a b c");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ignore_initial_plus_slash_combination() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["--tabs=+/3"])
|
||||||
|
.pipe_in("\ta\tb\tc")
|
||||||
|
.succeeds()
|
||||||
|
// 01234567890
|
||||||
|
.stdout_is(" a b c");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_comma_with_plus_and_multi_character_values() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["--tabs=3,+6"])
|
||||||
|
.pipe_in("\taaa\tbbbb\tcccc")
|
||||||
|
.succeeds()
|
||||||
|
// 01234567890
|
||||||
|
.stdout_is(" aaa bbb ccc");
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue