Skip to content

Commit

Permalink
Fix Regex.replace Full
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Apr 12, 2022
1 parent 0e5d0cd commit ea9240d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -559,11 +559,8 @@ type Pattern
internal_matcher = this.build_matcher input start end
internal_matcher.replaceAll replacement
Mode.Full ->
## TODO this is inconsistent with other modes - will not allow
capture group references in replacement. It should be revised
with: https://www.pivotaltracker.com/story/show/181524498
case this.match input mode=Mode.Full of
Match _ _ _ _ -> replacement
Match _ _ _ _ -> this.replace input replacement Mode.First
Nothing -> input
Matching_Mode.Last ->
all_matches = this.match input
Expand Down
9 changes: 6 additions & 3 deletions test/Tests/src/Data/Text/Default_Regex_Engine_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,11 @@ spec =
match = pattern.replace input "REPLACED" mode=Mode.Full
match . should_equal "REPLACED"

Test.specify "should correctly replace entire input in Full mode even if partial matches are possible" <|
pattern = engine.compile "(aa)+" []
pattern.replace "aaa" "REPLACED" mode=Mode.Full . should_equal "aaa"
pattern.replace "aaaa" "REPLACED" mode=Mode.Full . should_equal "REPLACED"

Test.specify "should return the input for a full replace if the pattern doesn't match the entire input" <|
pattern = engine.compile "(..)" []
input = "aa ab"
Expand Down Expand Up @@ -438,9 +443,7 @@ spec =
pattern.replace "foo bar, baz" "[${capture}]" mode=Mode.First . should_equal "[foo] bar, baz"
pattern.replace "foo bar, baz" "[${capture}]" mode=Matching_Mode.Last . should_equal "foo bar, [baz]"

## TODO this should be revised with:
https://www.pivotaltracker.com/story/show/181524498
Test.specify "should handle capture groups in replacement in All mode" pending="Regex needs revision." <|
Test.specify "should handle capture groups in replacement in All mode" <|
pattern = engine.compile "([a-z]+)" []
pattern.replace "foo bar, baz" "[$1]" mode=Mode.Full . should_equal "foo bar, baz"
pattern.replace "foo" "[$1]" mode=Mode.Full . should_equal "[foo]"
Expand Down

0 comments on commit ea9240d

Please sign in to comment.