diff --git a/ValveKeyValue/ValveKeyValue/Deserialization/KeyValues1/KV1TokenReader.cs b/ValveKeyValue/ValveKeyValue/Deserialization/KeyValues1/KV1TokenReader.cs index 7fae6f5..6897b7a 100644 --- a/ValveKeyValue/ValveKeyValue/Deserialization/KeyValues1/KV1TokenReader.cs +++ b/ValveKeyValue/ValveKeyValue/Deserialization/KeyValues1/KV1TokenReader.cs @@ -1,4 +1,3 @@ -using System.Linq; using System.Text; namespace ValveKeyValue.Deserialization.KeyValues1 @@ -20,6 +19,7 @@ public KV1TokenReader(TextReader textReader, KVSerializerOptions options) : base this.options = options; } + readonly StringBuilder sb = new(); readonly KVSerializerOptions options; public KVToken ReadNextToken() @@ -69,8 +69,6 @@ KVToken ReadComment() { ReadChar(CommentBegin); - var sb = new StringBuilder(); - // Some keyvalues implementations have a bug where only a single slash is needed for a comment // If the file ends with a single slash then we have an empty comment, bail out if (!TryGetNext(out var next)) @@ -102,6 +100,7 @@ KVToken ReadComment() } var text = sb.ToString(); + sb.Clear(); return new KVToken(KVTokenType.Comment, text); } @@ -109,7 +108,7 @@ KVToken ReadComment() KVToken ReadCondition() { ReadChar(ConditionBegin); - var text = ReadUntil(ConditionEnd); + var text = ReadUntil(static (c) => c == ConditionEnd); ReadChar(ConditionEnd); return new KVToken(KVTokenType.Condition, text); @@ -118,7 +117,7 @@ KVToken ReadCondition() KVToken ReadInclusion() { ReadChar(InclusionMark); - var term = ReadUntil(new[] { ' ', '\t' }); + var term = ReadUntil(static c => c is ' ' or '\t'); var value = ReadStringRaw(); if (string.Equals(term, "include", StringComparison.Ordinal)) @@ -133,13 +132,11 @@ KVToken ReadInclusion() throw new InvalidDataException($"Unrecognized term after '#' symbol (line {Line}, column {Column})"); } - string ReadUntil(params char[] terminators) + string ReadUntil(Func isTerminator) { - var sb = new StringBuilder(); var escapeNext = false; - var integerTerminators = new HashSet(terminators.Select(t => (int)t)); - while (!integerTerminators.Contains(Peek()) || escapeNext) + while (escapeNext || !isTerminator(Peek())) { var next = Next(); @@ -178,6 +175,7 @@ string ReadUntil(params char[] terminators) } var result = sb.ToString(); + sb.Clear(); // Valve bug-for-bug compatibility with tier1 KeyValues/CUtlBuffer: an invalid escape sequence is a null byte which // causes the text to be trimmed to the point of that null byte. @@ -190,8 +188,6 @@ string ReadUntil(params char[] terminators) string ReadUntilWhitespaceOrQuote() { - var sb = new StringBuilder(); - while (true) { var next = Peek(); @@ -203,7 +199,10 @@ string ReadUntilWhitespaceOrQuote() sb.Append(Next()); } - return sb.ToString(); + var result = sb.ToString(); + sb.Clear(); + + return result; } string ReadStringRaw() @@ -222,7 +221,7 @@ string ReadStringRaw() string ReadQuotedStringRaw() { ReadChar(QuotationMark); - var text = ReadUntil(QuotationMark); + var text = ReadUntil(static (c) => c == QuotationMark); ReadChar(QuotationMark); return text; } diff --git a/ValveKeyValue/ValveKeyValue/KVObject.cs b/ValveKeyValue/ValveKeyValue/KVObject.cs index c7e26eb..1e907cb 100644 --- a/ValveKeyValue/ValveKeyValue/KVObject.cs +++ b/ValveKeyValue/ValveKeyValue/KVObject.cs @@ -100,17 +100,6 @@ KVCollectionValue GetCollectionValue() return collection; } - string DebuggerDescription - { - get - { - var description = new StringBuilder(); - description.Append(Name); - description.Append(": "); - description.Append(Value.ToString()); - - return description.ToString(); - } - } + string DebuggerDescription => $"{Name}: {Value}"; } }