EnsureFixedContents: array compare more proper style, better errormsg

This commit is contained in:
Arkadiusz Bulski 2018-01-22 20:00:29 +01:00
parent 5a9d8f36dd
commit 77a748f2f9
1 changed files with 9 additions and 16 deletions

View File

@ -428,26 +428,19 @@ namespace Kaitai
public byte[] EnsureFixedContents(byte[] expected)
{
byte[] bytes = ReadBytes(expected.Length);
bool error = false;
if (bytes.Length == expected.Length)
if (bytes.Length != expected.Length)
{
for (int i = 0; i < bytes.Length; i++)
throw new Exception(string.Format("Expected bytes: {0} ({1} bytes), Instead got: {2} ({3} bytes)", Convert.ToBase64String(expected), expected.Length, Convert.ToBase64String(bytes), bytes.Length));
}
for (int i = 0; i < bytes.Length; i++)
{
if (bytes[i] != expected[i])
{
if (bytes[i] != expected[i])
{
error = true;
break;
}
throw new Exception(string.Format("Expected bytes: {0} ({1} bytes), Instead got: {2} ({3} bytes)", Convert.ToBase64String(expected), expected.Length, Convert.ToBase64String(bytes), bytes.Length));
}
}
else
{
error = true;
}
if (error)
{
throw new Exception(string.Format("Expected bytes: {0}, Instead got: {1}", Convert.ToBase64String(expected), Convert.ToBase64String(bytes)));
}
return bytes;
}