diff options
author | 2015-09-03 18:36:33 +0000 | |
---|---|---|
committer | 2015-09-03 22:17:22 +0000 | |
commit | 8e8fa0546387b02eedea20e850d2b1eec0037646 (patch) | |
tree | 2e8f6383debd57efd657b78c52554ca46d077fab /src/main | |
parent | 2c3b120a26612c5ad4856c043e888468b39a5f46 (diff) |
Implement a Python 2 compatible isalpha function for Skylark strings.
--
MOS_MIGRATED_REVID=102263878
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java | 24 |
1 files changed, 23 insertions, 1 deletions
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 <code>sub</code> in " + "string, optionally restricting to [<code>start</code>:<code>end</code>], " @@ -1091,7 +1113,7 @@ public class MethodLibrary { } } }; - + @SkylarkSignature(name = "dict", returnType = Map.class, doc = "Creates a <a href=\"#modules.dict\">dictionary</a> from an optional positional " |