Skip to content

Commit 01395ba

Browse files
Fix all the suggestions
1 parent bceab07 commit 01395ba

File tree

2 files changed

+26
-52
lines changed

2 files changed

+26
-52
lines changed

ballerina/natives.bal

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,25 @@ public isolated function matches(string stringToMatch, string regex) returns boo
3838
# + originalString - The original string to replace the first occurrence of the
3939
# substring that matches the provided regex
4040
# + regex - The regex to match the first substring in the `originalString` to be replaced
41-
# + replacement - The replacement string or A function to be invoked to create the new substring to be used to replace the first match to the given regex
41+
# + replacement - The replacement string or A function to be invoked to create the new substring to be
42+
# used to replace the first match to the given regex
4243
# + startIndex - The starting index for the search
4344
# + return - The resultant string with the replaced substring
4445
public isolated function replace(string originalString, string regex, Replacement replacement,
4546
int startIndex = 0) returns string {
46-
string extractedString = getSubstring(originalString, startIndex);
47-
string|error replacementString = getReplacementString(originalString, regex, replacement, startIndex);
48-
if replacementString is error {
47+
Match? matched = search(originalString, regex, startIndex);
48+
if (matched is null) {
4949
return originalString;
5050
}
51-
handle|error value = trap replaceFirstExternal(java:fromString(extractedString), java:fromString(regex),
52-
java:fromString(replacementString));
53-
if value is handle {
54-
string? updatedString = java:toString(value);
55-
if updatedString is string {
56-
return strings:substring(originalString, 0, startIndex) + updatedString;
57-
}
51+
int index = 0;
52+
int length = originalString.length();
53+
string updatedString = strings:substring(originalString, index, matched.startIndex) +
54+
getReplacementString(matched, replacement);
55+
index = matched.endIndex;
56+
if (index < length) {
57+
updatedString += strings:substring(originalString, index, length);
5858
}
59-
return originalString;
59+
return updatedString;
6060
}
6161

6262
# Replaces each occurrence of the substrings, which match the provided
@@ -73,30 +73,21 @@ public isolated function replace(string originalString, string regex, Replacemen
7373
# match the regex
7474
# + return - The resultant string with the replaced substrings
7575
public isolated function replaceAll(string originalString, string regex, Replacement replacement) returns string {
76-
if (replacement is ReplacerFunction) {
77-
string updatedString = "";
78-
int startIndex = 0;
79-
Match[] matchedArray = searchAll(originalString, regex);
80-
foreach Match matched in matchedArray {
81-
updatedString += strings:substring(originalString, startIndex, matched.startIndex) +
82-
getStringFromReplacerFunction(matched, replacement);
83-
startIndex = matched.endIndex;
84-
}
85-
if (startIndex < originalString.length()) {
86-
updatedString += strings:substring(originalString, startIndex, originalString.length());
87-
}
88-
return updatedString;
89-
} else {
90-
handle|error value = trap replaceAllExternal(java:fromString(originalString), java:fromString(regex),
91-
java:fromString(replacement));
92-
if value is handle {
93-
string? updatedString = java:toString(value);
94-
if updatedString is string {
95-
return updatedString;
96-
}
97-
}
76+
Match[] matchedArray = searchAll(originalString, regex);
77+
if matchedArray.length() == 0 {
9878
return originalString;
9979
}
80+
string updatedString = "";
81+
int startIndex = 0;
82+
foreach Match matched in matchedArray {
83+
updatedString += strings:substring(originalString, startIndex, matched.startIndex) +
84+
getReplacementString(matched, replacement);
85+
startIndex = matched.endIndex;
86+
}
87+
if (startIndex < originalString.length()) {
88+
updatedString += strings:substring(originalString, startIndex, originalString.length());
89+
}
90+
return updatedString;
10091
}
10192

10293
# Replaces the first substring that matches the given regex with
@@ -207,12 +198,6 @@ isolated function matchesExternal(handle stringToMatch, handle regex) returns bo
207198
paramTypes: ["java.lang.String"]
208199
} external;
209200

210-
isolated function replaceAllExternal(handle originalString, handle regex, handle replacement) returns handle = @java:Method {
211-
name: "replaceAll",
212-
'class: "java.lang.String",
213-
paramTypes: ["java.lang.String", "java.lang.String"]
214-
} external;
215-
216201
isolated function replaceFirstExternal(handle originalString, handle regex, handle replacement) returns handle = @java:Method {
217202
name: "replaceFirst",
218203
'class: "java.lang.String",

ballerina/utils.bal

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,10 @@ isolated function getSubstring(string str, int startIndex) returns string {
2424
return strings:substring(str, startIndex, str.length());
2525
}
2626

27-
isolated function getReplacementString(string str, string regex, Replacement replacement, int startIndex = 0)
28-
returns string|error {
27+
isolated function getReplacementString(Match matched, Replacement replacement) returns string {
2928
if replacement is string {
3029
return replacement;
31-
} else {
32-
Match?|error matched = trap search(str, regex, startIndex);
33-
if matched is Match {
34-
return replacement(matched);
35-
} else {
36-
return error("There is no matching substrig in the given string.");
37-
}
3830
}
39-
}
40-
41-
isolated function getStringFromReplacerFunction(Match matched, ReplacerFunction replacement) returns string {
4231
return replacement(matched);
4332
}
4433

0 commit comments

Comments
 (0)