-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refuse to translate if set2 contains more than one unique characters and set1 contains a character class #6472
Merged
sylvestre
merged 2 commits into
uutils:main
from
cvonelm:issue-6344-complement-error-if-2-uniques
Jun 22, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -36,6 +36,7 @@ pub enum BadSequence { | |||||||
ClassExceptLowerUpperInSet2, | ||||||||
ClassInSet2NotMatchedBySet1, | ||||||||
Set1LongerSet2EndsInClass, | ||||||||
ComplementMoreThanOneUniqueInSet2, | ||||||||
} | ||||||||
|
||||||||
impl Display for BadSequence { | ||||||||
|
@@ -66,6 +67,9 @@ impl Display for BadSequence { | |||||||
Self::Set1LongerSet2EndsInClass => { | ||||||||
write!(f, "when translating with string1 longer than string2,\nthe latter string must not end with a character class") | ||||||||
} | ||||||||
Self::ComplementMoreThanOneUniqueInSet2 => { | ||||||||
write!(f, "when translating with complemented character classes,\nstring2 must map all characters in the domain to one") | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
@@ -224,7 +228,6 @@ impl Sequence { | |||||||
.count(); | ||||||||
|
||||||||
let star_compensate_len = set1_len.saturating_sub(set2_len); | ||||||||
|
||||||||
//Replace CharStar with CharRepeat | ||||||||
set2 = set2 | ||||||||
.iter() | ||||||||
|
@@ -263,6 +266,21 @@ impl Sequence { | |||||||
.filter_map(to_u8) | ||||||||
.collect(); | ||||||||
|
||||||||
// Calculate the set of unique characters in set2 | ||||||||
let mut set2_uniques = set2_solved.clone(); | ||||||||
set2_uniques.sort(); | ||||||||
set2_uniques.dedup(); | ||||||||
|
||||||||
//If the complement flag is used in translate mode, only one unique character may appear in | ||||||||
//set2. Validate this with the set of uniques in set2 that we just generated. | ||||||||
if set1.iter().any(|x| matches!(x, Self::Class(_))) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
&& translating | ||||||||
&& complement_flag | ||||||||
&& set2_uniques.len() > 1 | ||||||||
{ | ||||||||
return Err(BadSequence::ComplementMoreThanOneUniqueInSet2); | ||||||||
} | ||||||||
|
||||||||
if set2_solved.len() < set1_solved.len() | ||||||||
&& !truncate_set1_flag | ||||||||
&& matches!( | ||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -1386,3 +1386,23 @@ fn check_set1_longer_set2_ends_in_class_with_trunc() { | |||||||
.args(&["-t", "[:lower:]a", "[:upper:]"]) | ||||||||
.succeeds(); | ||||||||
} | ||||||||
|
||||||||
#[test] | ||||||||
fn check_complement_2_unique_in_set2() { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
let x226 = "x".repeat(226); | ||||||||
|
||||||||
// [y*] is expanded tp "y" here | ||||||||
let arg = x226 + "[y*]xxx"; | ||||||||
new_ucmd!().args(&["-c", "[:upper:]", arg.as_str()]).fails(); | ||||||||
} | ||||||||
|
||||||||
#[test] | ||||||||
fn check_complement_1_unique_in_set2() { | ||||||||
let x226 = "x".repeat(226); | ||||||||
|
||||||||
// [y*] is expanded to "" here | ||||||||
let arg = x226 + "[y*]xxxx"; | ||||||||
new_ucmd!() | ||||||||
.args(&["-c", "[:upper:]", arg.as_str()]) | ||||||||
.succeeds(); | ||||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.