Skip to content

Commit 5550fe8

Browse files
committed
Set up some extension methods for ITextReader.
1 parent f675211 commit 5550fe8

4 files changed

Lines changed: 62 additions & 21 deletions

File tree

Schema Tests/text/reader/TextReaderExtensionTests.cs renamed to Schema Tests/text/reader/TextReaderExtensionTests_Read.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,21 @@
33

44
namespace schema.text.reader;
55

6-
internal class TextReaderExtensionTests {
6+
internal partial class TextReaderExtensionTests {
7+
[Test]
8+
[TestCase("", ExpectedResult = "")]
9+
[TestCase(" ", ExpectedResult = "")]
10+
[TestCase(" foo ", ExpectedResult = "foo")]
11+
[TestCase(" \n foo \t ", ExpectedResult = "foo")]
12+
[TestCase("foo bar", ExpectedResult = "foo")]
13+
[TestCase("`foo bar`", ExpectedResult = "`foo bar`")]
14+
[TestCase("'foo bar'", ExpectedResult = "'foo bar'")]
15+
[TestCase("\"foo bar\"", ExpectedResult = "\"foo bar\"")]
16+
public string TestReadWord(string text) {
17+
using var tr = TextSchemaTestUtil.CreateTextReader(text);
18+
return tr.ReadWord();
19+
}
20+
721
[Test]
822
// Empty cases
923
[TestCase("", ExpectedResult = new string[] { })]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using NUnit.Framework;
2+
3+
4+
namespace schema.text.reader;
5+
6+
internal partial class TextReaderExtensionTests {
7+
// TODO
8+
}

Schema/src/text/reader/TextReaderExtensions.cs renamed to Schema/src/text/reader/TextReaderExtensions_Read.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,13 @@
55
namespace schema.text.reader;
66

77
public static partial class TextReaderExtensions {
8-
public static void SkipWhitespace(this ITextReader tr)
9-
=> tr.SkipManyIfPresent(TextReaderConstants.WHITESPACE_CHARS);
10-
118
public static string ReadWord(this ITextReader tr) {
129
tr.SkipWhitespace();
1310
return tr.ReadUpToStartOfTerminator([
1411
" ", "\t", "\n", "\r\n", ",", "{", "[", "}", "]", ":"
1512
]);
1613
}
1714

18-
public static bool TryToSkipComment(
19-
this ITextReader tr,
20-
string lineCommentPrefix = "//") {
21-
tr.SkipWhitespace();
22-
if (tr.Matches(lineCommentPrefix)) {
23-
tr.ReadLine();
24-
return true;
25-
}
26-
27-
if (tr.Matches("/*")) {
28-
tr.ReadUpToAndPastTerminator("*/");
29-
return true;
30-
}
31-
32-
return false;
33-
}
34-
3515
public static string[] ReadArguments(
3616
this ITextReader tr,
3717
ReadOnlySpan<char> separators,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace schema.text.reader;
2+
3+
public static partial class TextReaderExtensions {
4+
public static void SkipWhitespace(this ITextReader tr)
5+
=> tr.SkipManyIfPresent(TextReaderConstants.WHITESPACE_CHARS);
6+
7+
public static void SkipToEndOfLine(this ITextReader tr) {
8+
if (tr.Eof) {
9+
return;
10+
}
11+
12+
char c;
13+
do {
14+
c = tr.ReadChar();
15+
} while (c is not ('\n' or '\r'));
16+
17+
if (c == '\r') {
18+
tr.SkipOnceIfPresent('\n');
19+
}
20+
}
21+
22+
public static void SkipComments(
23+
this ITextReader tr,
24+
string lineCommentPrefix = "//") {
25+
TryAgain:
26+
27+
tr.SkipWhitespace();
28+
29+
if (tr.Matches(lineCommentPrefix)) {
30+
tr.SkipToEndOfLine();
31+
goto TryAgain;
32+
}
33+
34+
if (tr.Matches("/*")) {
35+
tr.ReadUpToAndPastTerminator("*/");
36+
goto TryAgain;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)