Skip to content

Commit d7ca541

Browse files
committed
BytesStripRight and BytesTerminate: can return original array
1 parent 3df4e31 commit d7ca541

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

KaitaiStream.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ public byte[] EnsureFixedContents(byte[] expected)
460460

461461
/// <summary>
462462
/// Perform right-to-left strip on a byte array.
463+
/// WARNING: Can return original byte array.
463464
/// </summary>
464465
/// <param name="src">The data, as byte array</param>
465466
/// <param name="padByte">The padding byte, as integer</param>
@@ -469,13 +470,17 @@ public static byte[] BytesStripRight(byte[] src, byte padByte)
469470
while (newLen > 0 && src[newLen - 1] == padByte)
470471
newLen--;
471472

473+
if (newLen == src.Length)
474+
return src;
475+
472476
byte[] dst = new byte[newLen];
473477
Array.Copy(src, dst, newLen);
474478
return dst;
475479
}
476480

477481
/// <summary>
478482
/// Perform left-to-right search of specified terminating byte, and cutoff remaining bytes.
483+
/// WARNING: Can return original byte array.
479484
/// </summary>
480485
/// <param name="src">The data, as byte array</param>
481486
/// <param name="terminator">The terminating byte, as integer</param>
@@ -491,6 +496,9 @@ public static byte[] BytesTerminate(byte[] src, byte terminator, bool includeTer
491496
if (includeTerminator && newLen < maxLen)
492497
newLen++;
493498

499+
if (newLen == src.Length)
500+
return src;
501+
494502
byte[] dst = new byte[newLen];
495503
Array.Copy(src, dst, newLen);
496504
return dst;

0 commit comments

Comments
 (0)