Skip to content

Commit

Permalink
Fix EndOfStreamException when a comment is the final token in a test …
Browse files Browse the repository at this point in the history
…file

Fixes #101
  • Loading branch information
yaakov-h committed Sep 9, 2024
1 parent 7995a33 commit 5ffa945
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
37 changes: 37 additions & 0 deletions ValveKeyValue/ValveKeyValue.Test/Text/TrailingCommentTestCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace ValveKeyValue.Test.Text
{
class TrailingCommentTestCase
{
[Test]
public void CanReadTrailingEmptyComment()
{
var data = """
"vertexlitgeneric" { "$basetexture" "models/props_oil/doors/oil_door" "$surfaceprop" "metal" "%keywords" "tf" }

/
""";
var kv = KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Deserialize(data);

Assert.That(kv.Name, Is.EqualTo("vertexlitgeneric"));
Assert.That(((string)kv["$basetexture"]), Is.EqualTo("models/props_oil/doors/oil_door"));
Assert.That(((string)kv["$surfaceprop"]), Is.EqualTo("metal"));
Assert.That(((string)kv["%keywords"]), Is.EqualTo("tf"));
}

[Test]
public void CanReadTrailingCommentWithText()
{
var data = """
"vertexlitgeneric" { "$basetexture" "models/props_oil/doors/oil_door" "$surfaceprop" "metal" "%keywords" "tf" }

// foo
""";
var kv = KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Deserialize(data);

Assert.That(kv.Name, Is.EqualTo("vertexlitgeneric"));
Assert.That(((string)kv["$basetexture"]), Is.EqualTo("models/props_oil/doors/oil_door"));
Assert.That(((string)kv["$surfaceprop"]), Is.EqualTo("metal"));
Assert.That(((string)kv["%keywords"]), Is.EqualTo("tf"));
}
}
}
20 changes: 12 additions & 8 deletions ValveKeyValue/ValveKeyValue/Deserialization/KVTokenReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,29 @@ public void Dispose()
}
}

protected char Next()
protected char Next() => TryGetNext(out var next) ? next : throw new EndOfStreamException();

protected bool TryGetNext(out char next)
{
int next;
int nextValue;

if (peekedNext.HasValue)
{
next = peekedNext.Value;
nextValue = peekedNext.Value;
peekedNext = null;
}
else
{
next = textReader.Read();
nextValue = textReader.Read();
}

if (next == -1)
if (nextValue == -1)
{
throw new EndOfStreamException();
next = default;
return false;
}

if (next is '\n')
if (nextValue is '\n')
{
lineOffset++;
columnOffset = 0;
Expand All @@ -63,7 +66,8 @@ protected char Next()
columnOffset++;
}

return (char)next;
next = (char)nextValue;
return true;
}

protected int Peek()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,24 @@ KVToken ReadComment()
ReadChar(CommentBegin);

var sb = new StringBuilder();
var next = Next();

// 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))
{
return new KVToken(KVTokenType.Comment, string.Empty);
}

// If the next character is not a slash, then we have a comment that starts with a single slash
// Otherwise pretend the comment is a double-slash and ignore this new second slash.
if (next != CommentBegin)
{
sb.Append(next);
}

while (true)
// Be more permissive here than in other places, as comments can be the last token in a file.
while (TryGetNext(out next))
{
next = Next();

if (next == '\n')
{
break;
Expand Down

0 comments on commit 5ffa945

Please sign in to comment.