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:19 UTC

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

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"));
+    }
 }