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:17:23 UTC

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

Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X 6ea3da47e -> 1bd694e8c


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/f0eb637d
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/f0eb637d
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/f0eb637d

Branch: refs/heads/GROOVY_2_4_X
Commit: f0eb637dc7fcd9a73cac29140a5aae33e856ea9b
Parents: 6ea3da4
Author: sunlan <su...@apache.org>
Authored: Sun Dec 31 23:46:56 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon Jan 1 00:10:25 2018 +0800

----------------------------------------------------------------------
 .../codehaus/groovy/runtime/StringGroovyMethodsTest.java  | 10 ++++++++++
 1 file changed, 10 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/f0eb637d/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"));
+    }
 }


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

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


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

Branch: refs/heads/GROOVY_2_4_X
Commit: 1bd694e8c134dac851dfd90e83f7f2889603f2c0
Parents: 2b382c9
Author: sunlan <su...@apache.org>
Authored: Mon Jan 1 00:12:32 2018 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon Jan 1 00:16:29 2018 +0800

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


http://git-wip-us.apache.org/repos/asf/groovy/blob/1bd694e8/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java b/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java
index 2dd1335..e2fa5e2 100644
--- a/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java
@@ -3634,4 +3634,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/1bd694e8/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 bd0812d..af5dd0c 100644
--- a/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java
+++ b/src/test/org/codehaus/groovy/runtime/StringGroovyMethodsTest.java
@@ -101,4 +101,5 @@ public class StringGroovyMethodsTest extends GroovyTestCase {
         assertTrue(StringGroovyMethods.endsWith("abcd", "cd", "ef"));
         assertFalse(StringGroovyMethods.endsWith("abcd", "ef", "gh"));
     }
+
 }


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

Posted by su...@apache.org.
Refine `GString.plus` for better performance


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

Branch: refs/heads/GROOVY_2_4_X
Commit: 2b382c9a857ddfc5b92793cd2ecb7431c296face
Parents: f0eb637
Author: sunlan <su...@apache.org>
Authored: Mon Jan 1 00:11:51 2018 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon Jan 1 00:11:51 2018 +0800

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


http://git-wip-us.apache.org/repos/asf/groovy/blob/2b382c9a/src/main/groovy/lang/GString.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/GString.java b/src/main/groovy/lang/GString.java
index cd88a2b..42a1e43 100644
--- a/src/main/groovy/lang/GString.java
+++ b/src/main/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) {