GROOVY-7472 add uncapitalize() to SGM, applied to CharSequence
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/9c9dbb37 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/9c9dbb37 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/9c9dbb37 Branch: refs/heads/GROOVY_2_4_X Commit: 9c9dbb375a336f34ad16c2db11b20a555e79b226 Parents: f302996 Author: aalmiray <[email protected]> Authored: Fri Jun 19 17:41:00 2015 +0200 Committer: paulk <[email protected]> Committed: Fri Dec 30 06:56:24 2016 +1000 ---------------------------------------------------------------------- .../groovy/runtime/StringGroovyMethods.java | 54 ++++++++------------ .../groovy/GroovyCharSequenceMethodsTest.groovy | 6 +++ 2 files changed, 28 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/9c9dbb37/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 f4af313..1c9941c 100644 --- a/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java +++ b/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java @@ -66,38 +66,6 @@ import static org.codehaus.groovy.runtime.DefaultGroovyMethods.each; * at the Java method call level. I.e. future versions of Groovy may * remove or move a method call in this file but would normally * aim to keep the method available from within Groovy. -<<<<<<< HEAD - * - * @author <a href="mailto:[email protected]">James Strachan</a> - * @author Jeremy Rayner - * @author Sam Pullara - * @author Rod Cope - * @author Guillaume Laforge - * @author John Wilson - * @author Hein Meling - * @author Dierk Koenig - * @author Pilho Kim - * @author Marc Guillemot - * @author Russel Winder - * @author bing ran - * @author Jochen Theodorou - * @author Paul King - * @author Michael Baehr - * @author Joachim Baumann - * @author Alex Tkachman - * @author Ted Naleid - * @author Brad Long - * @author Jim Jagielski - * @author Rodolfo Velasco - * @author jeremi Joslin - * @author Hamlet D'Arcy - * @author Cedric Champeau - * @author Tim Yates - * @author Dinko Srkoc - * @author Pascal Lombard - * @author Christophe Charles -======= ->>>>>>> 35b5f34... GROOVY-6950: StringGroovyMethods minor performance improvements (make use of line based iterator) */ public class StringGroovyMethods extends DefaultGroovyMethodsSupport { @@ -240,6 +208,28 @@ public class StringGroovyMethods extends DefaultGroovyMethodsSupport { } /** + * Convenience method to uncapitalize the first letter of a CharSequence + * (typically the first letter of a word). Example usage: + * <pre class="groovyTestCase"> + * assert 'H'.uncapitalize() == 'h' + * assert 'Hello'.uncapitalize() == 'hello' + * assert 'Hello world'.uncapitalize() == 'hello world' + * assert 'Hello World'.uncapitalize() == 'hello World' + * assert 'Hello World' == + * 'hello world'.split(' ').collect{ it.uncapitalize() }.join(' ') + * </pre> + * + * @param self The CharSequence to uncapitalize + * @return A String containing the uncapitalized toString() of the CharSequence + * @since 2.5.0 + */ + public static String uncapitalize(CharSequence self) { + String s = self.toString(); + if (s == null || s.length() == 0) return s; + return Character.toLowerCase(s.charAt(0)) + s.substring(1); + } + + /** * Convenience method to capitalize the first letter of a CharSequence * (typically the first letter of a word). Example usage: * <pre class="groovyTestCase"> http://git-wip-us.apache.org/repos/asf/groovy/blob/9c9dbb37/src/test/groovy/GroovyCharSequenceMethodsTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/GroovyCharSequenceMethodsTest.groovy b/src/test/groovy/GroovyCharSequenceMethodsTest.groovy index d37999e..3228f29 100644 --- a/src/test/groovy/GroovyCharSequenceMethodsTest.groovy +++ b/src/test/groovy/GroovyCharSequenceMethodsTest.groovy @@ -165,6 +165,12 @@ class GroovyCharSequenceMethodsTest extends GroovyTestCase { assert cs2.capitalize() == 'Foobar' } + void testUncapitalize() { + def csfoo = [toString:{->'Foo'}] as CharSequence + assert csfoo.uncapitalize() == 'foo' + assert cs2.uncapitalize() == 'foobar' + } + void testExpand() { def csfoobar = makeCharSequence('foo\tbar') assert csfoobar.expand() == 'foo bar'
