Skip to content

Commit

Permalink
bytesTerminateMulti(): update to follow C++ implementation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
generalmimon committed Jul 30, 2024
1 parent deb426e commit 20af3ac
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/main/java/io/kaitai/struct/KaitaiStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,22 @@ public static byte[] bytesTerminate(byte[] bytes, byte term, boolean includeTerm

public static byte[] bytesTerminateMulti(byte[] bytes, byte[] term, boolean includeTerm) {
int unitSize = term.length;
int lastUnitStart = bytes.length - unitSize;
outerLoop: for (int i = 0; i <= lastUnitStart; i += unitSize) {
for (int j = 0; j < unitSize; j++) {
if (bytes[i + j] != term[j]) {
continue outerLoop;
}
if (unitSize == 0) {
return new byte[0];
}
int len = bytes.length;
int iTerm = 0;
for (int iBytes = 0; iBytes < len;) {
if (bytes[iBytes] != term[iTerm]) {
iBytes += unitSize - iTerm;
iTerm = 0;
continue;
}
iBytes++;
iTerm++;
if (iTerm == unitSize) {
return Arrays.copyOf(bytes, iBytes - (includeTerm ? 0 : unitSize));
}
return Arrays.copyOf(bytes, i + (includeTerm ? unitSize : 0));
}
return Arrays.copyOf(bytes, bytes.length);
}
Expand Down

0 comments on commit 20af3ac

Please sign in to comment.