mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-05 23:47:46 +00:00
Some small cleanup to translation module
Signed-off-by: Hanif Bin Ariffin <hanif.ariffin.4326@gmail.com>
This commit is contained in:
parent
840c6e7b91
commit
572cbc6ba2
2 changed files with 34 additions and 23 deletions
|
@ -146,7 +146,7 @@ impl Sequence {
|
||||||
// This part is unchecked...not all `u32` => `char` is valid
|
// This part is unchecked...not all `u32` => `char` is valid
|
||||||
Sequence::CharRange(
|
Sequence::CharRange(
|
||||||
(start..=end)
|
(start..=end)
|
||||||
.map(|c| std::char::from_u32(c).unwrap())
|
.filter_map(|c| std::char::from_u32(c))
|
||||||
.collect(),
|
.collect(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -268,7 +268,16 @@ impl SymbolTranslatorNew for DeleteOperationNew {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum TranslateOperationNew {
|
pub enum TranslateOperationNew {
|
||||||
Standard(HashMap<char, char>),
|
Standard(HashMap<char, char>),
|
||||||
Complement(Vec<char>, Vec<char>, HashMap<char, char>, char),
|
Complement(u32, Vec<char>, Vec<char>, char, HashMap<char, char>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TranslateOperationNew {
|
||||||
|
fn next_complement_char(mut iter: u32) -> (u32, char) {
|
||||||
|
while let None = char::from_u32(iter) {
|
||||||
|
iter = iter.saturating_add(1)
|
||||||
|
}
|
||||||
|
(iter, char::from_u32(iter).unwrap())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TranslateOperationNew {
|
impl TranslateOperationNew {
|
||||||
|
@ -279,22 +288,21 @@ impl TranslateOperationNew {
|
||||||
complement: bool,
|
complement: bool,
|
||||||
) -> TranslateOperationNew {
|
) -> TranslateOperationNew {
|
||||||
let fallback = set2.last().cloned().unwrap();
|
let fallback = set2.last().cloned().unwrap();
|
||||||
|
println!("fallback:{:#?}", fallback);
|
||||||
if truncate_set2 {
|
if truncate_set2 {
|
||||||
set2.truncate(set1.len());
|
set2.truncate(set1.len());
|
||||||
}
|
}
|
||||||
if complement {
|
if complement {
|
||||||
TranslateOperationNew::Complement(
|
TranslateOperationNew::Complement(
|
||||||
set1.into_iter()
|
0,
|
||||||
.flat_map(Sequence::dissolve)
|
set1.into_iter().flat_map(Sequence::dissolve).collect(),
|
||||||
.rev()
|
|
||||||
.collect(),
|
|
||||||
set2.into_iter()
|
set2.into_iter()
|
||||||
.flat_map(Sequence::dissolve)
|
.flat_map(Sequence::dissolve)
|
||||||
.rev()
|
.rev()
|
||||||
.collect(),
|
.collect(),
|
||||||
HashMap::new(),
|
|
||||||
// TODO: Check how `tr` actually handles this
|
// TODO: Check how `tr` actually handles this
|
||||||
fallback.dissolve().first().cloned().unwrap(),
|
fallback.dissolve().first().cloned().unwrap(),
|
||||||
|
HashMap::new(),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
TranslateOperationNew::Standard(
|
TranslateOperationNew::Standard(
|
||||||
|
@ -319,22 +327,26 @@ impl SymbolTranslatorNew for TranslateOperationNew {
|
||||||
.find_map(|(l, r)| l.eq(¤t).then(|| *r))
|
.find_map(|(l, r)| l.eq(¤t).then(|| *r))
|
||||||
.unwrap_or(current),
|
.unwrap_or(current),
|
||||||
),
|
),
|
||||||
TranslateOperationNew::Complement(set1, set2, mapped_characters, fallback) => {
|
TranslateOperationNew::Complement(iter, set1, set2, fallback, mapped_characters) => {
|
||||||
// First, see if we have already mapped this character.
|
// First, try to see if current char is already mapped
|
||||||
// If so, return it.
|
// If so, return the mapped char
|
||||||
// Else, check if current character is part of set1
|
// Else, pop from set2
|
||||||
// If so, return it.
|
// If we popped something, map the next complement character to this value
|
||||||
// Else, consume from set2, create the translation pair, and return the mapped character
|
// If set2 is empty, we just map the current char directly to fallback --- to avoid looping unnecessarily
|
||||||
match mapped_characters.get(¤t) {
|
if let Some(c) = set1.iter().find(|c| c.eq(&¤t)) {
|
||||||
Some(k) => Some(*k),
|
Some(*c)
|
||||||
None => match set1.iter().any(|c| c.eq(&¤t)) {
|
} else {
|
||||||
true => Some(current),
|
while let None = mapped_characters.get(¤t) {
|
||||||
false => {
|
if let Some(p) = set2.pop() {
|
||||||
let popped = set2.pop().unwrap_or(*fallback);
|
let (next_index, next_value) =
|
||||||
mapped_characters.insert(current, popped);
|
TranslateOperationNew::next_complement_char(*iter);
|
||||||
Some(popped)
|
*iter = next_index;
|
||||||
|
mapped_characters.insert(next_value, p);
|
||||||
|
} else {
|
||||||
|
mapped_characters.insert(current, *fallback);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
Some(*mapped_characters.get(¤t).unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -303,7 +303,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
truncate_set1_flag,
|
truncate_set1_flag,
|
||||||
complement_flag,
|
complement_flag,
|
||||||
);
|
);
|
||||||
println!("op:{:#?}", op);
|
|
||||||
translate_input_new(&mut locked_stdin, &mut buffered_stdout, op);
|
translate_input_new(&mut locked_stdin, &mut buffered_stdout, op);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue