GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Honour the needle's reader index in ByteBufUtil.indexOf(ByteBuf, Byte… · netty/netty@2544d64

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src
    • main/java/io/netty/buffer
      • ByteBufUtil.java
    • test/java/io/netty/buffer
      • ByteBufUtilTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎buffer/src/main/java/io/netty/buffer/ByteBufUtil.java‎

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/ByteBufUtil.java
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,16 @@ public static int indexOf(ByteBuf needle, ByteBuf haystack) {
326326
}
327327

328328
private static long maxSuf(ByteBuf x, int m, int start, boolean isSuffix) {
329+
// j, k, ms and p are relative to start (the reader index of the needle), like the indexes in indexOf(...)
329330
int p = 1;
330331
int ms = -1;
331-
int j = start;
332+
int j = 0;
332333
int k = 1;
333334
byte a;
334335
byte b;
335336
while (j + k < m) {
336-
a = x.getByte(j + k);
337-
b = x.getByte(ms + k);
337+
a = x.getByte(start + j + k);
338+
b = x.getByte(start + ms + k);
338339
boolean suffix = isSuffix ? a < b : a > b;
339340
if (suffix) {
340341
j += k;
Collapse file

‎buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java‎

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java
+54Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
import org.junit.jupiter.api.function.Executable;
2222
import org.junit.jupiter.params.ParameterizedTest;
2323
import org.junit.jupiter.params.provider.MethodSource;
24+
import org.junit.jupiter.params.provider.ValueSource;
2425

2526
import java.nio.ByteOrder;
2627
import java.nio.charset.Charset;
2728
import java.util.ArrayList;
2829
import java.util.Arrays;
2930
import java.util.Collection;
3031
import java.util.List;
32+
import java.util.Random;
3133
import java.util.concurrent.ThreadLocalRandom;
3234
import java.util.concurrent.atomic.AtomicInteger;
3335
import java.util.concurrent.atomic.AtomicReference;
@@ -177,6 +179,58 @@ public void testIndexOf() {
177179
needle.release();
178180
}
179181

182+
@Test
183+
public void testIndexOfNeedleWithReaderIndex() {
184+
ByteBuf needle = Unpooled.copiedBuffer("abc123", CharsetUtil.US_ASCII);
185+
needle.readerIndex(3);
186+
ByteBuf haystack = Unpooled.copiedBuffer("ab1123", CharsetUtil.US_ASCII);
187+
assertEquals(3, ByteBufUtil.indexOf(needle, haystack));
188+
haystack.release();
189+
needle.release();
190+
191+
needle = Unpooled.copiedBuffer("xab", CharsetUtil.US_ASCII);
192+
needle.readerIndex(1);
193+
haystack = Unpooled.copiedBuffer("aab", CharsetUtil.US_ASCII);
194+
assertEquals(1, ByteBufUtil.indexOf(needle, haystack));
195+
haystack.release();
196+
needle.release();
197+
}
198+
199+
@ParameterizedTest
200+
@ValueSource(ints = { 0, 1, 2, 3 })
201+
public void testIndexOfMatchesNaiveSearch(int needleReaderIndex) {
202+
Random random = new Random(needleReaderIndex);
203+
for (int i = 0; i < 10000; i++) {
204+
byte[] needleBytes = new byte[2 + random.nextInt(5)];
205+
byte[] haystackBytes = new byte[random.nextInt(16)];
206+
byte[] prefix = new byte[needleReaderIndex];
207+
for (byte[] bytes : Arrays.asList(needleBytes, haystackBytes, prefix)) {
208+
for (int j = 0; j < bytes.length; j++) {
209+
bytes[j] = (byte) random.nextInt(3);
210+
}
211+
}
212+
ByteBuf needle = Unpooled.buffer().writeBytes(prefix).writeBytes(needleBytes);
213+
needle.readerIndex(prefix.length);
214+
ByteBuf haystack = Unpooled.wrappedBuffer(haystackBytes);
215+
assertEquals(naiveIndexOf(needleBytes, haystackBytes), ByteBufUtil.indexOf(needle, haystack));
216+
needle.release();
217+
haystack.release();
218+
}
219+
}
220+
221+
private static int naiveIndexOf(byte[] needle, byte[] haystack) {
222+
for (int i = 0; i <= haystack.length - needle.length; i++) {
223+
int j = 0;
224+
while (j < needle.length && haystack[i + j] == needle[j]) {
225+
j++;
226+
}
227+
if (j == needle.length) {
228+
return i;
229+
}
230+
}
231+
return -1;
232+
}
233+
180234
@Test
181235
public void equalsBufferSubsections() {
182236
byte[] b1 = new byte[128];

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.