@@ -10,6 +10,7 @@ import kotlinx.io.bytestring.isEmpty
10
10
import kotlinx.io.bytestring.unsafe.UnsafeByteStringApi
11
11
import kotlinx.io.bytestring.unsafe.UnsafeByteStringOperations
12
12
import kotlinx.io.unsafe.UnsafeBufferOperations
13
+ import kotlin.math.max
13
14
import kotlin.math.min
14
15
15
16
/* *
@@ -85,21 +86,26 @@ public fun Source.readByteString(byteCount: Int): ByteString {
85
86
* expands the source's buffer as necessary until [byteString] is found. This reads an unbounded number of
86
87
* bytes into the buffer. Returns `-1` if the stream is exhausted before the requested bytes are found.
87
88
*
89
+ * For empty byte strings this function returns [startIndex] if it lays within underlying buffer's bounds,
90
+ * `0` if [startIndex] was negative and the size of the underlying buffer if [startIndex] exceeds its size.
91
+ * If the [startIndex] value was greater than the underlying buffer's size, the data will be fetched and buffered
92
+ * despite the [byteString] is empty.
93
+ *
88
94
* @param byteString the sequence of bytes to find within the source.
89
95
* @param startIndex the index into the source to start searching from.
90
96
*
91
- * @throws IllegalArgumentException if [startIndex] is negative.
92
97
* @throws IllegalStateException if the source is closed.
93
98
* @throws IOException when some I/O error occurs.
94
99
*
95
100
* @sample kotlinx.io.samples.ByteStringSamples.indexOfByteString
96
101
*/
97
102
@OptIn(InternalIoApi ::class , UnsafeByteStringApi ::class )
98
103
public fun Source.indexOf (byteString : ByteString , startIndex : Long = 0): Long {
99
- require( startIndex >= 0 ) { " startIndex: $startIndex " }
104
+ val startIndex = max( 0 , startIndex)
100
105
101
106
if (byteString.isEmpty()) {
102
- return 0
107
+ request(startIndex)
108
+ return min(startIndex, buffer.size)
103
109
}
104
110
105
111
var offset = startIndex
@@ -117,12 +123,22 @@ public fun Source.indexOf(byteString: ByteString, startIndex: Long = 0): Long {
117
123
return - 1
118
124
}
119
125
126
+ /* *
127
+ * Returns the index of the first match for [byteString] in the buffer at or after [startIndex].
128
+ *
129
+ * For empty byte strings this function returns [startIndex] if it lays within buffer's bounds,
130
+ * `0` if [startIndex] was negative and [Buffer.size] if it was greater or equal to [Buffer.size].
131
+ *
132
+ * @param byteString the sequence of bytes to find within the buffer.
133
+ * @param startIndex the index into the buffer to start searching from.
134
+ *
135
+ * @sample kotlinx.io.samples.ByteStringSamples.indexOfByteString
136
+ */
120
137
@OptIn(UnsafeByteStringApi ::class )
121
138
public fun Buffer.indexOf (byteString : ByteString , startIndex : Long = 0): Long {
122
- require(startIndex <= size) {
123
- " startIndex ($startIndex ) should not exceed size ($size )"
124
- }
125
- if (byteString.isEmpty()) return 0
139
+ val startIndex = max(0 , min(startIndex, size))
140
+
141
+ if (byteString.isEmpty()) return startIndex
126
142
if (startIndex > size - byteString.size) return - 1L
127
143
128
144
UnsafeByteStringOperations .withByteArrayUnsafe(byteString) { byteStringData ->
0 commit comments