Skip to content

Commit

Permalink
Make ASCII classes consistent with other engines.
Browse files Browse the repository at this point in the history
For example, the regex `[:upper:]` used to correspond to the `upper`
ASCII character class, but it now corresponds to the character class
containing the characters `:upper:`.

Forms like `[[:upper:][:blank:]]` are still accepted.

Fixes #175
  • Loading branch information
BurntSushi committed Dec 30, 2016
1 parent 8ee9262 commit bc06024
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
13 changes: 4 additions & 9 deletions regex-syntax/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ impl Parser {
'*' => try!(self.parse_simple_repeat(Repeater::ZeroOrMore)),
'+' => try!(self.parse_simple_repeat(Repeater::OneOrMore)),
'{' => try!(self.parse_counted_repeat()),
'[' => match self.maybe_parse_ascii() {
None => try!(self.parse_class()),
Some(cls) => Build::Expr(Expr::Class(cls)),
},
'[' => try!(self.parse_class()),
'^' => {
if self.flags.multi {
self.parse_one(Expr::StartLine)
Expand Down Expand Up @@ -2224,10 +2221,11 @@ mod tests {

#[test]
fn ascii_classes() {
assert_eq!(p("[:upper:]"), Expr::Class(class(UPPER)));
assert_eq!(p("[:blank:]"), Expr::Class(class(&[
(':', ':'), ('a', 'b'), ('k', 'l'), ('n', 'n'),
])));
assert_eq!(p("[[:upper:]]"), Expr::Class(class(UPPER)));

assert_eq!(pb("(?-u)[:upper:]"), Expr::Class(class(UPPER)));
assert_eq!(pb("(?-u)[[:upper:]]"),
Expr::ClassBytes(class(UPPER).to_byte_class()));
}
Expand Down Expand Up @@ -2270,12 +2268,9 @@ mod tests {

#[test]
fn ascii_classes_case_fold() {
assert_eq!(p("(?i)[:upper:]"), Expr::Class(class(UPPER).case_fold()));
assert_eq!(p("(?i)[[:upper:]]"),
Expr::Class(class(UPPER).case_fold()));

assert_eq!(pb("(?i-u)[:upper:]"),
Expr::Class(class(UPPER).case_fold()));
assert_eq!(pb("(?i-u)[[:upper:]]"),
Expr::ClassBytes(class(UPPER).to_byte_class().case_fold()));
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@
//! </pre>
//!
//! Any named character class may appear inside a bracketed `[...]` character
//! class. For example, `[\p{Greek}\pN]` matches any Greek or numeral
//! character.
//! class. For example, `[\p{Greek}[:digit:]]` matches any Greek or ASCII
//! digit.
//!
//! ## Composites
//!
Expand Down

0 comments on commit bc06024

Please sign in to comment.