Skip to content

split: impl struct Suffix and add tests #313

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

Merged
merged 2 commits into from
Oct 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 67 additions & 36 deletions file/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,48 +55,37 @@ struct Args {
prefix: String,
}

fn inc_char(ch: char) -> char {
((ch as u8) + 1) as char
}

struct OutputState {
prefix: String,
boundary: u64,

pub struct Suffix {
suffix: String,
suffix_len: u32,
count: u64,
outf: Option<File>,
}

impl OutputState {
fn new(prefix: &str, boundary: u64, suffix_len: u32) -> OutputState {
OutputState {
prefix: String::from(prefix),
boundary,
suffix_len,
suffix: String::new(),
count: 0,
outf: None,
impl Suffix {
pub fn new(len: usize) -> Self {
debug_assert!(len > 0);
Self {
suffix: "a".repeat(len),
}
}

fn incr_suffix(&mut self) -> Result<(), &'static str> {
assert!(self.suffix_len > 1);
fn inc_char(ch: char) -> char {
debug_assert!('a' <= ch && ch < 'z');
((ch as u8) + 1) as char
}
}

impl Iterator for Suffix {
type Item = String;

if self.suffix.is_empty() {
self.suffix = "a".repeat(self.suffix_len as usize);
return Ok(());
}
fn next(&mut self) -> Option<Self::Item> {
let current = self.suffix.clone();

assert!(self.suffix.len() > 1);
let mut i = self.suffix.len() - 1;
loop {
let ch = self.suffix.chars().nth(i).unwrap();
if ch != 'z' {
self.suffix
.replace_range(i..i + 1, inc_char(ch).to_string().as_str());
return Ok(());
.replace_range(i..i + 1, Self::inc_char(ch).to_string().as_str());
return Some(current);
}

self.suffix
Expand All @@ -107,21 +96,43 @@ impl OutputState {
}
i -= 1;
}
None
}
}

Err("maximum suffix reached")
struct OutputState {
prefix: String,
boundary: u64,

suffix: Suffix,
count: u64,
outf: Option<File>,
}

impl OutputState {
fn new(prefix: &str, boundary: u64, suffix_len: u32) -> OutputState {
OutputState {
prefix: String::from(prefix),
boundary,
suffix: Suffix::new(suffix_len as usize),
count: 0,
outf: None,
}
}

fn open_output(&mut self) -> io::Result<()> {
if self.outf.is_some() {
return Ok(());
}

let inc_res = self.incr_suffix();
if let Err(e) = inc_res {
return Err(Error::new(ErrorKind::Other, e));
}
let suffix = match self.suffix.next() {
Some(s) => s,
None => {
return Err(Error::new(ErrorKind::Other, "maximum suffix reached"));
}
};

let out_fn = format!("{}{}", self.prefix, self.suffix);
let out_fn = format!("{}{}", self.prefix, suffix);
let f = OpenOptions::new()
.read(false)
.write(true)
Expand Down Expand Up @@ -152,7 +163,8 @@ impl OutputState {
fn write(&mut self, buf: &[u8]) -> io::Result<()> {
match &mut self.outf {
Some(ref mut f) => f.write_all(buf),
None => Ok(()),
// TODO:
None => panic!("unreachable"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw, you can use macro unreachable!()

}
}

Expand Down Expand Up @@ -265,3 +277,22 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_suffix_inc_char() {
assert_eq!(Suffix::inc_char('a'), 'b');
assert_eq!(Suffix::inc_char('b'), 'c');
assert_eq!(Suffix::inc_char('y'), 'z');
}

#[ignore]
#[test]
fn test_suffix_iterable() {
let suffix = Suffix::new(1);
assert_eq!(suffix.count(), 26);
}
}