From d813e00588c578ce3780163a2de202308d48912c Mon Sep 17 00:00:00 2001 From: Hanif Bin Ariffin Date: Sun, 1 Aug 2021 12:32:35 +0800 Subject: [PATCH] Don't convert octal if its not valid character Signed-off-by: Hanif Bin Ariffin --- src/uu/tr/src/convert.rs | 3 ++- src/uu/tr/src/operation.rs | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/uu/tr/src/convert.rs b/src/uu/tr/src/convert.rs index 0584a82f6..c143681e3 100644 --- a/src/uu/tr/src/convert.rs +++ b/src/uu/tr/src/convert.rs @@ -13,8 +13,9 @@ fn parse_octal(input: &str) -> IResult<&str, char> { preceded(tag("\\"), recognize(many_m_n(1, 3, one_of("01234567")))), |out: &str| { u32::from_str_radix(out, 8) - .map(|u| char::from_u32(u).unwrap()) + .map(|u| char::from_u32(u)) .ok() + .flatten() }, )(input) } diff --git a/src/uu/tr/src/operation.rs b/src/uu/tr/src/operation.rs index 9660e594a..9c6ca6b4a 100644 --- a/src/uu/tr/src/operation.rs +++ b/src/uu/tr/src/operation.rs @@ -22,6 +22,7 @@ pub enum BadSequence { MultipleCharRepeatInSet2, CharRepeatInSet1, InvalidRepeatCount(String), + EmptySet2WhenNotTruncatingSet1, } impl Display for BadSequence { @@ -42,6 +43,9 @@ impl Display for BadSequence { BadSequence::InvalidRepeatCount(count) => { writeln!(f, "invalid repeat count '{}' in [c*n] construct", count) } + BadSequence::EmptySet2WhenNotTruncatingSet1 => { + writeln!(f, "when not truncating set1, string2 must be non-empty") + } } } } @@ -434,7 +438,7 @@ pub struct TranslateOperationStandard { } impl TranslateOperationStandard { - fn new(set1: Vec, set2: Vec) -> Result { + fn new(set1: Vec, set2: Vec) -> Result { if let Some(fallback) = set2.last().copied() { Ok(TranslateOperationStandard { translation_map: set1 @@ -447,7 +451,7 @@ impl TranslateOperationStandard { translation_map: HashMap::new(), }) } else { - Err("when not truncating set1, string2 must be non-empty".to_string()) + Err(BadSequence::EmptySet2WhenNotTruncatingSet1) } } } @@ -473,7 +477,7 @@ impl TranslateOperation { set1: Vec, set2: Vec, complement: bool, - ) -> Result { + ) -> Result { if complement { Ok(TranslateOperation::Complement( TranslateOperationComplement::new(set1, set2),