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/12/31 16:09:18 UTC

[1/2] groovy git commit: Refine `GString.plus` for better performance

Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X 0c67ddc42 -> fa3ac273e


Refine `GString.plus` for better performance

(cherry picked from commit beee076)


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

Branch: refs/heads/GROOVY_2_6_X
Commit: 7a34c59118d610ca2646c8e518a8310376fd5f35
Parents: 0c67ddc
Author: sunlan <su...@apache.org>
Authored: Sun Dec 31 22:47:43 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon Jan 1 00:08:53 2018 +0800

----------------------------------------------------------------------
 src/main/groovy/groovy/lang/GString.java | 45 ++++++++++++++++-----------
 1 file changed, 27 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/7a34c591/src/main/groovy/groovy/lang/GString.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/groovy/lang/GString.java b/src/main/groovy/groovy/lang/GString.java
index 8323bf2..54ae7d3 100644
--- a/src/main/groovy/groovy/lang/GString.java
+++ b/src/main/groovy/groovy/lang/GString.java
@@ -27,9 +27,6 @@ import java.io.Serializable;
 import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
 import java.util.regex.Pattern;
 
 /**
@@ -94,28 +91,40 @@ public abstract class GString extends GroovyObjectSupport implements Comparable,
     }
 
     public GString plus(GString that) {
-        List<String> stringList = new ArrayList<String>(Arrays.asList(getStrings()));
-        List<Object> valueList = new ArrayList<Object>(Arrays.asList(getValues()));
+        Object[] values = getValues();
 
-        List<String> thatStrings = Arrays.asList(that.getStrings());
+        return new GStringImpl(appendValues(values, that.getValues()), appendStrings(getStrings(), that.getStrings(), values.length));
+    }
+
+    private String[] appendStrings(String[] strings, String[] thatStrings, int valuesLength) {
+        int stringsLength = strings.length;
+        boolean isStringsLonger = stringsLength > valuesLength;
+        int lastIndexOfStrings = stringsLength - 1;
+        int thatStringsLength = isStringsLonger ? thatStrings.length - 1 : thatStrings.length;
+
+        String[] newStrings = new String[stringsLength + thatStringsLength];
+        System.arraycopy(strings, 0, newStrings, 0, stringsLength);
 
-        int stringListSize = stringList.size();
-        if (stringListSize > valueList.size()) {
-            thatStrings = new ArrayList<String>(thatStrings);
+        if (isStringsLonger) {
             // merge onto end of previous GString to avoid an empty bridging value
-            int lastIndexOfStringList = stringListSize - 1;
-            String s = stringList.get(lastIndexOfStringList);
-            s += thatStrings.remove(0);
-            stringList.set(lastIndexOfStringList, s);
+            System.arraycopy(thatStrings, 1, newStrings, stringsLength, thatStringsLength);
+            newStrings[lastIndexOfStrings] = strings[lastIndexOfStrings] + thatStrings[0];
+        } else {
+            System.arraycopy(thatStrings, 0, newStrings, stringsLength, thatStringsLength);
         }
 
-        stringList.addAll(thatStrings);
-        valueList.addAll(Arrays.asList(that.getValues()));
+        return newStrings;
+    }
+
+    private Object[] appendValues(Object[] values, Object[] thatValues) {
+        int valuesLength = values.length;
+        int thatValuesLength = thatValues.length;
 
-        final String[] newStrings = stringList.toArray(EMPTY_STRING_ARRAY);
-        final Object[] newValues = valueList.toArray();
+        Object[] newValues = new Object[valuesLength + thatValuesLength];
+        System.arraycopy(values, 0, newValues, 0, valuesLength);
+        System.arraycopy(thatValues, 0, newValues, valuesLength, thatValuesLength);
 
-        return new GStringImpl(newValues, newStrings);
+        return newValues;
     }
 
     public GString plus(String that) {


[2/2] groovy git commit: Add DGM `startsWith(String...)` and `endsWith(String...)`

Posted by su...@apache.org.
Add DGM `startsWith(String...)` and `endsWith(String...)`

(cherry picked from commit ff220c6)


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

Branch: refs/heads/GROOVY_2_6_X
Commit: fa3ac273e7dc1ea2379737dc2b40d81ce720c076
Parents: 7a34c59
Author: sunlan <su...@apache.org>
Authored: Sun Dec 31 23:46:56 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon Jan 1 00:08:57 2018 +0800

----------------------------------------------------------------------
 .../groovy/runtime/StringGroovyMethods.java     | 33 ++++++++++++++++++++
 .../groovy/runtime/StringGroovyMethodsTest.java | 10 ++++++
 2 files changed, 43 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/fa3ac273/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
index c47bb7f..1738695 100644
--- a/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
@@ -3797,4 +3797,37 @@ public class StringGroovyMethods extends DefaultGroovyMethodsSupport {
         return unexpandLine((CharSequence) self, tabStop);
     }
 
+    /**
+     * Tests if this string starts with any specified prefixes.
+     *
+     * @param   prefixes   the prefixes.
+     * @return  {@code true} if this string starts with any specified prefixes.
+     * @since   2.4.14
+     */
+    public static boolean startsWith(String self, String... prefixes) {
+        for (String prefix : prefixes) {
+            if (self.startsWith(prefix)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    /**
+     * Tests if this string ends with any specified suffixes.
+     *
+     * @param   suffixes   the suffixes.
+     * @return  {@code true} if this string ends with any specified suffixes
+     * @since   2.4.14
+     */
+    public static boolean endsWith(String self, String... suffixes) {
+        for (String suffix : suffixes) {
+            if (self.endsWith(suffix)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/fa3ac273/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java b/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java
index c61f314..bd0812d 100644
--- a/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java
+++ b/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java
@@ -91,4 +91,14 @@ public class StringGroovyMethodsTest extends GroovyTestCase {
         assertTrue(StringGroovyMethods.isNumber(floatStr));
         assertFalse(StringGroovyMethods.isNumber(nonNumberStr));
     }
+
+    public void testStartsWith() {
+        assertTrue(StringGroovyMethods.startsWith("abcd", "ab", "ef"));
+        assertFalse(StringGroovyMethods.startsWith("abcd", "ef", "gh"));
+    }
+
+    public void testEndsWith() {
+        assertTrue(StringGroovyMethods.endsWith("abcd", "cd", "ef"));
+        assertFalse(StringGroovyMethods.endsWith("abcd", "ef", "gh"));
+    }
 }