This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 2cedccfd9dab37c371b5eb746012f82a9d865ff3
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Dec 14 23:24:04 2018 +1000

    GROOVY-8927: String variants for take and drop
---
 .../groovy/runtime/StringGroovyMethods.java        | 33 ++++++++++++++++++++--
 .../groovy/GroovyCharSequenceMethodsTest.groovy    | 12 ++++++++
 2 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java 
b/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
index 3f55c58..ddca930 100644
--- a/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
@@ -514,11 +514,25 @@ public class StringGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @param num the number of characters to drop from this GString
      * @return a String consisting of all characters except the first 
<code>num</code> ones,
      *         or else an empty String, if the toString() of this GString has 
less than <code>num</code> characters.
-     * @see #drop(CharSequence, int)
+     * @see #drop(String, int)
      * @since 2.3.7
      */
     public static String drop(GString self, int num) {
-        return drop(self.toString(), num).toString();
+        return drop(self.toString(), num);
+    }
+
+    /**
+     * A String variant of the equivalent CharSequence method.
+     *
+     * @param self the original String
+     * @param num the number of characters to drop from this String
+     * @return a String consisting of all characters except the first 
<code>num</code> ones,
+     *         or else an empty String, if the String has less than 
<code>num</code> characters.
+     * @see #drop(CharSequence, int)
+     * @since 2.5.5
+     */
+    public static String drop(String self, int num) {
+        return (String) drop((CharSequence) self, num);
     }
 
     /**
@@ -3323,7 +3337,20 @@ public class StringGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 2.3.7
      */
     public static String take(GString self, int num) {
-        return (String) take(self.toString(), num);
+        return take(self.toString(), num);
+    }
+
+    /**
+     * A String variant of the equivalent CharSequence method.
+     *
+     * @param self the original String
+     * @param num  the number of chars to take from this String
+     * @return a String consisting of the first <code>num</code> chars,
+     *         or else the whole String if it has less then <code>num</code> 
elements.
+     * @since 2.5.5
+     */
+    public static String take(String self, int num) {
+        return (String) take((CharSequence) self, num);
     }
 
     /**
diff --git a/src/test/groovy/GroovyCharSequenceMethodsTest.groovy 
b/src/test/groovy/GroovyCharSequenceMethodsTest.groovy
index 3554e8d..f4111ca 100644
--- a/src/test/groovy/GroovyCharSequenceMethodsTest.groovy
+++ b/src/test/groovy/GroovyCharSequenceMethodsTest.groovy
@@ -132,6 +132,18 @@ class GroovyCharSequenceMethodsTest extends GroovyTestCase 
{
         assert cs2.drop(3) == 'bar'
     }
 
+    void testDropTakeTC() {
+        assertScript '''
+            @groovy.transform.TypeChecked
+            def method() {
+                assert 'Foo Bar'.drop(4).toLowerCase() == 'bar'
+                assert 'Foo Bar'.take(3).toLowerCase() == 'foo'
+            }
+
+            method()
+        '''
+    }
+
     void testAsBoolean() {
         assert cs1 && cs2
         assert !csEmpty

Reply via email to