From 12b147b7d2ca0b1860da8f5c85a5f6cb4307f964 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Fri, 9 Feb 2024 21:33:58 +0200 Subject: [PATCH] Move ParseHexStringAsByteArray to a class --- .../KeyValues1/KV1TextReader.cs | 16 +------------- .../ValveKeyValue/HexStringHelper.cs | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 15 deletions(-) create mode 100644 ValveKeyValue/ValveKeyValue/HexStringHelper.cs diff --git a/ValveKeyValue/ValveKeyValue/Deserialization/KeyValues1/KV1TextReader.cs b/ValveKeyValue/ValveKeyValue/Deserialization/KeyValues1/KV1TextReader.cs index b124cc7c..070ef526 100644 --- a/ValveKeyValue/ValveKeyValue/Deserialization/KeyValues1/KV1TextReader.cs +++ b/ValveKeyValue/ValveKeyValue/Deserialization/KeyValues1/KV1TextReader.cs @@ -259,7 +259,7 @@ static KVValue ParseValue(string text) if (text.Length == HexStringLengthForUnsignedLong && text.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) { var hexadecimalString = text[2..]; - var data = ParseHexStringAsByteArray(hexadecimalString); + var data = HexStringHelper.ParseHexStringAsByteArray(hexadecimalString); if (BitConverter.IsLittleEndian) { @@ -293,20 +293,6 @@ static KVValue ParseValue(string text) return new KVObjectValue(text, KVValueType.String); } - static byte[] ParseHexStringAsByteArray(string hexadecimalRepresentation) - { - Require.NotNull(hexadecimalRepresentation, nameof(hexadecimalRepresentation)); - - var data = new byte[hexadecimalRepresentation.Length / 2]; - for (var i = 0; i < data.Length; i++) - { - var currentByteText = hexadecimalRepresentation.Substring(i * 2, 2); - data[i] = byte.Parse(currentByteText, NumberStyles.HexNumber, CultureInfo.InvariantCulture); - } - - return data; - } - // The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional // ‘+’ or ‘-’ sign. If base is zero or 16, the string may then include a “0x” prefix, and the number will be read in base 16; // otherwise, a zero base is taken as 10 (decimal) unless the next character is ‘0’, in which case it is taken as 8 (octal). diff --git a/ValveKeyValue/ValveKeyValue/HexStringHelper.cs b/ValveKeyValue/ValveKeyValue/HexStringHelper.cs new file mode 100644 index 00000000..5cacd776 --- /dev/null +++ b/ValveKeyValue/ValveKeyValue/HexStringHelper.cs @@ -0,0 +1,21 @@ +using System.Globalization; + +namespace ValveKeyValue +{ + internal static class HexStringHelper + { + public static byte[] ParseHexStringAsByteArray(string hexadecimalRepresentation) + { + Require.NotNull(hexadecimalRepresentation, nameof(hexadecimalRepresentation)); + + var data = new byte[hexadecimalRepresentation.Length / 2]; + for (var i = 0; i < data.Length; i++) + { + var currentByteText = hexadecimalRepresentation.Substring(i * 2, 2); + data[i] = byte.Parse(currentByteText, NumberStyles.HexNumber, CultureInfo.InvariantCulture); + } + + return data; + } + } +}