diff options
author | 2015-11-09 14:35:54 +0000 | |
---|---|---|
committer | 2015-11-10 10:23:05 +0000 | |
commit | 3ef1eea7b84841bb696ad6341b2503804fa6562c (patch) | |
tree | 7362fb4deca0fb9c8fc9dd00e8614d2a2ae1a39c /src/main/java/com/google/devtools/build/lib | |
parent | 4171208f957b78638313219d1858c9a4f1263b1a (diff) |
Add list.index method
--
MOS_MIGRATED_REVID=107379557
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java | 27 |
1 files changed, 27 insertions, 0 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 3cc778984b..dbbb46ff47 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 @@ -882,6 +882,33 @@ public class MethodLibrary { } }; + @SkylarkSignature( + name = "index", + objectType = MutableList.class, + returnType = Integer.class, + doc = + "Returns the index in the list of the first item whose value is x. " + + "It is an error if there is no such item.", + mandatoryPositionals = { + @Param(name = "self", type = MutableList.class, doc = "This string, a separator."), + @Param(name = "x", type = Object.class, doc = "The object to search.") + }, + useLocation = true + ) + private static BuiltinFunction listIndex = + new BuiltinFunction("index") { + public Integer invoke(MutableList self, Object x, Location loc) throws EvalException { + int i = 0; + for (Object obj : self) { + if (obj.equals(x)) { + return i; + } + i++; + } + throw new EvalException(loc, Printer.format("Item %r not found in list", x)); + } + }; + // dictionary access operator @SkylarkSignature(name = "$index", documented = false, objectType = Map.class, doc = "Looks up a value in a dictionary.", |