1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.IO;
using System.Linq;
using System.Text;
namespace System.Web.Razor.Parser
{
internal static class TextReaderExtensions
{
public static string ReadUntil(this TextReader reader, char terminator)
{
return ReadUntil(reader, terminator, inclusive: false);
}
public static string ReadUntil(this TextReader reader, char terminator, bool inclusive)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
// Rather not allocate an array to use ReadUntil(TextReader, params char[]) so we'll just call the predicate version directly
return reader.ReadUntil(c => c == terminator, inclusive);
}
public static string ReadUntil(this TextReader reader, params char[] terminators)
{
// NOTE: Using named parameters would be difficult here, hence the inline comment
return reader.ReadUntil(inclusive: false, terminators: terminators);
}
public static string ReadUntil(this TextReader reader, bool inclusive, params char[] terminators)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
if (terminators == null)
{
throw new ArgumentNullException("terminators");
}
return reader.ReadUntil(c => terminators.Any(tc => tc == c), inclusive: inclusive);
}
public static string ReadUntil(this TextReader reader, Predicate<char> condition)
{
return reader.ReadUntil(condition, inclusive: false);
}
public static string ReadUntil(this TextReader reader, Predicate<char> condition, bool inclusive)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
if (condition == null)
{
throw new ArgumentNullException("condition");
}
StringBuilder builder = new StringBuilder();
int ch = -1;
while ((ch = reader.Peek()) != -1 && !condition((char)ch))
{
reader.Read(); // Advance the reader
builder.Append((char)ch);
}
if (inclusive && reader.Peek() != -1)
{
builder.Append((char)reader.Read());
}
return builder.ToString();
}
public static string ReadWhile(this TextReader reader, Predicate<char> condition)
{
return reader.ReadWhile(condition, inclusive: false);
}
public static string ReadWhile(this TextReader reader, Predicate<char> condition, bool inclusive)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
if (condition == null)
{
throw new ArgumentNullException("condition");
}
return reader.ReadUntil(ch => !condition(ch), inclusive);
}
public static string ReadWhiteSpace(this TextReader reader)
{
return reader.ReadWhile(c => Char.IsWhiteSpace(c));
}
public static string ReadUntilWhiteSpace(this TextReader reader)
{
return reader.ReadUntil(c => Char.IsWhiteSpace(c));
}
}
}
|