Skip to content

Commit 20af3ac

Browse files
committed
bytesTerminateMulti(): update to follow C++ implementation
See kaitai-io/kaitai_struct_cpp_stl_runtime@1ac7819 I don't think this change fixes any bug in Java, but it's good to have consistent implementations across languages.
1 parent deb426e commit 20af3ac

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/main/java/io/kaitai/struct/KaitaiStream.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,22 @@ public static byte[] bytesTerminate(byte[] bytes, byte term, boolean includeTerm
352352

353353
public static byte[] bytesTerminateMulti(byte[] bytes, byte[] term, boolean includeTerm) {
354354
int unitSize = term.length;
355-
int lastUnitStart = bytes.length - unitSize;
356-
outerLoop: for (int i = 0; i <= lastUnitStart; i += unitSize) {
357-
for (int j = 0; j < unitSize; j++) {
358-
if (bytes[i + j] != term[j]) {
359-
continue outerLoop;
360-
}
355+
if (unitSize == 0) {
356+
return new byte[0];
357+
}
358+
int len = bytes.length;
359+
int iTerm = 0;
360+
for (int iBytes = 0; iBytes < len;) {
361+
if (bytes[iBytes] != term[iTerm]) {
362+
iBytes += unitSize - iTerm;
363+
iTerm = 0;
364+
continue;
365+
}
366+
iBytes++;
367+
iTerm++;
368+
if (iTerm == unitSize) {
369+
return Arrays.copyOf(bytes, iBytes - (includeTerm ? 0 : unitSize));
361370
}
362-
return Arrays.copyOf(bytes, i + (includeTerm ? unitSize : 0));
363371
}
364372
return Arrays.copyOf(bytes, bytes.length);
365373
}

0 commit comments

Comments
 (0)