diff options
author | Laurent Le Brun <laurentlb@google.com> | 2015-11-09 14:35:54 +0000 |
---|---|---|
committer | Damien Martin-Guillerez <dmarting@google.com> | 2015-11-10 10:23:05 +0000 |
commit | 3ef1eea7b84841bb696ad6341b2503804fa6562c (patch) | |
tree | 7362fb4deca0fb9c8fc9dd00e8614d2a2ae1a39c /src | |
parent | 4171208f957b78638313219d1858c9a4f1263b1a (diff) |
Add list.index method
--
MOS_MIGRATED_REVID=107379557
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java | 27 | ||||
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java | 13 |
2 files changed, 40 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.", diff --git a/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java b/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java index c78fba035c..eee12f78df 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java @@ -858,6 +858,19 @@ public class MethodLibraryTest extends EvaluationTestCase { } @Test + public void testListIndexMethod() throws Exception { + new BothModesTest() + .testStatement("['a', 'b', 'c'].index('a')", 0) + .testStatement("['a', 'b', 'c'].index('b')", 1) + .testStatement("['a', 'b', 'c'].index('c')", 2) + .testStatement("[2, 4, 6].index(4)", 1) + .testStatement("[2, 4, 6].index(4)", 1) + .testStatement("[0, 1, [1]].index([1])", 2) + .testIfErrorContains("Item \"a\" not found in list", "[1, 2].index('a')") + .testIfErrorContains("Item 0 not found in list", "[].index(0)"); + } + + @Test public void testListIndex() throws Exception { new BothModesTest() .testStatement("['a', 'b', 'c', 'd'][0]", "a") |