@@ -329,6 +329,62 @@ object ByteString {
329329 else - 1
330330 }
331331
332+ // Derived from code in Netty
333+ // https://github.com/netty/netty/blob/d28a0fc6598b50fbe8f296831777cf4b653a475f/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java#L242-L325
334+ override def indexOfSlice (slice : Array [Byte ], from : Int ): Int = {
335+ val n = length - from
336+ val m = slice.length
337+ if (m == 0 ) return 0
338+ // When the needle has only one byte that can be read,
339+ // the indexOf() can be used
340+ if (m == 1 ) return indexOf(slice.head, from)
341+ var i = 0
342+ var j = 0
343+ val aStartIndex = 0
344+ val bStartIndex = from
345+ val suffixes = SWARUtil .maxSuf(slice, m, aStartIndex, true )
346+ val prefixes = SWARUtil .maxSuf(slice, m, aStartIndex, false )
347+ val ell = Math .max((suffixes >> 32 ).toInt, (prefixes >> 32 ).toInt)
348+ var per = Math .max(suffixes.toInt, prefixes.toInt)
349+ var memory = 0
350+ val checkLen = Math .min(m - per, ell + 1 )
351+ if (SWARUtil .arrayBytesMatch(slice, aStartIndex, slice, aStartIndex + per, checkLen)) {
352+ memory = - 1
353+ while (j <= n - m) {
354+ i = Math .max(ell, memory) + 1
355+ while (i < m && (slice(i + aStartIndex) == bytes(i + j + bStartIndex))) i += 1
356+ if (i > n) return - 1
357+ if (i >= m) {
358+ i = ell
359+ while (i > memory && (slice(i + aStartIndex) == bytes(i + j + bStartIndex))) i -= 1
360+ if (i <= memory) return j + bStartIndex
361+ j += per
362+ memory = m - per - 1
363+ }
364+ else {
365+ j += i - ell
366+ memory = - 1
367+ }
368+ }
369+ }
370+ else {
371+ per = Math .max(ell + 1 , m - ell - 1 ) + 1
372+ while (j <= n - m) {
373+ i = ell + 1
374+ while (i < m && (slice(i + aStartIndex) == bytes(i + j + bStartIndex))) i += 1
375+ if (i > n) return - 1
376+ if (i >= m) {
377+ i = ell
378+ while (i >= 0 && (slice(i + aStartIndex) == bytes(i + j + bStartIndex))) i -= 1
379+ if (i < 0 ) return j + bStartIndex
380+ j += per
381+ }
382+ else j += i - ell
383+ }
384+ }
385+ - 1
386+ }
387+
332388 // Derived from code in Netty
333389 // https://github.com/netty/netty/blob/d28a0fc6598b50fbe8f296831777cf4b653a475f/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java#L366-L408
334390 override private [util] def bytesMatch (fromIndex : Int , checkBytes : Array [Byte ], bytesFromIndex : Int ,
@@ -615,6 +671,62 @@ object ByteString {
615671 else - 1
616672 }
617673
674+ // Derived from code in Netty
675+ // https://github.com/netty/netty/blob/d28a0fc6598b50fbe8f296831777cf4b653a475f/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java#L242-L325
676+ override def indexOfSlice (slice : Array [Byte ], from : Int ): Int = {
677+ val n = length - from
678+ val m = slice.length
679+ if (m == 0 ) return 0
680+ // When the needle has only one byte that can be read,
681+ // the indexOf() can be used
682+ if (m == 1 ) return indexOf(slice.head, from)
683+ var i = 0
684+ var j = 0
685+ val aStartIndex = 0
686+ val bStartIndex = from + startIndex
687+ val suffixes = SWARUtil .maxSuf(slice, m, aStartIndex, true )
688+ val prefixes = SWARUtil .maxSuf(slice, m, aStartIndex, false )
689+ val ell = Math .max((suffixes >> 32 ).toInt, (prefixes >> 32 ).toInt)
690+ var per = Math .max(suffixes.toInt, prefixes.toInt)
691+ var memory = 0
692+ val checkLen = Math .min(m - per, ell + 1 )
693+ if (SWARUtil .arrayBytesMatch(slice, aStartIndex, slice, aStartIndex + per, checkLen)) {
694+ memory = - 1
695+ while (j <= n - m) {
696+ i = Math .max(ell, memory) + 1
697+ while (i < m && (slice(i + aStartIndex) == bytes(i + j + bStartIndex))) i += 1
698+ if (i > n) return - 1
699+ if (i >= m) {
700+ i = ell
701+ while (i > memory && (slice(i + aStartIndex) == bytes(i + j + bStartIndex))) i -= 1
702+ if (i <= memory) return j + bStartIndex - startIndex
703+ j += per
704+ memory = m - per - 1
705+ }
706+ else {
707+ j += i - ell
708+ memory = - 1
709+ }
710+ }
711+ }
712+ else {
713+ per = Math .max(ell + 1 , m - ell - 1 ) + 1
714+ while (j <= n - m) {
715+ i = ell + 1
716+ while (i < m && (slice(i + aStartIndex) == bytes(i + j + bStartIndex))) i += 1
717+ if (i > n) return - 1
718+ if (i >= m) {
719+ i = ell
720+ while (i >= 0 && (slice(i + aStartIndex) == bytes(i + j + bStartIndex))) i -= 1
721+ if (i < 0 ) return j + bStartIndex - startIndex
722+ j += per
723+ }
724+ else j += i - ell
725+ }
726+ }
727+ - 1
728+ }
729+
618730 // Derived from code in Netty
619731 // https://github.com/netty/netty/blob/d28a0fc6598b50fbe8f296831777cf4b653a475f/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java#L366-L408
620732 override private [util] def bytesMatch (fromIndex : Int ,
0 commit comments