From d425f77f0ba346de5b909176800c0602ed0b6a72 Mon Sep 17 00:00:00 2001 From: Mikhail Yakshin Date: Fri, 7 Apr 2017 08:13:57 +0300 Subject: [PATCH] Implemented ByteArrayCompare --- KaitaiStream.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/KaitaiStream.cs b/KaitaiStream.cs index 54df529..fdda7b1 100644 --- a/KaitaiStream.cs +++ b/KaitaiStream.cs @@ -616,6 +616,33 @@ namespace Kaitai return r; } + /// + /// Compares two byte arrays in lexicographical order. + /// + /// negative number if a is less than b, 0 if a is equal to b, positive number if a is greater than b. + /// First byte array to compare + /// Second byte array to compare. + public static int ByteArrayCompare(byte[] a, byte[] b) + { + if (a == b) + return 0; + int al = a.Count(); + int bl = b.Count(); + int minLen = al < bl ? al : bl; + for (int i = 0; i < minLen; i++) { + int cmp = a[i] - b[i]; + if (cmp != 0) + return cmp; + } + + // Reached the end of at least one of the arrays + if (al == bl) { + return 0; + } else { + return al - bl; + } + } + #endregion } }