Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
46 changes: 41 additions & 5 deletions src/uu/tr/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ use nom::{
IResult,
};
use std::{
char,
collections::{HashMap, HashSet},
error::Error,
fmt::{Debug, Display},
io::{BufRead, Write},
ops::Not,
};
use uucore::error::UError;
use uucore::show_warning;

#[derive(Debug, Clone)]
pub enum BadSequence {
Expand Down Expand Up @@ -293,7 +295,9 @@ impl Sequence {
Self::parse_class,
Self::parse_char_equal,
// NOTE: This must be the last one
map(Self::parse_backslash_or_char, |s| Ok(Self::Char(s))),
map(Self::parse_backslash_or_char_with_warning, |s| {
Ok(Self::Char(s))
}),
)))(input)
.map(|(_, r)| r)
.unwrap()
Expand All @@ -302,10 +306,16 @@ impl Sequence {
}

fn parse_octal(input: &[u8]) -> IResult<&[u8], u8> {
// For `parse_char_range`, `parse_char_star`, `parse_char_repeat`, `parse_char_equal`.
// Because in these patterns, there's no ambiguous cases.
preceded(tag("\\"), Self::parse_octal_up_to_three_digits)(input)
}

fn parse_octal_with_warning(input: &[u8]) -> IResult<&[u8], u8> {
preceded(
tag("\\"),
alt((
Self::parse_octal_up_to_three_digits,
Self::parse_octal_up_to_three_digits_with_warning,
// Fallback for if the three digit octal escape is greater than \377 (0xFF), and therefore can't be
// parsed as as a byte
// See test `test_multibyte_octal_sequence`
Expand All @@ -319,13 +329,31 @@ impl Sequence {
recognize(many_m_n(1, 3, one_of("01234567"))),
|out: &[u8]| {
let str_to_parse = std::str::from_utf8(out).unwrap();
match u8::from_str_radix(str_to_parse, 8) {
Ok(ue) => Some(ue),
Err(_pa) => None,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You can simplify this and use ok() to turn the Result into an Option:

Suggested change
match u8::from_str_radix(str_to_parse, 8) {
Ok(ue) => Some(ue),
Err(_pa) => None,
}
u8::from_str_radix(str_to_parse, 8).ok()

Copy link
Contributor

Choose a reason for hiding this comment

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

i wonder if there is a clippy warning for this?!

Copy link
Contributor

Choose a reason for hiding this comment

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

@sylvestre I'm not aware of such a lint.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right. I just copied it and removed the previous TODO comment, so I didn't notice that this match is redundant.

Copy link
Contributor

Choose a reason for hiding this comment

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

@samueltardieu you might be interested by this :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, this is lacking. I'll add this to my TODO list.

Copy link
Contributor

Choose a reason for hiding this comment

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

},
)(input)
}

fn parse_octal_up_to_three_digits_with_warning(input: &[u8]) -> IResult<&[u8], u8> {
map_opt(
recognize(many_m_n(1, 3, one_of("01234567"))),
|out: &[u8]| {
let str_to_parse = std::str::from_utf8(out).unwrap();
match u8::from_str_radix(str_to_parse, 8) {
Ok(ue) => Some(ue),
Err(_pa) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is cleaner to simply use Err(_):

Suggested change
Err(_pa) => {
Err(_) => {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The similar reason to the one above, and I agree with this change too. Thank you for your review and suggestions.

// TODO
// A warning needs to be printed here
// See https://github.com/uutils/coreutils/issues/6821
let origin_octal: &str = std::str::from_utf8(input).unwrap();
let actual_octal_tail: &str = std::str::from_utf8(&input[0..2]).unwrap();
let outstand_char: char = char::from_u32(input[2] as u32).unwrap();
show_warning!(
"the ambiguous octal escape \\{} is being\n interpreted as the 2-byte sequence \\0{}, {}",
origin_octal,
actual_octal_tail,
outstand_char
);
None
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Here it might be an option to use something like:

let result = u8::from_str_radix(str_to_parse, 8).ok();

if result.is_none() {
     let origin_octal: &str = std::str::from_utf8(input).unwrap();
     let actual_octal_tail: &str = std::str::from_utf8(&input[0..2]).unwrap();
     let outstand_char: char = char::from_u32(input[2] as u32).unwrap();
     show_warning!(
         "the ambiguous octal escape \\{} is being\n        interpreted as the 2-byte sequence \\0{}, {}",
         origin_octal,
         actual_octal_tail,
         outstand_char
     );
}

result

This way you could remove one indentation level.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think this is necessary. But it does make the code looks nicer. I think I'll accept this suggestion.

Expand Down Expand Up @@ -360,6 +388,14 @@ impl Sequence {
alt((Self::parse_octal, Self::parse_backslash, Self::single_char))(input)
}

fn parse_backslash_or_char_with_warning(input: &[u8]) -> IResult<&[u8], u8> {
alt((
Self::parse_octal_with_warning,
Self::parse_backslash,
Self::single_char,
))(input)
}

fn single_char(input: &[u8]) -> IResult<&[u8], u8> {
take(1usize)(input).map(|(l, a)| (l, a[0]))
}
Expand Down
4 changes: 1 addition & 3 deletions tests/by-util/test_tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1494,9 +1494,7 @@ fn test_multibyte_octal_sequence() {
.args(&["-d", r"\501"])
.pipe_in("(1Ł)")
.succeeds()
// TODO
// A warning needs to be printed here
// See https://github.com/uutils/coreutils/issues/6821
.stderr_is("tr: warning: the ambiguous octal escape \\501 is being\n interpreted as the 2-byte sequence \\050, 1\n")
.stdout_is("Ł)");
}

Expand Down
Loading