You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2017/09/14 04:28:45 UTC
groovy git commit: Simplify the `replace` method
Repository: groovy
Updated Branches:
refs/heads/master 9d681bcd4 -> fe7a0391e
Simplify the `replace` method
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/fe7a0391
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/fe7a0391
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/fe7a0391
Branch: refs/heads/master
Commit: fe7a0391e5d51142158e0723c60afff930f57e95
Parents: 9d681bc
Author: sunlan <su...@apache.org>
Authored: Thu Sep 14 12:28:30 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Thu Sep 14 12:28:41 2017 +0800
----------------------------------------------------------------------
.../groovy/parser/antlr4/util/StringUtils.java | 94 ++------------------
1 file changed, 6 insertions(+), 88 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/fe7a0391/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java
index 02b09d4..cc1f2b7 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java
@@ -151,9 +151,8 @@ public class StringUtils {
return length == quotationLength << 1 ? "" : text.substring(quotationLength, length - quotationLength);
}
-
/**
- * Copied from Apache commons-lang3-3.6
+ * The modified implementation is based on StringUtils#replace(String text, String searchString, String replacement, int max), Apache commons-lang3-3.6
*
* <p>Replaces all occurrences of a String within another String.</p>
*
@@ -170,110 +169,29 @@ public class StringUtils {
* StringUtils.replace("aba", "a", "z") = "zbz"
* </pre>
*
- * @see #replace(String text, String searchString, String replacement, int max)
- * @param text text to search and replace in, may be null
- * @param searchString the String to search for, may be null
- * @param replacement the String to replace it with, may be null
- * @return the text with any replacements processed,
- * {@code null} if null String input
- */
- public static String replace(final String text, final String searchString, final String replacement) {
- return replace(text, searchString, replacement, -1);
- }
-
- /**
- * Copied from Apache commons-lang3-3.6
- *
- * <p>Replaces a String with another String inside a larger String,
- * for the first {@code max} values of the search String.</p>
- *
- * <p>A {@code null} reference passed to this method is a no-op.</p>
- *
- * <pre>
- * StringUtils.replace(null, *, *, *) = null
- * StringUtils.replace("", *, *, *) = ""
- * StringUtils.replace("any", null, *, *) = "any"
- * StringUtils.replace("any", *, null, *) = "any"
- * StringUtils.replace("any", "", *, *) = "any"
- * StringUtils.replace("any", *, *, 0) = "any"
- * StringUtils.replace("abaa", "a", null, -1) = "abaa"
- * StringUtils.replace("abaa", "a", "", -1) = "b"
- * StringUtils.replace("abaa", "a", "z", 0) = "abaa"
- * StringUtils.replace("abaa", "a", "z", 1) = "zbaa"
- * StringUtils.replace("abaa", "a", "z", 2) = "zbza"
- * StringUtils.replace("abaa", "a", "z", -1) = "zbzz"
- * </pre>
- *
* @param text text to search and replace in, may be null
* @param searchString the String to search for, may be null
* @param replacement the String to replace it with, may be null
- * @param max maximum number of values to replace, or {@code -1} if no maximum
* @return the text with any replacements processed,
* {@code null} if null String input
*/
- public static String replace(final String text, final String searchString, final String replacement, final int max) {
- return replace(text, searchString, replacement, max, false);
- }
-
- /**
- * Copied from Apache commons-lang3-3.6
- *
- * <p>Replaces a String with another String inside a larger String,
- * for the first {@code max} values of the search String,
- * case sensitively/insensisitively based on {@code ignoreCase} value.</p>
- *
- * <p>A {@code null} reference passed to this method is a no-op.</p>
- *
- * <pre>
- * StringUtils.replace(null, *, *, *, false) = null
- * StringUtils.replace("", *, *, *, false) = ""
- * StringUtils.replace("any", null, *, *, false) = "any"
- * StringUtils.replace("any", *, null, *, false) = "any"
- * StringUtils.replace("any", "", *, *, false) = "any"
- * StringUtils.replace("any", *, *, 0, false) = "any"
- * StringUtils.replace("abaa", "a", null, -1, false) = "abaa"
- * StringUtils.replace("abaa", "a", "", -1, false) = "b"
- * StringUtils.replace("abaa", "a", "z", 0, false) = "abaa"
- * StringUtils.replace("abaa", "A", "z", 1, false) = "abaa"
- * StringUtils.replace("abaa", "A", "z", 1, true) = "zbaa"
- * StringUtils.replace("abAa", "a", "z", 2, true) = "zbza"
- * StringUtils.replace("abAa", "a", "z", -1, true) = "zbzz"
- * </pre>
- *
- * @param text text to search and replace in, may be null
- * @param searchString the String to search for (case insensitive), may be null
- * @param replacement the String to replace it with, may be null
- * @param max maximum number of values to replace, or {@code -1} if no maximum
- * @param ignoreCase if true replace is case insensitive, otherwise case sensitive
- * @return the text with any replacements processed,
- * {@code null} if null String input
- */
- private static String replace(final String text, String searchString, final String replacement, int max, final boolean ignoreCase) {
- if (isEmpty(text) || isEmpty(searchString) || replacement == null || max == 0) {
+ public static String replace(final String text, String searchString, final String replacement) {
+ if (isEmpty(text) || isEmpty(searchString) || replacement == null) {
return text;
}
- String searchText = text;
- if (ignoreCase) {
- searchText = text.toLowerCase();
- searchString = searchString.toLowerCase();
- }
int start = 0;
- int end = searchText.indexOf(searchString, start);
+ int end = text.indexOf(searchString, start);
if (end == INDEX_NOT_FOUND) {
return text;
}
final int replLength = searchString.length();
int increase = replacement.length() - replLength;
- increase = increase < 0 ? 0 : increase;
- increase *= max < 0 ? 16 : max > 64 ? 64 : max;
+ increase = (increase < 0 ? 0 : increase) * 16;
final StringBuilder buf = new StringBuilder(text.length() + increase);
while (end != INDEX_NOT_FOUND) {
buf.append(text.substring(start, end)).append(replacement);
start = end + replLength;
- if (--max == 0) {
- break;
- }
- end = searchText.indexOf(searchString, start);
+ end = text.indexOf(searchString, start);
}
buf.append(text.substring(start));
return buf.toString();