Skip to content

Commit

Permalink
Remove hashset allocation in token read
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Sep 16, 2024
1 parent 16b9d57 commit 0f99eae
Showing 1 changed file with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Linq;
using System.Text;

namespace ValveKeyValue.Deserialization.KeyValues1
Expand Down Expand Up @@ -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);
Expand All @@ -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))
Expand All @@ -133,12 +132,11 @@ KVToken ReadInclusion()
throw new InvalidDataException($"Unrecognized term after '#' symbol (line {Line}, column {Column})");
}

string ReadUntil(params char[] terminators)
string ReadUntil(Func<int, bool> isTerminator)
{
var escapeNext = false;

var integerTerminators = new HashSet<int>(terminators.Select(t => (int)t));
while (!integerTerminators.Contains(Peek()) || escapeNext)
while (escapeNext || !isTerminator(Peek()))
{
var next = Next();

Expand Down Expand Up @@ -223,7 +221,7 @@ string ReadStringRaw()
string ReadQuotedStringRaw()
{
ReadChar(QuotationMark);
var text = ReadUntil(QuotationMark);
var text = ReadUntil(static (c) => c == QuotationMark);
ReadChar(QuotationMark);
return text;
}
Expand Down

0 comments on commit 0f99eae

Please sign in to comment.