|
| 1 | +# Proposal: Improve the replacement functions |
| 2 | + |
| 3 | +_Owners_: @daneshk @kalaiyarasiganeshalingam |
| 4 | +_Reviewers_: @daneshk |
| 5 | +_Created_: 2022/03/11 |
| 6 | +_Updated_: 2022/03/11 |
| 7 | +_Issues_: [#2772](https://github.com/ballerina-platform/ballerina-standard-library/issues/2772) |
| 8 | + |
| 9 | +## Summary |
| 10 | +The existing replacement functions don't support the following functionalities: |
| 11 | +- There is no way to replace the matches from the particular string index of the given string. |
| 12 | +- There isn't any mechanism to replace the matches with a dynamic value. |
| 13 | + |
| 14 | +So, this proposal improves the replacement functions to support the above functionalities. |
| 15 | + |
| 16 | +## Goals |
| 17 | +Provide a way to support the above functionalities. |
| 18 | + |
| 19 | +## Motivation |
| 20 | +This allows users to get the above functionalities when using replacement functions. |
| 21 | + |
| 22 | +## Description |
| 23 | +API improvements are: |
| 24 | + |
| 25 | +- Introduce a function to create the new substring to be used to replace the matches. |
| 26 | + ```ballerina |
| 27 | + # A function to be invoked to create the new substring to be used to replace the matches. |
| 28 | + type ReplacerFunction function(Match matched) returns string; |
| 29 | + ``` |
| 30 | +
|
| 31 | +- Introduce a new type to get the replacement value. |
| 32 | + ```ballerina |
| 33 | + # A function to be invoked to create the new substring or string value to be used to replace the matches. |
| 34 | + public type Replacement ReplacerFunction|string; |
| 35 | + ``` |
| 36 | +
|
| 37 | +- Change the type of replacement param from string to `Replacement` type. |
| 38 | + ```ballerina |
| 39 | + # Replaces each occurrence of the substrings, which match the provided |
| 40 | + # regex from the given original string value with the |
| 41 | + # provided replacement string. |
| 42 | + # ```ballerina |
| 43 | + # string result = regex:replaceAll("Ballerina is great", "\\s+", "_"); |
| 44 | + # ``` |
| 45 | + # |
| 46 | + # + originalString - The original string to replace the occurrences of the |
| 47 | + # substrings that match the provided regex |
| 48 | + # + regex - The regex to match the substrings in the `originalString` , which is to be replaced |
| 49 | + # + replacement - The replacement string or function to replace the substrings, which |
| 50 | + # match the regex |
| 51 | + # + return - The resultant string with the replaced substrings |
| 52 | + public isolated function replaceAll(string originalString, string regex, Replacement replacement) returns string; |
| 53 | + ``` |
| 54 | +
|
| 55 | +- Introduce a new function to replace the first substring from the given start index of the string. |
| 56 | + This is an improved function from the `replaceFirst`. |
| 57 | + ```ballerina |
| 58 | + # Replaces the first substring that matches the given regex with |
| 59 | + # the provided replacement string or function. |
| 60 | + # ```ballerina |
| 61 | + # string result = regex:replace("Ballerina is great", "\\s+", "_"); |
| 62 | + # ``` |
| 63 | + # |
| 64 | + # + originalString - The original string to replace the first occurrence of the |
| 65 | + # substring that matches the provided regex |
| 66 | + # + regex - The regex to match the first substring in the `originalString` to |
| 67 | + # be replaced |
| 68 | + # + replacement - The replacement string or function to replace the first substring, which |
| 69 | + # matches the regex |
| 70 | + # + startIndex - The starting index for the search |
| 71 | + # + return - The resultant string with the replaced substring |
| 72 | + public isolated function replace(string originalString, string regex, Replacement replacement, int startIndex = 0) |
| 73 | + returns string; |
| 74 | + ``` |
| 75 | + |
| 76 | +- Deprecate the existing `replaceFirst` function. |
| 77 | + ```ballerina |
| 78 | + # Replaces the first substring that matches the given regex with |
| 79 | + # the provided replacement string. |
| 80 | + # ```ballerina |
| 81 | + # string result = regex:replaceFirst("Ballerina is great", "\\s+", "_"); |
| 82 | + # ``` |
| 83 | + # |
| 84 | + # + originalString - The original string to replace the first occurrence of the |
| 85 | + # substring that matches the provided regex |
| 86 | + # + regex - The regex to match the first substring in the `originalString` to |
| 87 | + # be replaced |
| 88 | + # + replacement - The replacement string to replace the first substring, which |
| 89 | + # matches the regex |
| 90 | + # + return - The resultant string with the replaced substring |
| 91 | + public isolated function replaceFirst(string originalString, string regex, string replacement) returns string; |
| 92 | + ``` |
0 commit comments