@@ -333,23 +333,42 @@ internal inline fun RealBufferedSource.commonIndexOf(b: Byte, fromIndex: Long, t
333
333
return - 1L
334
334
}
335
335
336
- internal inline fun RealBufferedSource.commonIndexOf (
336
+ internal fun RealBufferedSource.commonIndexOf (
337
337
bytes : ByteString ,
338
+ bytesOffset : Int = 0,
339
+ byteCount : Int = bytes.size,
338
340
fromIndex : Long ,
339
341
toIndex : Long = Long .MAX_VALUE ,
340
342
): Long {
343
+ checkOffsetAndCount(bytes.size.toLong(), bytesOffset.toLong(), byteCount.toLong())
344
+
341
345
var fromIndex = fromIndex
342
346
check(! closed) { " closed" }
343
347
344
348
while (true ) {
345
- val result = buffer.indexOf(bytes, fromIndex, toIndex)
349
+ val result = buffer.commonIndexOf(
350
+ bytes = bytes,
351
+ bytesOffset = bytesOffset,
352
+ byteCount = byteCount,
353
+ fromIndex = fromIndex,
354
+ toIndex = toIndex,
355
+ )
346
356
if (result != - 1L ) return result
347
357
348
358
val lastBufferSize = buffer.size
349
- val nextFromIndex = lastBufferSize - bytes.size + 1
359
+ val nextFromIndex = lastBufferSize - byteCount + 1
350
360
if (nextFromIndex >= toIndex) return - 1L
351
-
352
- if (! matchPossibleByExpandingBuffer(buffer, bytes, fromIndex, toIndex)) return - 1L
361
+ if (
362
+ ! buffer.isMatchPossibleByExpandingBuffer(
363
+ bytes = bytes,
364
+ bytesOffset = bytesOffset,
365
+ byteCount = byteCount,
366
+ fromIndex = fromIndex,
367
+ toIndex = toIndex,
368
+ )
369
+ ) {
370
+ return - 1L
371
+ }
353
372
if (source.read(buffer, Segment .SIZE .toLong()) == - 1L ) return - 1L
354
373
355
374
// Keep searching, picking up from where we left off.
@@ -372,20 +391,21 @@ internal inline fun RealBufferedSource.commonIndexOf(
372
391
* if the next loaded byte is 'o' then the result will be 1. But if the source's loaded content is
373
392
* 'look', we know the result is -1 without loading more data.
374
393
*/
375
- private fun matchPossibleByExpandingBuffer (
376
- buffer : Buffer ,
394
+ private fun Buffer.isMatchPossibleByExpandingBuffer (
377
395
bytes : ByteString ,
396
+ bytesOffset : Int ,
397
+ byteCount : Int ,
378
398
fromIndex : Long ,
379
399
toIndex : Long ,
380
400
): Boolean {
381
401
// Load new data if the match could come entirely in that new data.
382
- if (buffer. size < toIndex) return true
402
+ if (size < toIndex) return true
383
403
384
404
// Load new data if a prefix of 'bytes' matches a suffix of 'buffer'.
385
- val begin = maxOf(1 , buffer. size - toIndex + 1 ).toInt()
386
- val limit = minOf(bytes.size, buffer. size - fromIndex + 1 ).toInt()
405
+ val begin = maxOf(1 , size - toIndex + 1 ).toInt()
406
+ val limit = minOf(byteCount, size - fromIndex + 1 ).toInt()
387
407
for (i in limit - 1 downTo begin) {
388
- if (buffer. rangeEquals(buffer. size - i, bytes, 0 , i)) {
408
+ if (rangeEquals(size - i, bytes, bytesOffset , i)) {
389
409
return true
390
410
}
391
411
}
@@ -394,7 +414,10 @@ private fun matchPossibleByExpandingBuffer(
394
414
return false
395
415
}
396
416
397
- internal inline fun RealBufferedSource.commonIndexOfElement (targetBytes : ByteString , fromIndex : Long ): Long {
417
+ internal inline fun RealBufferedSource.commonIndexOfElement (
418
+ targetBytes : ByteString ,
419
+ fromIndex : Long ,
420
+ ): Long {
398
421
var fromIndex = fromIndex
399
422
check(! closed) { " closed" }
400
423
@@ -418,19 +441,18 @@ internal inline fun RealBufferedSource.commonRangeEquals(
418
441
): Boolean {
419
442
check(! closed) { " closed" }
420
443
421
- if (offset < 0L ||
422
- bytesOffset < 0 ||
423
- byteCount < 0 ||
424
- bytes.size - bytesOffset < byteCount
425
- ) {
426
- return false
427
- }
428
- for (i in 0 until byteCount) {
429
- val bufferOffset = offset + i
430
- if (! request(bufferOffset + 1 )) return false
431
- if (buffer[bufferOffset] != bytes[bytesOffset + i]) return false
432
- }
433
- return true
444
+ if (byteCount < 0 ) return false
445
+ if (offset < 0 ) return false
446
+ if (bytesOffset < 0 || bytesOffset + byteCount > bytes.size) return false
447
+ if (byteCount == 0 ) return true
448
+
449
+ return commonIndexOf(
450
+ bytes = bytes,
451
+ bytesOffset = bytesOffset,
452
+ byteCount = byteCount,
453
+ fromIndex = offset,
454
+ toIndex = offset + 1 ,
455
+ ) != - 1L
434
456
}
435
457
436
458
internal inline fun RealBufferedSource.commonPeek (): BufferedSource {
0 commit comments