tr: fix octal interpretation of repeat count string (#3178)

* tr: fix octal interpretation of repeat count string

* tr: fix formatting errors

* tr: fix formatting issues 2

* tr: attempt to bypass spell check error

* tr: fix spell check errors attempt 2

* tr: formatting fixes

Co-authored-by: DevSabb <devsabb@local>
This commit is contained in:
DevSabb 2022-02-25 06:11:53 -05:00 committed by GitHub
parent 629e1f7c4e
commit b8a3795d95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 8 deletions

View file

@ -277,15 +277,21 @@ impl Sequence {
separated_pair(Self::parse_backslash_or_char, tag("*"), digit1),
tag("]"),
)(input)
.map(|(l, (c, str))| {
(
l,
match usize::from_str_radix(str, 8) {
.map(|(l, (c, cnt_str))| {
let result = if cnt_str.starts_with('0') {
match usize::from_str_radix(cnt_str, 8) {
Ok(0) => Ok(Self::CharStar(c)),
Ok(count) => Ok(Self::CharRepeat(c, count)),
Err(_) => Err(BadSequence::InvalidRepeatCount(str.to_string())),
},
)
Err(_) => Err(BadSequence::InvalidRepeatCount(cnt_str.to_string())),
}
} else {
match cnt_str.parse::<usize>() {
Ok(0) => Ok(Self::CharStar(c)),
Ok(count) => Ok(Self::CharRepeat(c, count)),
Err(_) => Err(BadSequence::InvalidRepeatCount(cnt_str.to_string())),
}
};
(l, result)
})
}

View file

@ -1,4 +1,4 @@
// spell-checker:ignore aabbaa aabbcc aabc abbb abcc abcdefabcdef abcdefghijk abcdefghijklmn abcdefghijklmnop ABCDEFGHIJKLMNOPQRS abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFZZ abcxyz ABCXYZ abcxyzabcxyz ABCXYZABCXYZ acbdef alnum amzamz AMZXAMZ bbbd cclass cefgm cntrl compl dabcdef dncase Gzabcdefg PQRST upcase wxyzz xdigit xycde xyyye xyyz xyzzzzxyzzzz ZABCDEF Zamz
// spell-checker:ignore aabbaa aabbcc aabc abbb abcc abcdefabcdef abcdefghijk abcdefghijklmn abcdefghijklmnop ABCDEFGHIJKLMNOPQRS abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFZZ abcxyz ABCXYZ abcxyzabcxyz ABCXYZABCXYZ acbdef alnum amzamz AMZXAMZ bbbd cclass cefgm cntrl compl dabcdef dncase Gzabcdefg PQRST upcase wxyzz xdigit xycde xyyye xyyz xyzzzzxyzzzz ZABCDEF Zamz Cdefghijkl Cdefghijklmn
use crate::common::util::*;
#[test]
@ -869,6 +869,26 @@ fn check_against_gnu_tr_tests_o_rep_2() {
.stdout_is("BCx");
}
#[test]
fn octal_repeat_count_test() {
//below will result in 8'x' and 4'y' as octal 010 = decimal 8
new_ucmd!()
.args(&["ABCdefghijkl", "[x*010]Y"])
.pipe_in("ABCdefghijklmn12")
.succeeds()
.stdout_is("xxxxxxxxYYYYmn12");
}
#[test]
fn non_octal_repeat_count_test() {
//below will result in 10'x' and 2'y' as the 10 does not have 0 prefix
new_ucmd!()
.args(&["ABCdefghijkl", "[x*10]Y"])
.pipe_in("ABCdefghijklmn12")
.succeeds()
.stdout_is("xxxxxxxxxxYYmn12");
}
#[test]
fn check_against_gnu_tr_tests_esc() {
// ['esc', qw('a\-z' A-Z), {IN=>'abc-z'}, {OUT=>'AbcBC'}],