Skip to content

Commit 16b9d57

Browse files
committed
Reuse StringBuilder in token reader
1 parent 29a24e8 commit 16b9d57

File tree

2 files changed

+8
-18
lines changed

2 files changed

+8
-18
lines changed

ValveKeyValue/ValveKeyValue/Deserialization/KeyValues1/KV1TokenReader.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public KV1TokenReader(TextReader textReader, KVSerializerOptions options) : base
2020
this.options = options;
2121
}
2222

23+
readonly StringBuilder sb = new();
2324
readonly KVSerializerOptions options;
2425

2526
public KVToken ReadNextToken()
@@ -69,8 +70,6 @@ KVToken ReadComment()
6970
{
7071
ReadChar(CommentBegin);
7172

72-
var sb = new StringBuilder();
73-
7473
// Some keyvalues implementations have a bug where only a single slash is needed for a comment
7574
// If the file ends with a single slash then we have an empty comment, bail out
7675
if (!TryGetNext(out var next))
@@ -102,6 +101,7 @@ KVToken ReadComment()
102101
}
103102

104103
var text = sb.ToString();
104+
sb.Clear();
105105

106106
return new KVToken(KVTokenType.Comment, text);
107107
}
@@ -135,7 +135,6 @@ KVToken ReadInclusion()
135135

136136
string ReadUntil(params char[] terminators)
137137
{
138-
var sb = new StringBuilder();
139138
var escapeNext = false;
140139

141140
var integerTerminators = new HashSet<int>(terminators.Select(t => (int)t));
@@ -178,6 +177,7 @@ string ReadUntil(params char[] terminators)
178177
}
179178

180179
var result = sb.ToString();
180+
sb.Clear();
181181

182182
// Valve bug-for-bug compatibility with tier1 KeyValues/CUtlBuffer: an invalid escape sequence is a null byte which
183183
// causes the text to be trimmed to the point of that null byte.
@@ -190,8 +190,6 @@ string ReadUntil(params char[] terminators)
190190

191191
string ReadUntilWhitespaceOrQuote()
192192
{
193-
var sb = new StringBuilder();
194-
195193
while (true)
196194
{
197195
var next = Peek();
@@ -203,7 +201,10 @@ string ReadUntilWhitespaceOrQuote()
203201
sb.Append(Next());
204202
}
205203

206-
return sb.ToString();
204+
var result = sb.ToString();
205+
sb.Clear();
206+
207+
return result;
207208
}
208209

209210
string ReadStringRaw()

ValveKeyValue/ValveKeyValue/KVObject.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,6 @@ KVCollectionValue GetCollectionValue()
100100
return collection;
101101
}
102102

103-
string DebuggerDescription
104-
{
105-
get
106-
{
107-
var description = new StringBuilder();
108-
description.Append(Name);
109-
description.Append(": ");
110-
description.Append(Value.ToString());
111-
112-
return description.ToString();
113-
}
114-
}
103+
string DebuggerDescription => $"{Name}: {Value}";
115104
}
116105
}

0 commit comments

Comments
 (0)