Skip to content

Commit

Permalink
BytesStripRight and BytesTerminate: can return original array
Browse files Browse the repository at this point in the history
  • Loading branch information
arekbulski committed Apr 6, 2018
1 parent 3df4e31 commit d7ca541
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions KaitaiStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ public byte[] EnsureFixedContents(byte[] expected)

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

if (newLen == src.Length)
return src;

byte[] dst = new byte[newLen];
Array.Copy(src, dst, newLen);
return dst;
}

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

if (newLen == src.Length)
return src;

byte[] dst = new byte[newLen];
Array.Copy(src, dst, newLen);
return dst;
Expand Down

0 comments on commit d7ca541

Please sign in to comment.