From b4114cc59508efb5709ca6c36cef1520784b7a0b Mon Sep 17 00:00:00 2001 From: Laurent Le Brun Date: Wed, 26 Aug 2015 14:53:37 +0000 Subject: Add string.capitalize() -- MOS_MIGRATED_REVID=101575207 --- .../devtools/build/lib/syntax/MethodLibrary.java | 45 ++++++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) (limited to 'src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java') diff --git a/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java b/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java index e7cbf90a57..93a01e97bf 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java @@ -385,6 +385,26 @@ public class MethodLibrary { return result; } + @SkylarkSignature( + name = "capitalize", + objectType = StringModule.class, + returnType = String.class, + doc = + "Returns a copy of the string with its first character capitalized and the rest" + + "lowercased. This method does not support non-ascii characters.", + mandatoryPositionals = {@Param(name = "self", type = String.class, doc = "This string.")} + ) + private static BuiltinFunction capitalize = + new BuiltinFunction("capitalize") { + @SuppressWarnings("unused") + public String invoke(String self) throws EvalException { + if (self.isEmpty()) { + return self; + } + return Character.toUpperCase(self.charAt(0)) + self.substring(1).toLowerCase(); + } + }; + @SkylarkSignature(name = "title", objectType = StringModule.class, returnType = String.class, doc = @@ -1379,9 +1399,28 @@ public class MethodLibrary { + "") public static final class DictModule {} - public static final List stringFunctions = ImmutableList.of( - count, endswith, find, index, format, join, lower, partition, replace, rfind, - rindex, rpartition, rsplit, slice, split, startswith, strip, title, upper); + public static final List stringFunctions = + ImmutableList.of( + capitalize, + count, + endswith, + find, + index, + format, + join, + lower, + partition, + replace, + rfind, + rindex, + rpartition, + rsplit, + slice, + split, + startswith, + strip, + title, + upper); public static final List listPureFunctions = ImmutableList.of( slice); -- cgit v1.2.3