Compare commits

...

15 commits

Author SHA1 Message Date
4105928b9a merge commit for archive created by Sapling 2024-05-29 17:26:43 +01:00
3f21759c47 fix: infinite loop when using .* patterns
Summary:

When we are using `.*` patterns, this causes an infinite loop. What happens is
we are adding text to the end and then replacing that. Its only when we are
matching the end of the output string.

We can detect this when the starting point is the same as the end point and the
match is "" (an empty string). If we hit this condition we can get out and
return the output that has been replaced.

Test Plan:

Test in CI
2024-05-29 17:26:26 +01:00
e3f52b573b merge commit for archive created by Sapling 2024-05-11 21:45:31 +01:00
817c994da8 fix: handle invalid regex patterns by returning the input string
Right now we don't want to be panicking if the user provides an invalid regex.
We also don't really want to be throwing or returning an error, this will mess
with any live preview that is going on in external tools.

We should return the input and let any preview display the text. This will
happen if the user is doing some preview as you type kind of thing.
2024-05-11 21:45:24 +01:00
ea6547861a
Merge 9fc7a737eb into sapling-pr-archive-AdeAttwood 2024-05-11 21:44:22 +01:00
832428dbf1 feat: add input argument to read input from text or stdin
You can now provide input as the content you want to search and replace in. If
you don't provide input, the program will read from stdin.
2024-05-11 21:44:16 +01:00
9fc7a737eb fix: handle invalid regex patterns by returning the input string
Right now we don't want to be panicking if the user provides an invalid regex.
We also don't really want to be throwing or returning an error, this will mess
with any live preview that is going on in external tools.

We should return the input and let any preview display the text. This will
happen if the user is doing some preview as you type kind of thing.
2024-05-11 21:44:16 +01:00
89889def72
Merge dc5fbc4026 into sapling-pr-archive-AdeAttwood 2024-05-11 21:43:53 +01:00
dc5fbc4026 fix: handle invalid regex patterns by returning the input string
Right now we don't want to be panicking if the user provides an invalid regex.
We also don't really want to be throwing or returning an error, this will mess
with any live preview that is going on in external tools.

We should return the input and let any preview display the text. This will
happen if the user is doing some preview as you type kind of thing.
2024-05-11 21:43:31 +01:00
568e618106 feat: add input argument to read input from text or stdin
You can now provide input as the content you want to search and replace in. If
you don't provide input, the program will read from stdin.
2024-05-11 21:36:55 +01:00
2617363f4f
Merge bb03e09764 into sapling-pr-archive-AdeAttwood 2024-05-11 15:21:01 +01:00
bb03e09764 feat: add case preserving search and replace 2024-05-11 15:20:43 +01:00
fa9e139e36
Merge 08d96f8ea4 into sapling-pr-archive-AdeAttwood 2024-05-11 15:16:28 +01:00
08d96f8ea4 feat: add case preserving search and replace 2024-05-11 15:16:19 +01:00
d0b4ad2046 test: add regex feature tests 2024-05-11 10:39:36 +01:00
2 changed files with 19 additions and 1 deletions

View file

@ -26,6 +26,12 @@ pub fn replace(search: &String, replace: String, input: String) -> String {
let start = search_match.start();
let end = search_match.end();
// Prevent an infinite loop. If we hit this condition we will keep adding a new replacement
// to the end.
if start == end {
break;
}
let mut replacement = String::new();
match search_pattern.captures(&output[start..end]) {
Some(captures) => {

View file

@ -22,4 +22,16 @@ Feature: Regex search and replace
Given Search is '(\w+'
And Replace is 'new'
And Input is 'this is a'
Then Output is 'this is a'
Then Output is 'this is a'
Scenario: You can replace a pattern with a dot in it like a css class name
Given Search is '.testing'
And Replace is '.another'
And Input is '.testing {'
Then Output is '.another {'
Scenario: You can replace a pattern that grabs all the text
Given Search is '.*'
And Replace is '.another'
And Input is '.testing {'
Then Output is '.another'