Repository navigation
regex: accept literal [ in bracket expressions #439
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -304,10 +304,9 @@ fn parse_character_class( | |
|
|
||
| continue; | ||
| } | ||
| // Not a POSIX construct — treat as literal | ||
| // Not a POSIX construct: '[' is literal, and the next character | ||
| // may still terminate or otherwise participate in the class. | ||
| result.push(b'['); | ||
|
Contributor
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. parse_regex is the only caller, so could we push |
||
| result.push(line.current_byte()); | ||
| line.advance(); | ||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -965,6 +964,38 @@ mod tests { | |
| assert_eq!(result, b"[^]abc]"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_literal_open_bracket() { | ||
|
Contributor
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. these 4 tests are almost the same, could be a single one with a loop, no? |
||
| let mut line = char_provider_from("[[]"); | ||
| let lines = test_lines(); | ||
| let result = parse_character_class(&lines, &mut line, CharacterMode::Utf8).unwrap(); | ||
| assert_eq!(result, b"[[]"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_negated_literal_open_bracket() { | ||
| let mut line = char_provider_from("[^[]"); | ||
| let lines = test_lines(); | ||
| let result = parse_character_class(&lines, &mut line, CharacterMode::Utf8).unwrap(); | ||
| assert_eq!(result, b"[^[]"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_literal_open_bracket_in_class() { | ||
| let mut line = char_provider_from("[a[b]"); | ||
| let lines = test_lines(); | ||
| let result = parse_character_class(&lines, &mut line, CharacterMode::Utf8).unwrap(); | ||
| assert_eq!(result, b"[a[b]"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_negated_literal_open_bracket_in_class() { | ||
| let mut line = char_provider_from("[^a[b]"); | ||
| let lines = test_lines(); | ||
| let result = parse_character_class(&lines, &mut line, CharacterMode::Utf8).unwrap(); | ||
| assert_eq!(result, b"[^a[b]"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_escaped_character_begin() { | ||
| let mut line = char_provider_from("[\\nabc]"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -568,6 +568,23 @@ fn subst_multiline_flag_matches_embedded_line_end() { | |
| .stdout_is("foX\nbaX\n"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_subst_literal_open_bracket_in_character_classes() { | ||
| for (script, input, expected) in [ | ||
| (r"s/[[]/X/", "x\n", "x\n"), | ||
| (r"s/[^[]/X/", "x\n", "X\n"), | ||
| (r"s/[a[b]/X/", "x\n", "x\n"), | ||
| (r"s/[^a[b]/X/", "x\n", "X\n"), | ||
|
Contributor
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. please add the same cases without -E too (BRE path goes through bre_to_ere) |
||
| (r"s/\[[a]/X/", "[a\n", "X\n"), | ||
| ] { | ||
| new_ucmd!() | ||
| .args(&["-E", script]) | ||
| .pipe_in(input) | ||
| .succeeds() | ||
| .stdout_is(expected); | ||
| } | ||
| } | ||
|
|
||
| // Check appropriate selection and behavior of fast_Regex matcher | ||
| // Literal matcher | ||
| check_output!(subst_literal_start, ["-e", r"s/^l1/L1/", LINES1]); | ||
|
|
||
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.
Well it is a bit long now :/
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.
Oh, sorry, ok.
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.
How is this