ByteBufUtil use IndexOfProcessor to find occurrence.

Motivation:
The way of firstIndexOf and lastIndexOf iterating the ByteBuf is similar to forEachByte and forEachByteDesc, but have many range checks.
Modifications:
Use forEachByte and a IndexOfProcessor to find occurrence.
Result:
eliminate range checks
This commit is contained in:
Alwayswithme 2015-05-07 10:36:49 +08:00 committed by Norman Maurer
parent d98b21be04
commit c727c16707

View File

@ -312,13 +312,7 @@ public final class ByteBufUtil {
return -1; return -1;
} }
for (int i = fromIndex; i < toIndex; i ++) { return buffer.forEachByte(fromIndex, toIndex - fromIndex, new IndexOfProcessor(value));
if (buffer.getByte(i) == value) {
return i;
}
}
return -1;
} }
private static int lastIndexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value) { private static int lastIndexOf(ByteBuf buffer, int fromIndex, int toIndex, byte value) {
@ -327,13 +321,7 @@ public final class ByteBufUtil {
return -1; return -1;
} }
for (int i = fromIndex - 1; i >= toIndex; i --) { return buffer.forEachByteDesc(toIndex, fromIndex - toIndex, new IndexOfProcessor(value));
if (buffer.getByte(i) == value) {
return i;
}
}
return -1;
} }
/** /**
@ -566,5 +554,18 @@ public final class ByteBufUtil {
} }
} }
private static class IndexOfProcessor implements ByteBufProcessor {
private final byte byteToFind;
public IndexOfProcessor(byte byteToFind) {
this.byteToFind = byteToFind;
}
@Override
public boolean process(byte value) {
return value != byteToFind;
}
}
private ByteBufUtil() { } private ByteBufUtil() { }
} }