Skip to content

Commit

Permalink
missed spacer for unary
Browse files Browse the repository at this point in the history
  • Loading branch information
SymmetricChaos committed Oct 22, 2023
1 parent 51e8783 commit 9765034
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions codes/src/mathematical/unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct UnaryCode {
pub maps: LetterWordIntCode,
pub mode: IOMode,
pub invert: bool,
pub spaced: bool,
}

impl UnaryCode {
Expand Down Expand Up @@ -51,6 +52,7 @@ impl Default for UnaryCode {
maps,
mode: IOMode::Letter,
invert: false,
spaced: false,
}
}
}
Expand All @@ -62,21 +64,33 @@ impl Code for UnaryCode {
if self.mode == IOMode::Letter {
for c in text.chars() {
let n = self.maps.char_to_int(c)?;
output.push_str(&self.encode_usize(n))
output.push_str(&self.encode_usize(n));
if self.spaced {
output.push(' ');
}
}
} else if self.mode == IOMode::Word {
for w in text.split(" ") {
let n = self.maps.word_to_int(w)?;
output.push_str(&self.encode_usize(n))
output.push_str(&self.encode_usize(n));
if self.spaced {
output.push(' ');
}
}
} else {
for w in text.split(" ") {
let n =
usize::from_str_radix(w, 10).map_err(|e| CodeError::Input(e.to_string()))?;
output.push_str(&"1".repeat(n));
output.push('0');
if self.spaced {
output.push(' ');
}
}
}
if self.spaced {
output.pop();
}
if self.invert {
Ok(swap_ab('0', '1', &output))
} else {
Expand All @@ -87,9 +101,9 @@ impl Code for UnaryCode {
fn decode(&self, text: &str) -> Result<String, CodeError> {
let mut output = String::new();
let text = if self.invert {
swap_ab('0', '1', text)
swap_ab('0', '1', text).replace(" ", "")
} else {
text.to_string()
text.replace(" ", "")
};
if self.mode == IOMode::Letter {
for section in self.recognize_code(&text) {
Expand Down

0 comments on commit 9765034

Please sign in to comment.