Skip to content

Commit 486fd57

Browse files
committed
implement bytearray.__contains__(bytes)
1 parent 70962eb commit 486fd57

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/ByteArrayBuiltins.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,30 @@ public int len(PByteArray self,
506506
@Builtin(name = SpecialMethodNames.__CONTAINS__, fixedNumOfPositionalArgs = 2)
507507
@GenerateNodeFactory
508508
abstract static class ContainsNode extends PythonBinaryBuiltinNode {
509+
@Child private SequenceStorageNodes.LenNode lenNode;
510+
509511
@Specialization
510-
boolean contains(PSequence self, Object other,
512+
boolean contains(PByteArray self, PBytes other,
513+
@Cached("create()") BytesNodes.FindNode findNode) {
514+
return findNode.execute(self, other, 0, getLength(self.getSequenceStorage())) != -1;
515+
}
516+
517+
@Specialization
518+
boolean contains(PByteArray self, PByteArray other,
519+
@Cached("create()") BytesNodes.FindNode findNode) {
520+
return findNode.execute(self, other, 0, getLength(self.getSequenceStorage())) != -1;
521+
}
522+
523+
private int getLength(SequenceStorage s) {
524+
if (lenNode == null) {
525+
CompilerDirectives.transferToInterpreterAndInvalidate();
526+
lenNode = insert(SequenceStorageNodes.LenNode.create());
527+
}
528+
return lenNode.execute(s);
529+
}
530+
531+
@Specialization(guards = {"!isBytes(other)"})
532+
boolean contains(PByteArray self, Object other,
511533
@Cached("create()") BranchProfile errorProfile,
512534
@Cached("create()") SequenceStorageNodes.ContainsNode containsNode) {
513535

0 commit comments

Comments
 (0)