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 15:47:04 UTC
groovy git commit: Add DGM `startsWith(String...)` and
`endsWith(String...)`
Repository: groovy
Updated Branches:
refs/heads/master beee076fe -> ff220c6e2
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/ff220c6e
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ff220c6e
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ff220c6e
Branch: refs/heads/master
Commit: ff220c6e2fce1342fc3111b995a43f81a1330b2c
Parents: beee076
Author: sunlan <su...@apache.org>
Authored: Sun Dec 31 23:46:56 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sun Dec 31 23:46:56 2017 +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/ff220c6e/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/ff220c6e/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"));
+ }
}