From 8e8fa0546387b02eedea20e850d2b1eec0037646 Mon Sep 17 00:00:00 2001 From: Googler Date: Thu, 3 Sep 2015 18:36:33 +0000 Subject: Implement a Python 2 compatible isalpha function for Skylark strings. -- MOS_MIGRATED_REVID=102263878 --- .../devtools/build/lib/syntax/MethodLibrary.java | 24 +++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (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 ef958d356f..996ae41bbe 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 @@ -541,6 +541,28 @@ public class MethodLibrary { } }; + @SkylarkSignature(name = "isalpha", objectType = StringModule.class, returnType = Boolean.class, + doc = "Returns True if all characters in the string are alphabetic ([a-zA-Z]) and it " + + "contains at least one character.", + mandatoryPositionals = { + @Param(name = "self", type = String.class, doc = "This string.")}) + private static BuiltinFunction isalpha = new BuiltinFunction("isalpha") { + public Boolean invoke(String self) throws EvalException { + int length = self.length(); + if (length < 1) { + return false; + } + for (int index = 0; index < length; index++) { + char character = self.charAt(index); + if (!((character >= 'A' && character <= 'Z') + || (character >= 'a' && character <= 'z'))) { + return false; + } + } + return true; + } + }; + @SkylarkSignature(name = "count", objectType = StringModule.class, returnType = Integer.class, doc = "Returns the number of (non-overlapping) occurrences of substring sub in " + "string, optionally restricting to [start:end], " @@ -1091,7 +1113,7 @@ public class MethodLibrary { } } }; - + @SkylarkSignature(name = "dict", returnType = Map.class, doc = "Creates a dictionary from an optional positional " -- cgit v1.2.3