groovy git commit: Use "replace" method from commons-lang3 to improve performance

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

groovy git commit: Use "replace" method from commons-lang3 to improve performance

Daniel.Sun
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X 5db125714 -> 6e47e6df4


Use "replace" method from commons-lang3 to improve performance

(cherry picked from commit a4f779335c97d0aa0fe2f42351b13eedf48e6119)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/6e47e6df
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/6e47e6df
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/6e47e6df

Branch: refs/heads/GROOVY_2_6_X
Commit: 6e47e6df435ad590863e5e9ee1a8996143a841a1
Parents: 5db1257
Author: sunlan <[hidden email]>
Authored: Wed Sep 13 22:12:26 2017 +0800
Committer: sunlan <[hidden email]>
Committed: Wed Sep 13 22:12:26 2017 +0800

----------------------------------------------------------------------
 .../groovy/parser/antlr4/util/StringUtils.java  | 172 ++++++++++++++++++-
 1 file changed, 166 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/6e47e6df/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 49b3f3c..86c91e6 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
@@ -80,7 +80,7 @@ public class StringUtils {
  }
  });
 
- return result.replace("\\\\", "\\");
+ return replace(result,"\\\\", "\\");
  }
 
  public static final int NONE_SLASHY = 0;
@@ -93,12 +93,12 @@ public class StringUtils {
  text = StringUtils.replaceLineEscape(text);
 
  if (slashyType == SLASHY) {
- text = text.replace("\\/", "/");
+ text = replace(text,"\\/", "/");
  }
 
  if (slashyType == DOLLAR_SLASHY) {
- text = text.replace("$$", "$");
- text = text.replace("$/", "/");
+ text = replace(text,"$$", "$");
+ text = replace(text,"$/", "/");
  }
 
  } else if (slashyType == NONE_SLASHY) {
@@ -111,7 +111,7 @@ public class StringUtils {
  }
 
  private static String replaceEscapes(String text) {
- text = text.replace("\\$", "$");
+ text = replace(text,"\\$", "$");
 
  text = StringUtils.replaceLineEscape(text);
 
@@ -138,7 +138,7 @@ public class StringUtils {
  }
 
  public static String removeCR(String text) {
- return text.replace("\r\n", "\n");
+ return replace(text,"\r\n", "\n");
  }
 
  public static long countChar(String text, char c) {
@@ -155,4 +155,164 @@ public class StringUtils {
 
  return length == quotationLength << 1 ? "" : text.substring(quotationLength, length - quotationLength);
  }
+
+
+ /**
+ * Copied from Apache commons-lang3-3.6
+ *
+ * <p>Replaces all occurrences of a String within another 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("aba", "a", null)  = "aba"
+ * StringUtils.replace("aba", "a", "")    = "b"
+ * 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) {
+ return text;
+ }
+ String searchText = text;
+ if (ignoreCase) {
+ searchText = text.toLowerCase();
+ searchString = searchString.toLowerCase();
+ }
+ int start = 0;
+ int end = searchText.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;
+ 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);
+ }
+ buf.append(text.substring(start));
+ return buf.toString();
+ }
+
+ /**
+ * Copied from Apache commons-lang3-3.6
+ *
+ * <p>Checks if a CharSequence is empty ("") or null.</p>
+ *
+ * <pre>
+ * StringUtils.isEmpty(null)      = true
+ * StringUtils.isEmpty("")        = true
+ * StringUtils.isEmpty(" ")       = false
+ * StringUtils.isEmpty("bob")     = false
+ * StringUtils.isEmpty("  bob  ") = false
+ * </pre>
+ *
+ * <p>NOTE: This method changed in Lang version 2.0.
+ * It no longer trims the CharSequence.
+ * That functionality is available in isBlank().</p>
+ *
+ * @param cs  the CharSequence to check, may be null
+ * @return {@code true} if the CharSequence is empty or null
+ * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
+ */
+ public static boolean isEmpty(final CharSequence cs) {
+ return cs == null || cs.length() == 0;
+ }
+
+ /**
+ * Copied from Apache commons-lang3-3.6
+ *
+ * Represents a failed index search.
+ */
+ private static final int INDEX_NOT_FOUND = -1;
 }
\ No newline at end of file