He-Pin commented on code in PR #2306:
URL: https://github.com/apache/pekko/pull/2306#discussion_r2441722662


##########
actor/src/main/scala/org/apache/pekko/util/ByteString.scala:
##########
@@ -1056,6 +1056,72 @@ sealed abstract class ByteString
    */
   def indexOf(elem: Byte): Int = indexOf(elem, 0)
 
+  override def indexOfSlice[B >: Byte](slice: scala.collection.Seq[B], from: 
Int): Int = {
+    // this is only called if the first byte matches, so we can skip that check
+    def check(startPos: Int): Boolean = {
+      var i = startPos + 1
+      var j = 1
+      // Bounds are guaranteed by the indexOf call limiting the search range, 
so startPos + slice.length <= length.
+      while (j < slice.length) {
+        if (apply(i) != slice(j)) return false
+        i += 1
+        j += 1
+      }
+      true
+    }
+    val headByte = slice.head.asInstanceOf[Byte]
+    @tailrec def rec(from: Int): Int = {
+      val startPos = indexOf(headByte, from, length - slice.length + 1)
+      if (startPos == -1) -1
+      else if (check(startPos)) startPos
+      else rec(startPos + 1)

Review Comment:
   see io.netty.buffer.ByteBufUtil#indexOf(io.netty.buffer.ByteBuf, 
io.netty.buffer.ByteBuf)
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to