mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07:00
split: use iterator to produce filenames
Replace the `FilenameFactory` with `FilenameIterator` and calls to `FilenameFactory::make()` with calls to `FilenameIterator::next()`. We did not need the fully generality of being able to produce the filename for an arbitrary chunk index. Instead we need only iterate over filenames one after another. This allows for a less mathematically dense algorithm that is easier to understand and maintain. Furthermore, it can be connected to some familiar concepts from the representation of numbers as a sequence of digits. This does not change the behavior of the `split` program, just the implementation of how filenames are produced. Co-authored-by: Terts Diepraam <terts.diepraam@gmail.com>
This commit is contained in:
co-authored by
Terts Diepraam
parent
ff4ac206f5
commit
a5b435da58
+105
-452
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -8,9 +8,10 @@
|
||||
// spell-checker:ignore (ToDO) PREFIXaa
|
||||
|
||||
mod filenames;
|
||||
mod number;
|
||||
mod platform;
|
||||
|
||||
use crate::filenames::FilenameFactory;
|
||||
use crate::filenames::FilenameIterator;
|
||||
use clap::{crate_version, App, AppSettings, Arg, ArgMatches};
|
||||
use std::convert::TryFrom;
|
||||
use std::env;
|
||||
@@ -384,7 +385,7 @@ where
|
||||
let chunk_size = (num_bytes / (num_chunks as u64)) as usize;
|
||||
|
||||
// This object is responsible for creating the filename for each chunk.
|
||||
let filename_factory = FilenameFactory::new(
|
||||
let mut filename_iterator = FilenameIterator::new(
|
||||
&settings.prefix,
|
||||
&settings.additional_suffix,
|
||||
settings.suffix_length,
|
||||
@@ -394,9 +395,9 @@ where
|
||||
// Create one writer for each chunk. This will create each
|
||||
// of the underlying files (if not in `--filter` mode).
|
||||
let mut writers = vec![];
|
||||
for i in 0..num_chunks {
|
||||
let filename = filename_factory
|
||||
.make(i)
|
||||
for _ in 0..num_chunks {
|
||||
let filename = filename_iterator
|
||||
.next()
|
||||
.ok_or_else(|| USimpleError::new(1, "output file suffixes exhausted"))?;
|
||||
let writer = platform::instantiate_current_writer(&settings.filter, filename.as_str());
|
||||
writers.push(writer);
|
||||
@@ -462,17 +463,16 @@ fn split(settings: Settings) -> UResult<()> {
|
||||
};
|
||||
|
||||
// This object is responsible for creating the filename for each chunk.
|
||||
let filename_factory = FilenameFactory::new(
|
||||
let mut filename_iterator = FilenameIterator::new(
|
||||
&settings.prefix,
|
||||
&settings.additional_suffix,
|
||||
settings.suffix_length,
|
||||
settings.numeric_suffix,
|
||||
);
|
||||
let mut fileno = 0;
|
||||
loop {
|
||||
// Get a new part file set up, and construct `writer` for it.
|
||||
let filename = filename_factory
|
||||
.make(fileno)
|
||||
let filename = filename_iterator
|
||||
.next()
|
||||
.ok_or_else(|| USimpleError::new(1, "output file suffixes exhausted"))?;
|
||||
let mut writer = platform::instantiate_current_writer(&settings.filter, filename.as_str());
|
||||
|
||||
@@ -509,8 +509,6 @@ fn split(settings: Settings) -> UResult<()> {
|
||||
if settings.verbose {
|
||||
println!("creating file {}", filename.quote());
|
||||
}
|
||||
|
||||
fileno += 1;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user