本文整理汇总了C#中TextReader.Peek方法的典型用法代码示例。如果您正苦于以下问题:C# TextReader.Peek方法的具体用法?C# TextReader.Peek怎么用?C# TextReader.Peek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TextReader
的用法示例。
在下文中一共展示了TextReader.Peek方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Scan
private void Scan(TextReader input)
{
while (input.Peek() != -1)
{
var ch = (char)input.Peek();
if (char.IsWhiteSpace(ch))
{
// eat the current char and skip ahead!
input.Read();
}
else if (char.IsLetter(ch) || ch == '_' || ch == '#')
{
// keyword or identifier
ScanIdent(input, ch);
}
else if (ch == '"')
{
// string literal
ScanString(input);
}
else if (char.IsDigit(ch))
{
// numeric literal
ScanNumber(input, ch);
}
else if (ch == ';')
{
// end of statement
ScanSemi(input);
}
else
{
// arithmetic tokens such as + - / * =
ScanArith(input, ch);
}
}
}
示例2: StringFormReader
public static object StringFormReader(TextReader stream, Reader reader)
{
object form = null;
char cur = Convert.ToChar(stream.Peek());
if (cur != '"') {
return null;
}
bool escaping = false;
StringWriter buf = new StringWriter();
SkipChar(stream);
do {
if (Empty(stream)) {
throw new ReaderException("Invalid string literal");
}
cur = ReadChar(stream);
if (escaping) {
escaping = false;
if (!ESCAPE_SEQUENCE_MAP.ContainsKey(cur)) {
throw new ReaderException("Unexpected escape sequence \\" + cur);
}
cur = ESCAPE_SEQUENCE_MAP[cur];
}
else {
if (cur == '\\') {
escaping = true;
}
else if (cur == '"') {
if (!escaping) {
form = buf.ToString();
}
}
}
if (form == null && !escaping) {
buf.Write(cur);
}
} while (form == null);
return form;
}
示例3: FromReader
public static IEnumerable<IList<string>> FromReader(TextReader csv, bool ignoreFirstLine)
{
if (ignoreFirstLine) csv.ReadLine();
IList<string> result = new List<string>();
StringBuilder curValue = new StringBuilder();
char c;
c = (char)csv.Read();
while (csv.Peek() != -1)
{
switch (c)
{
case ',': //empty field
result.Add("");
c = (char)csv.Read();
break;
case '"': //qualified text
case '\'':
char q = c;
c = (char)csv.Read();
bool inQuotes = true;
while (inQuotes && csv.Peek() != -1)
{
if (c == q)
{
c = (char)csv.Read();
if (c != q)
inQuotes = false;
}
if (inQuotes)
{
curValue.Append(c);
c = (char)csv.Read();
}
}
result.Add(curValue.ToString());
curValue = new StringBuilder();
if (c == ',') c = (char)csv.Read(); // either ',', newline, or endofstream
break;
case '\n': //end of the record
case '\r':
//potential bug here depending on what your line breaks look like
if (result.Count > 0) // don't return empty records
{
yield return result;
result = new List<string>();
}
c = (char)csv.Read();
break;
default: //normal unqualified text
while (c != ',' && c != '\r' && c != '\n' && csv.Peek() != -1)
{
curValue.Append(c);
c = (char)csv.Read();
}
result.Add(curValue.ToString());
curValue = new StringBuilder();
if (c == ',') c = (char)csv.Read(); //either ',', newline, or endofstream
break;
}
}
if (curValue.Length > 0) //potential bug: I don't want to skip on a empty column in the last record if a caller really expects it to be there
result.Add(curValue.ToString());
if (result.Count > 0)
yield return result;
}
示例4: generateTemplateLibraryCode
private static string generateTemplateLibraryCode(TextReader reader, string writerName)
{
State state = State.TemplateMode;
StringBuilder temp = new StringBuilder();
StringBuilder code = new StringBuilder();
string indentation = string.Empty;
code.Append(indentation);
while (reader.Peek() > -1) {
state = processChar(ref indentation, (char)reader.Read(), state, temp, code, writerName);
}
dump(state, temp, code, writerName);
return code.ToString().TrimEnd();
}
示例5: readNAG
private string readNAG(TextReader reader)
{
StringBuilder buffer = new StringBuilder ();
int ch;
while (true)
{
ch = reader.Peek ();
if (ch < 0)
break;
if (!Char.IsDigit ((char) ch))
{
break;
}
else
{
ch = reader.Read ();
buffer.Append ((char) ch);
}
}
return buffer.ToString ();
}
示例6: ScanString
private void ScanString(TextReader input)
{
char ch;
var accum = new StringBuilder();
input.Read(); // skip the '"'
if (input.Peek() == -1)
{
throw new Exception("unterminated string literal");
}
while ((ch = (char)input.Peek()) != '"')
{
accum.Append(ch);
input.Read();
if (input.Peek() == -1)
{
throw new Exception("unterminated string literal");
}
}
// skip the terminating "
input.Read();
_result.Add(accum);
}
示例7: ScanNumber
private void ScanNumber(TextReader input, char ch)
{
var accum = new StringBuilder();
while (char.IsDigit(ch))
{
accum.Append(ch);
input.Read();
if (input.Peek() == -1)
{
break;
}
ch = (char)input.Peek();
}
_result.Add(int.Parse(accum.ToString()));
}
示例8: ScanIdent
private void ScanIdent(TextReader input, char ch)
{
var accum = new StringBuilder();
while (char.IsLetter(ch) || ch == '_' || ch == '#')
{
accum.Append(ch);
input.Read();
if (input.Peek() == -1)
{
break;
}
ch = (char)input.Peek();
}
_result.Add(accum.ToString());
}
示例9: Scan
private void Scan(TextReader input)
{
while (input.Peek() != -1)
{
char ch = (char)input.Peek();
// Scan individual tokens
if (char.IsWhiteSpace(ch))
{
// eat the current char and skip ahead!
input.Read();
}
else if (char.IsLetter(ch) || ch == '_')
{
// keyword or identifier
Text.StringBuilder accum = new Text.StringBuilder();
while (char.IsLetter(ch) || ch == '_')
{
accum.Append(ch);
input.Read();
if (input.Peek() == -1)
{
break;
}
else
{
ch = (char)input.Peek();
}
}
this.result.Add(accum.ToString());
}
else if (ch == '"')
{
// string literal
Text.StringBuilder accum = new Text.StringBuilder();
input.Read(); // skip the '"'
if (input.Peek() == -1)
{
throw new System.Exception("unterminated string literal");
}
while ((ch = (char)input.Peek()) != '"')
{
accum.Append(ch);
input.Read();
if (input.Peek() == -1)
{
throw new System.Exception("unterminated string literal");
}
}
// skip the terminating "
input.Read();
this.result.Add(accum);
}
else if (char.IsDigit(ch))
{
// numeric literal
Text.StringBuilder accum = new Text.StringBuilder();
while (char.IsDigit(ch))
{
accum.Append(ch);
input.Read();
if (input.Peek() == -1)
{
break;
}
else
{
ch = (char)input.Peek();
}
}
this.result.Add(int.Parse(accum.ToString()));
}
else switch (ch)
{
case '+':
input.Read();
this.result.Add(Scanner.Add);
break;
case '-':
input.Read();
this.result.Add(Scanner.Sub);
break;
case '*':
input.Read();
this.result.Add(Scanner.Mul);
//.........这里部分代码省略.........
示例10: PeekChar
public static bool PeekChar(TextReader stream, out char c)
{
if (Empty(stream)) {
c = '\0';
return false;
}
c = Convert.ToChar(stream.Peek());
return true;
}
示例11: Empty
public static bool Empty(TextReader stream)
{
return stream == null || stream.Peek() == -1;
}
示例12: IntegerFormReader
// TODO: uh, handle negative numbers :-|
// TODO: oh yeah, floats and doubles too.
// TODO: hex! octal!
// TODO: bonus points: variable radix (3r20 == 0x06)
// TODO: ratios?
public static object IntegerFormReader(TextReader stream, Reader reader)
{
char cur = Convert.ToChar(stream.Peek());
if (!ISDIGIT_PATTERN.Match(cur.ToString()).Success) {
return null;
}
StringWriter buf = new StringWriter();
do {
buf.Write(ReadChar(stream));
if (Empty(stream)) {
break;
}
PeekChar(stream, out cur);
} while (ISDIGIT_PATTERN.Match(cur.ToString()).Success);
return Int64.Parse(buf.ToString());
}