本文整理汇总了C#中TextReader.TryRead方法的典型用法代码示例。如果您正苦于以下问题:C# TextReader.TryRead方法的具体用法?C# TextReader.TryRead怎么用?C# TextReader.TryRead使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TextReader
的用法示例。
在下文中一共展示了TextReader.TryRead方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadPropertyValue
/// <summary>
/// Reads a property value that supports quotes and escaping.
/// </summary>
/// <param name="reader">The text reader.</param>
/// <returns>The property value as a string or null if there is no value.</returns>
private string ReadPropertyValue(TextReader reader)
{
reader.SkipWhitespace();
var result = new StringBuilder();
bool singleQuotes = false;
bool doubleQuotes = false;
bool quoted = false;
char c;
// First character
if (!reader.TryRead(out c))
return null;
;
if (c == '\'')
singleQuotes = quoted = true;
else if (c == '"')
doubleQuotes = quoted = true;
else
result.Append(c);
// Next characters
do {
if (!reader.TryRead(out c))
break;
if (c == '\\') {
// Not yet implemented in OpenRCT2
// result.Append(ReadEscapedCharacter(reader));
result.Append(c);
} else if ((c == ';' || c == '#') && !quoted) {
break;
} else if (c == '\'' && singleQuotes) {
singleQuotes = false;
break;
} else if (c == '"' && doubleQuotes) {
doubleQuotes = false;
break;
} else {
result.Append(c);
}
} while (true);
if (singleQuotes || doubleQuotes)
throw new Exception("No end quote(s)");
if (!quoted)
return result.ToString().TrimEnd();
return result.ToString();
}
示例2: ParseGroup
private string ParseGroup(TextReader reader, int lineNumber, char openCharacter, char closeCharacter)
{
if (!reader.TryReadExpected(openCharacter.ToString()))
{
_logger.LogError(lineNumber, $"Expected '{openCharacter}'");
return null;
}
var sb = new StringBuilder();
char c;
bool missingEndBracket = true;
while (reader.TryRead(out c))
{
if (c == closeCharacter)
{
missingEndBracket = false;
break;
}
else
{
sb.Append(c);
}
}
if (missingEndBracket)
{
_logger.LogError(lineNumber, $"Group name did not end with '{closeCharacter}'");
return null;
}
ParseWhitespace(reader, lineNumber, 0);
return sb.ToString();
}
示例3: ReadEscapedCharacter
/// <summary>
/// Read an escaped character e.g. "\n". Must be performed after the backslash has been read.
/// </summary>
/// <param name="reader">The text reader.</param>
/// <returns>The escaped character.</returns>
private char ReadEscapedCharacter(TextReader reader)
{
char c;
if (!reader.TryRead(out c))
throw new Exception("Expected escape character");
switch (c) {
case '0':
return '\0';
case 't':
return '\t';
case 'r':
return '\r';
case 'n':
return '\n';
case 'x':
char[] hexCode = new char[4];
if (reader.ReadBlock(hexCode, 0, 4) != 4)
throw new Exception("Wrong format for escaped unicode character.");
return (char)Int32.Parse(new string(hexCode), NumberStyles.AllowHexSpecifier);
default:
return c;
}
}