Remove new from struct names

Signed-off-by: Hanif Bin Ariffin <hanif.ariffin.4326@gmail.com>
This commit is contained in:
Hanif Bin Ariffin 2021-07-19 21:32:52 +08:00
parent 6ff826b712
commit f13c0ba5a7
2 changed files with 31 additions and 31 deletions

View file

@ -268,26 +268,26 @@ impl Sequence {
}
}
pub trait SymbolTranslatorNew {
pub trait SymbolTranslator {
fn translate(&mut self, current: char) -> Option<char>;
}
#[derive(Debug, Clone)]
pub struct DeleteOperationNew {
pub struct DeleteOperation {
set: Vec<Sequence>,
complement_flag: bool,
}
impl DeleteOperationNew {
pub fn new(set: Vec<Sequence>, complement_flag: bool) -> DeleteOperationNew {
DeleteOperationNew {
impl DeleteOperation {
pub fn new(set: Vec<Sequence>, complement_flag: bool) -> DeleteOperation {
DeleteOperation {
set,
complement_flag,
}
}
}
impl SymbolTranslatorNew for DeleteOperationNew {
impl SymbolTranslator for DeleteOperation {
fn translate(&mut self, current: char) -> Option<char> {
let found = self.set.iter().any(|sequence| match sequence {
Sequence::Char(c) => c.eq(&current),
@ -298,7 +298,7 @@ impl SymbolTranslatorNew for DeleteOperationNew {
}
#[derive(Debug, Clone)]
pub enum TranslateOperationNew {
pub enum TranslateOperation {
Standard(HashMap<char, char>),
Complement(
// iter
@ -314,7 +314,7 @@ pub enum TranslateOperationNew {
),
}
impl TranslateOperationNew {
impl TranslateOperation {
fn next_complement_char(mut iter: u32) -> (u32, char) {
while char::from_u32(iter).is_none() {
iter = iter.saturating_add(1)
@ -323,13 +323,13 @@ impl TranslateOperationNew {
}
}
impl TranslateOperationNew {
impl TranslateOperation {
pub fn new(
pset1: Vec<Sequence>,
pset2: Vec<Sequence>,
truncate_set1: bool,
complement: bool,
) -> TranslateOperationNew {
) -> TranslateOperation {
let mut set1 = pset1
.into_iter()
.flat_map(Sequence::dissolve)
@ -343,7 +343,7 @@ impl TranslateOperationNew {
}
let fallback = set2.last().cloned().unwrap();
if complement {
TranslateOperationNew::Complement(
TranslateOperation::Complement(
0,
set1,
set2,
@ -352,7 +352,7 @@ impl TranslateOperationNew {
HashMap::new(),
)
} else {
TranslateOperationNew::Standard(
TranslateOperation::Standard(
set1.into_iter()
.zip(set2.into_iter().chain(std::iter::repeat(fallback)))
.collect::<HashMap<_, _>>(),
@ -361,15 +361,15 @@ impl TranslateOperationNew {
}
}
impl SymbolTranslatorNew for TranslateOperationNew {
impl SymbolTranslator for TranslateOperation {
fn translate(&mut self, current: char) -> Option<char> {
match self {
TranslateOperationNew::Standard(map) => Some(
TranslateOperation::Standard(map) => Some(
map.iter()
.find_map(|(l, r)| l.eq(&current).then(|| *r))
.unwrap_or(current),
),
TranslateOperationNew::Complement(iter, set1, set2, fallback, mapped_characters) => {
TranslateOperation::Complement(iter, set1, set2, fallback, mapped_characters) => {
// First, try to see if current char is already mapped
// If so, return the mapped char
// Else, pop from set2
@ -381,7 +381,7 @@ impl SymbolTranslatorNew for TranslateOperationNew {
while mapped_characters.get(&current).is_none() {
if let Some(p) = set2.pop() {
let (next_index, next_value) =
TranslateOperationNew::next_complement_char(*iter);
TranslateOperation::next_complement_char(*iter);
*iter = next_index;
mapped_characters.insert(next_value, p);
} else {
@ -396,15 +396,15 @@ impl SymbolTranslatorNew for TranslateOperationNew {
}
#[derive(Debug, Clone)]
pub struct SqueezeOperationNew {
pub struct SqueezeOperation {
squeeze_set: Vec<char>,
complement: bool,
previous: Option<char>,
}
impl SqueezeOperationNew {
pub fn new(squeeze_set: Vec<Sequence>, complement: bool) -> SqueezeOperationNew {
SqueezeOperationNew {
impl SqueezeOperation {
pub fn new(squeeze_set: Vec<Sequence>, complement: bool) -> SqueezeOperation {
SqueezeOperation {
squeeze_set: squeeze_set
.into_iter()
.flat_map(Sequence::dissolve)
@ -415,7 +415,7 @@ impl SqueezeOperationNew {
}
}
impl SymbolTranslatorNew for SqueezeOperationNew {
impl SymbolTranslator for SqueezeOperation {
fn translate(&mut self, current: char) -> Option<char> {
if self.complement {
let next = if self.squeeze_set.iter().any(|c| c.eq(&current)) {
@ -461,7 +461,7 @@ impl SymbolTranslatorNew for SqueezeOperationNew {
pub fn translate_input_new<T, R, W>(input: &mut R, output: &mut W, mut translator: T)
where
T: SymbolTranslatorNew,
T: SymbolTranslator,
R: BufRead,
W: Write,
{

View file

@ -18,10 +18,10 @@ mod operation;
use clap::{crate_version, App, Arg};
use nom::AsBytes;
use operation::{translate_input_new, Sequence, SqueezeOperationNew, TranslateOperationNew};
use operation::{translate_input_new, Sequence, SqueezeOperation, TranslateOperation};
use std::io::{stdin, stdout, BufReader, BufWriter};
use crate::operation::DeleteOperationNew;
use crate::operation::DeleteOperation;
use uucore::InvalidEncodingHandling;
static ABOUT: &str = "translate or delete characters";
@ -96,30 +96,30 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
{
let mut delete_writer = BufWriter::new(&mut delete_buffer);
let delete_op =
DeleteOperationNew::new(Sequence::parse_set_string(&sets[0]), complement_flag);
DeleteOperation::new(Sequence::parse_set_string(&sets[0]), complement_flag);
translate_input_new(&mut locked_stdin, &mut delete_writer, delete_op);
}
{
let mut squeeze_reader = BufReader::new(delete_buffer.as_bytes());
let squeeze_op =
SqueezeOperationNew::new(Sequence::parse_set_string(&sets[1]), complement_flag);
SqueezeOperation::new(Sequence::parse_set_string(&sets[1]), complement_flag);
translate_input_new(&mut squeeze_reader, &mut buffered_stdout, squeeze_op);
}
} else {
let op = DeleteOperationNew::new(Sequence::parse_set_string(&sets[0]), complement_flag);
let op = DeleteOperation::new(Sequence::parse_set_string(&sets[0]), complement_flag);
translate_input_new(&mut locked_stdin, &mut buffered_stdout, op);
}
} else if squeeze_flag {
if sets.len() < 2 {
let op =
SqueezeOperationNew::new(Sequence::parse_set_string(&sets[0]), complement_flag);
SqueezeOperation::new(Sequence::parse_set_string(&sets[0]), complement_flag);
translate_input_new(&mut locked_stdin, &mut buffered_stdout, op);
} else {
let mut translate_buffer = vec![];
{
let mut writer = BufWriter::new(&mut translate_buffer);
let translate_op = TranslateOperationNew::new(
let translate_op = TranslateOperation::new(
Sequence::parse_set_string(&sets[0]),
Sequence::parse_set_string(&sets[1]),
truncate_set1_flag,
@ -130,12 +130,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
{
let mut reader = BufReader::new(translate_buffer.as_bytes());
let squeeze_op =
SqueezeOperationNew::new(Sequence::parse_set_string(&sets[1]), false);
SqueezeOperation::new(Sequence::parse_set_string(&sets[1]), false);
translate_input_new(&mut reader, &mut buffered_stdout, squeeze_op);
}
}
} else {
let op = TranslateOperationNew::new(
let op = TranslateOperation::new(
Sequence::parse_set_string(&sets[0]),
Sequence::parse_set_string(&sets[1]),
truncate_set1_flag,