-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for starting suffix numbers #3976
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool! One nit about the for loop. I agree that it might not be slow, but it still feels wrong. It's too bad that Number
does not already have some convenient method of this.
src/uu/split/src/filenames.rs
Outdated
Number::DynamicWidth(DynamicWidthNumber::new(radix)) | ||
} else { | ||
Number::FixedWidth(FixedWidthNumber::new(radix, suffix_length)) | ||
}; | ||
FilenameIterator { | ||
// Not very efficient, but typically the suffixes are small. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, this is a bit too hacky for my taste :) Maybe you could implement a set_number/set_digits/something_else
method on Number
? Or add an add
method alongside increment
that takes an argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another alternative is to add a function like DynamicWidthNumber::starting_at(radix, suffix_start)
(and FixedWidthNumber::starting_at(radix, suffix_length, suffix_start)
) to encapsulate the functionality in this loop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK - a little more refactoring than expected, but the numeric class is much nicer now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!
assert_eq!(it.nth(10 * 9 - 1).unwrap(), "chunk_89.txt"); | ||
assert_eq!(it.next().unwrap(), "chunk_9000.txt"); | ||
assert_eq!(it.next().unwrap(), "chunk_9001.txt"); | ||
} | ||
|
||
#[test] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good tests. Could you also write one in the tests/by-util/test_split.rs
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
src/uu/split/src/filenames.rs
Outdated
Number::DynamicWidth(DynamicWidthNumber::new(radix)) | ||
} else { | ||
Number::FixedWidth(FixedWidthNumber::new(radix, suffix_length)) | ||
}; | ||
FilenameIterator { | ||
// Not very efficient, but typically the suffixes are small. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another alternative is to add a function like DynamicWidthNumber::starting_at(radix, suffix_start)
(and FixedWidthNumber::starting_at(radix, suffix_length, suffix_start)
) to encapsulate the functionality in this loop
608289d
to
bbf3fc4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, turns out --suffix-length
always uses fixed width. So we can simplify this a bit, to only have the initial number for fixed width.
src/uu/split/src/number.rs
Outdated
assert_eq!(format!("{}", num(10 * 9)), "90"); | ||
assert_eq!(format!("{}", num(10 * 9 + 1)), "91"); | ||
assert_eq!(format!("{}", num(10 * 99 - 1)), "989"); | ||
assert_eq!(format!("{}", num(10 * 99)), "990"); | ||
assert_eq!(format!("{}", num(10 * 99 + 1)), "991"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was actually very intentional behaviour (and matched GNU), implemented in #2859. Consider what happens to the filename sorting in your implementation:
some_file_98.txt
some_file_99.txt
some_file_100.txt
# gets sorted into this (due to lexical order of strings):
some_file_100.txt
some_file_98.txt
some_file_99.txt
The current algorithm, however, keeps 9
s at the start to ensure being sorted after earlier files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay so looking at this again I made an important discovery for this PR. With the --numeric-suffix
option, the dynamic number is never used. It's always fixed width with a default width of 2. Here is an example:
$ split Cargo.lock Foo_ -l 1 --numeric-suffixes=80
split: output file suffixes exhausted
[ creates Foo_80 to Foo_99 ]
$ split Cargo.lock Foo_ -l 1 --numeric-suffixes=80 --suffix-length=4
[ creates Foo_80 to Foo_3376 ]
$ split Cargo.lock Foo_ -l 1 -d # warning creates a lot of files
[ creates Foo_00 up to Foo_992124 (which is 3307 files) ]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK - I'll take another pass at this later this week. It makes more sense now how it works, but it is a little crazy!
c0738a5
to
802035d
Compare
sorry but needs a small rebase |
802035d
to
ca165bf
Compare
fails on:
|
ca165bf
to
ddc8609
Compare
I plan to finish this today. I have a easy way to compute the names without counting. |
911235e
to
e2b3a3e
Compare
This commit now allows split to pass split/numeric.sh
e2b3a3e
to
49e1cc6
Compare
OK - I think it is ready for review again. I haven't compared the output with the output from gnu split completely yet, but I think the current behavior is much better. The "initial" value for dynamic is a little confusing and could possibly be improved upon. |
Let me know if you need anything more changes on this PR. |
terrific: |
This commit now allows split to pass split/numeric.sh