From 58226afd7b72d945dd5d0fb853771131a662e97d Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Wed, 3 Oct 2018 14:43:08 -0500 Subject: [PATCH] Revert ReadAsciiZ and ReadUtf8Z changes --- LibHac/Util.cs | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/LibHac/Util.cs b/LibHac/Util.cs index 2fdfa20a..8eebbae7 100644 --- a/LibHac/Util.cs +++ b/LibHac/Util.cs @@ -32,11 +32,17 @@ namespace LibHac public static bool ArraysEqual(T[] a1, T[] a2) { if (a1 == null || a2 == null) return false; + if (a1 == a2) return true; if (a1.Length != a2.Length) return false; + for (int i = 0; i < a1.Length; i++) { - if (!a1[i].Equals(a2[i])) return false; + if (!a1[i].Equals(a2[i])) + { + return false; + } } + return true; } @@ -83,34 +89,36 @@ namespace LibHac public static string ReadAsciiZ(this BinaryReader reader, int maxLength = int.MaxValue) { - var str = new List(); - byte ch; + long start = reader.BaseStream.Position; int size = 0; - while (size < maxLength) + + // Read until we hit the end of the stream (-1) or a zero + while (reader.BaseStream.ReadByte() - 1 > 0 && size < maxLength) { size++; - ch = reader.ReadByte(); - if (ch == 0) - break; - str.Add(ch); } - return Encoding.ASCII.GetString(str.ToArray()); + + reader.BaseStream.Position = start; + string text = reader.ReadAscii(size); + reader.BaseStream.Position++; // Skip the null byte + return text; } public static string ReadUtf8Z(this BinaryReader reader, int maxLength = int.MaxValue) { - var str = new List(); - byte ch; + long start = reader.BaseStream.Position; int size = 0; - while (size < maxLength) + + // Read until we hit the end of the stream (-1) or a zero + while (reader.BaseStream.ReadByte() - 1 > 0 && size < maxLength) { size++; - ch = reader.ReadByte(); - if (ch == 0) - break; - str.Add(ch); } - return Encoding.UTF8.GetString(str.ToArray()); + + reader.BaseStream.Position = start; + string text = reader.ReadUtf8(size); + reader.BaseStream.Position++; // Skip the null byte + return text; } public static void WriteUTF8(this BinaryWriter writer, string value)