Skip to content

Commit a21e294

Browse files
committed
Fix a bug in negating a character class.
If the character class is empty and it is negated, then it should yield all of Unicode. Previously, it was returning an empty class.
1 parent f824afb commit a21e294

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

regex-syntax/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,12 @@ impl CharClass {
374374
pub fn negate(mut self) -> CharClass {
375375
fn range(s: char, e: char) -> ClassRange { ClassRange::new(s, e) }
376376

377-
if self.is_empty() { return self; }
377+
if self.is_empty() {
378+
// Inverting an empty range yields all of Unicode.
379+
return CharClass {
380+
ranges: vec![ClassRange { start: '\x00', end: '\u{10ffff}' }],
381+
};
382+
}
378383
self = self.canonicalize();
379384
let mut inv = self.to_empty();
380385
if self[0].start > '\x00' {

regex-syntax/src/properties.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ fn class(ranges: &[(char, char)]) -> CharClass {
3131
#[test]
3232
fn negate() {
3333
fn prop(ranges: Vec<(char, char)>) -> bool {
34-
class(&ranges).canonicalize() == class(&ranges).negate().negate()
34+
let expected = class(&ranges).canonicalize();
35+
let got = class(&ranges).negate().negate();
36+
expected == got
3537
}
3638
qc(prop as fn(Vec<(char, char)>) -> bool);
3739
}

0 commit comments

Comments
 (0)