mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
split: remove unused ByteSplitter and LineSplitter
This commit is contained in:
parent
1d7e1b8732
commit
70ca1f45ea
1 changed files with 1 additions and 101 deletions
|
@ -13,11 +13,10 @@ mod platform;
|
||||||
|
|
||||||
use crate::filenames::FilenameIterator;
|
use crate::filenames::FilenameIterator;
|
||||||
use clap::{crate_version, App, AppSettings, Arg, ArgMatches};
|
use clap::{crate_version, App, AppSettings, Arg, ArgMatches};
|
||||||
use std::convert::TryFrom;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fs::{metadata, File};
|
use std::fs::{metadata, File};
|
||||||
use std::io::{stdin, BufRead, BufReader, BufWriter, ErrorKind, Read, Write};
|
use std::io::{stdin, BufReader, BufWriter, ErrorKind, Read, Write};
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
|
@ -550,105 +549,6 @@ impl<'a> Write for LineChunkWriter<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trait Splitter {
|
|
||||||
// Consume as much as possible from `reader` so as to saturate `writer`.
|
|
||||||
// Equivalent to finishing one of the part files. Returns the number of
|
|
||||||
// bytes that have been moved.
|
|
||||||
fn consume(
|
|
||||||
&mut self,
|
|
||||||
reader: &mut BufReader<Box<dyn Read>>,
|
|
||||||
writer: &mut BufWriter<Box<dyn Write>>,
|
|
||||||
) -> std::io::Result<u128>;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct LineSplitter {
|
|
||||||
lines_per_split: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl LineSplitter {
|
|
||||||
fn new(chunk_size: usize) -> Self {
|
|
||||||
Self {
|
|
||||||
lines_per_split: chunk_size,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Splitter for LineSplitter {
|
|
||||||
fn consume(
|
|
||||||
&mut self,
|
|
||||||
reader: &mut BufReader<Box<dyn Read>>,
|
|
||||||
writer: &mut BufWriter<Box<dyn Write>>,
|
|
||||||
) -> std::io::Result<u128> {
|
|
||||||
let mut bytes_consumed = 0u128;
|
|
||||||
let mut buffer = String::with_capacity(1024);
|
|
||||||
for _ in 0..self.lines_per_split {
|
|
||||||
let bytes_read = reader.read_line(&mut buffer)?;
|
|
||||||
// If we ever read 0 bytes then we know we've hit EOF.
|
|
||||||
if bytes_read == 0 {
|
|
||||||
return Ok(bytes_consumed);
|
|
||||||
}
|
|
||||||
|
|
||||||
writer.write_all(buffer.as_bytes())?;
|
|
||||||
// Empty out the String buffer since `read_line` appends instead of
|
|
||||||
// replaces.
|
|
||||||
buffer.clear();
|
|
||||||
|
|
||||||
bytes_consumed += bytes_read as u128;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(bytes_consumed)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ByteSplitter {
|
|
||||||
bytes_per_split: u128,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ByteSplitter {
|
|
||||||
fn new(chunk_size: usize) -> Self {
|
|
||||||
Self {
|
|
||||||
bytes_per_split: u128::try_from(chunk_size).unwrap(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Splitter for ByteSplitter {
|
|
||||||
fn consume(
|
|
||||||
&mut self,
|
|
||||||
reader: &mut BufReader<Box<dyn Read>>,
|
|
||||||
writer: &mut BufWriter<Box<dyn Write>>,
|
|
||||||
) -> std::io::Result<u128> {
|
|
||||||
// We buffer reads and writes. We proceed until `bytes_consumed` is
|
|
||||||
// equal to `self.bytes_per_split` or we reach EOF.
|
|
||||||
let mut bytes_consumed = 0u128;
|
|
||||||
const BUFFER_SIZE: usize = 1024;
|
|
||||||
let mut buffer = [0u8; BUFFER_SIZE];
|
|
||||||
while bytes_consumed < self.bytes_per_split {
|
|
||||||
// Don't overshoot `self.bytes_per_split`! Note: Using std::cmp::min
|
|
||||||
// doesn't really work since we have to get types to match which
|
|
||||||
// can't be done in a way that keeps all conversions safe.
|
|
||||||
let bytes_desired = if (BUFFER_SIZE as u128) <= self.bytes_per_split - bytes_consumed {
|
|
||||||
BUFFER_SIZE
|
|
||||||
} else {
|
|
||||||
// This is a safe conversion since the difference must be less
|
|
||||||
// than BUFFER_SIZE in this branch.
|
|
||||||
(self.bytes_per_split - bytes_consumed) as usize
|
|
||||||
};
|
|
||||||
let bytes_read = reader.read(&mut buffer[0..bytes_desired])?;
|
|
||||||
// If we ever read 0 bytes then we know we've hit EOF.
|
|
||||||
if bytes_read == 0 {
|
|
||||||
return Ok(bytes_consumed);
|
|
||||||
}
|
|
||||||
|
|
||||||
writer.write_all(&buffer[0..bytes_read])?;
|
|
||||||
|
|
||||||
bytes_consumed += bytes_read as u128;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(bytes_consumed)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Split a file into a specific number of chunks by byte.
|
/// Split a file into a specific number of chunks by byte.
|
||||||
///
|
///
|
||||||
/// This function always creates one output file for each chunk, even
|
/// This function always creates one output file for each chunk, even
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue