Skip to content

Commit 16bc36b

Browse files
committed
expr: Handle '$' at the end of a capturing group
1 parent a2d68ec commit 16bc36b

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/uu/expr/src/syntax_tree.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl StringOp {
156156
re_string.push('^');
157157

158158
// Handle first character from the input pattern
159-
let mut pattern_chars = right.chars();
159+
let mut pattern_chars = right.chars().peekable();
160160
let first = pattern_chars.next();
161161
match first {
162162
Some('^') => {} // Start of string anchor is already added
@@ -169,7 +169,7 @@ impl StringOp {
169169
// Escaped previous character should not affect the current.
170170
let mut prev = first.unwrap_or_default();
171171
let mut prev_is_escaped = false;
172-
for curr in pattern_chars {
172+
while let Some(curr) = pattern_chars.next() {
173173
match curr {
174174
'^' => match (prev, prev_is_escaped) {
175175
// Start of a capturing group
@@ -183,7 +183,15 @@ impl StringOp {
183183
_ => re_string.push_str(r"\^"),
184184
},
185185
'$' => {
186-
if prev_is_escaped || prev != '\\' {
186+
if let Some('\\') = pattern_chars.peek() {
187+
let backslash = pattern_chars.next().unwrap_or_default();
188+
match pattern_chars.peek() {
189+
// End of a capturing group
190+
Some(')') => re_string.push('$'),
191+
_ => re_string.push_str(r"\$"),
192+
}
193+
re_string.push(backslash);
194+
} else if prev_is_escaped || prev != '\\' {
187195
re_string.push_str(r"\$");
188196
} else {
189197
re_string.push('$');

0 commit comments

Comments
 (0)