|
62 | 62 | import com.oracle.graal.python.builtins.objects.PNotImplemented;
|
63 | 63 | import com.oracle.graal.python.builtins.objects.bytes.BytesBuiltinsFactory.BytesLikeNoGeneralizationNodeGen;
|
64 | 64 | import com.oracle.graal.python.builtins.objects.common.IndexNodes.NormalizeIndexNode;
|
| 65 | +import com.oracle.graal.python.builtins.objects.common.SequenceNodes; |
65 | 66 | import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetObjectArrayNode;
|
66 | 67 | import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetSequenceStorageNode;
|
67 | 68 | import com.oracle.graal.python.builtins.objects.common.SequenceNodesFactory.GetObjectArrayNodeGen;
|
@@ -853,23 +854,42 @@ protected boolean doIt(byte[] bytes, byte[] suffix, int start, int end) {
|
853 | 854 |
|
854 | 855 | // bytes.index(x)
|
855 | 856 | // bytearray.index(x)
|
856 |
| - @Builtin(name = "index", minNumOfPositionalArgs = 2) |
| 857 | + @Builtin(name = "index", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 4) |
857 | 858 | @GenerateNodeFactory
|
858 |
| - public abstract static class ByteArrayIndexNode extends PythonBuiltinNode { |
859 |
| - @Child private SequenceStorageNodes.LenNode lenNode; |
860 |
| - |
| 859 | + public abstract static class ByteArrayIndexNode extends PythonQuaternaryBuiltinNode { |
861 | 860 | @Specialization
|
862 |
| - public int index(VirtualFrame frame, PIBytesLike byteArray, Object arg, |
863 |
| - @Cached("create()") BytesNodes.FindNode findNode) { |
864 |
| - return findNode.execute(frame, byteArray, arg, 0, getLength(byteArray.getSequenceStorage())); |
| 861 | + int index(VirtualFrame frame, PIBytesLike byteArray, Object arg, @SuppressWarnings("unused") PNone start, @SuppressWarnings("unused") PNone end, |
| 862 | + @Shared("len") @Cached SequenceStorageNodes.LenNode lenNode, |
| 863 | + @Shared("storage") @Cached SequenceNodes.GetSequenceStorageNode getStorageNode, |
| 864 | + @Shared("findNode") @Cached("create()") BytesNodes.FindNode findNode) { |
| 865 | + return checkResult(findNode.execute(frame, byteArray, arg, 0, lenNode.execute(getStorageNode.execute(byteArray)))); |
865 | 866 | }
|
866 | 867 |
|
867 |
| - private int getLength(SequenceStorage s) { |
868 |
| - if (lenNode == null) { |
869 |
| - CompilerDirectives.transferToInterpreterAndInvalidate(); |
870 |
| - lenNode = insert(SequenceStorageNodes.LenNode.create()); |
| 868 | + @Specialization(guards = {"!isPNone(start)"}) |
| 869 | + int indexWithStart(VirtualFrame frame, PIBytesLike byteArray, Object arg, Object start, @SuppressWarnings("unused") PNone end, |
| 870 | + @Shared("storage") @Cached SequenceNodes.GetSequenceStorageNode getStorageNode, |
| 871 | + @Shared("len") @Cached SequenceStorageNodes.LenNode lenNode, |
| 872 | + @Shared("findNode") @Cached("create()") BytesNodes.FindNode findNode) { |
| 873 | + return checkResult(findNode.execute(frame, byteArray, arg, start, lenNode.execute(getStorageNode.execute(byteArray)))); |
| 874 | + } |
| 875 | + |
| 876 | + @Specialization(guards = {"!isPNone(end)"}) |
| 877 | + int indexWithEnd(VirtualFrame frame, PIBytesLike byteArray, Object arg, @SuppressWarnings("unused") PNone start, Object end, |
| 878 | + @Shared("findNode") @Cached("create()") BytesNodes.FindNode findNode) { |
| 879 | + return checkResult(checkResult(findNode.execute(frame, byteArray, arg, 0, end))); |
| 880 | + } |
| 881 | + |
| 882 | + @Specialization(guards = {"!isPNone(start)", "!isPNone(end)"}) |
| 883 | + int indexWithStartEnd(VirtualFrame frame, PIBytesLike byteArray, Object arg, Object start, Object end, |
| 884 | + @Shared("findNode") @Cached("create()") BytesNodes.FindNode findNode) { |
| 885 | + return checkResult(findNode.execute(frame, byteArray, arg, start, end)); |
| 886 | + } |
| 887 | + |
| 888 | + private int checkResult(int result) { |
| 889 | + if (result == -1) { |
| 890 | + throw raise(PythonBuiltinClassType.ValueError, ErrorMessages.SUBSECTION_NOT_FOUND); |
871 | 891 | }
|
872 |
| - return lenNode.execute(s); |
| 892 | + return result; |
873 | 893 | }
|
874 | 894 | }
|
875 | 895 |
|
|
0 commit comments