本文整理汇总了C#中TextReader.Read方法的典型用法代码示例。如果您正苦于以下问题:C# TextReader.Read方法的具体用法?C# TextReader.Read怎么用?C# TextReader.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TextReader
的用法示例。
在下文中一共展示了TextReader.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CalculateIndent
public static string CalculateIndent(TextReader code, int line, bool tabsToSpaces = false, int indentWidth = 4)
{
if(line < 2)
return string.Empty;
var eng = new IndentEngine(DFormattingOptions.CreateDStandard(), tabsToSpaces, indentWidth);
int curLine = 1;
const int lf = (int)'\n';
const int cr = (int)'\r';
int c;
while((c = code.Read()) != -1)
{
if(c == lf || c == cr)
{
if(c == cr && code.Peek() == lf)
code.Read();
if(++curLine > line)
break;
}
eng.Push((char)c);
}
return eng.ThisLineIndent;
}
示例2: CsvReader
public CsvReader(TextReader reader, char delimiter, bool parseHeader)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
_reader = reader;
_delimiter = delimiter;
int t;
if ((t = _reader.Read()) != -1)
{
_c = t;
}
if ((t = _reader.Read()) != -1)
{
_lac = t;
}
if (parseHeader)
{
if (Read())
{
for (int i = 0; i < _fields.Count; i++)
{
_columns[_fields[i]] = i;
}
}
}
}
示例3: ParseText
/// <summary> Split text of the documents into tokens</summary>
/// <param name="reader">stream with document text
/// </param>
/// <returns> array of occurrences of words in thedocument
/// </returns>
public virtual Occurrence[] ParseText(TextReader reader)
{
int pos = 0;
ArrayList list = new ArrayList();
int ch = reader.Read();
while (ch > 0)
{
if (Char.IsLetter((char) ch) || Char.IsDigit((char) ch))
{
StringBuilder buf = new StringBuilder();
int wordPos = pos;
do
{
pos += 1;
buf.Append((char) ch);
ch = reader.Read();
}
while (ch > 0 && (Char.IsLetter((char) ch) || Char.IsDigit((char) ch)));
string word = buf.ToString().ToLower();
if (!IsStopWord(word))
{
list.Add(new Occurrence(word, wordPos, 0));
}
}
else
{
pos += 1;
ch = reader.Read();
}
}
return (Occurrence[])list.ToArray(typeof(Occurrence));
}
示例4: DeserializeArray
static void DeserializeArray(TextReader reader, ObjectBuilder builder)
{
int c;
builder.StartArray();
while(true)
{
c = reader.Peek();
if (c == -1) throw new DeserializationException("Unexpected end of stream", reader);
if (c == ']')
{
reader.Read(); // skip the ]
break;
}
_DeserializeMember(reader, builder);
Methods.ConsumeWhiteSpace(reader);
c = reader.Read();
if(c == ',') continue;
if(c == ']') break;
if(c == -1) throw new DeserializationException("Unexpected end of stream", reader);
throw new DeserializationException("Expected , or ], found "+(char)c, reader);
}
builder.EndArray();
}
示例5: RawPBM
internal RawPBM(TextReader reader)
{
// Read width and height
Width = ParseNumber(ReadToken(reader));
Height = ParseNumber(ReadToken(reader));
// Skip single whitespace character
reader.Read();
// Read raster
InitializeRaster();
int length = Width * Height;
int lineLength = (Width + 7) / 8;
for (int i = 0; i < Height; i++)
{
for (int j = 0; j < lineLength; j++)
{
// This is wrong but i don't care
int vec = reader.Read();
if (vec == -1)
throw new MalformedFileException();
ProcessPixels(i, (j * 8), vec, PackedBits(j));
}
}
}
示例6: AccumulateComment
private string AccumulateComment(TextReader reader)
{
reader.Read();
bool? escaped = null;
StringBuilder buffer = new StringBuilder();
while (true)
{
if (escaped == null)
{
escaped = CheckIfEscaped(reader, buffer);
}
if (IsClosed(reader, buffer, escaped.Value))
{
break;
}
var node = reader.Read();
if (node == -1)
{
throw new InvalidOperationException("Reached end of template in the middle of a comment");
}
else
{
buffer.Append((char)node);
}
}
return buffer.ToString();
}
示例7: ReadNumber
/// <summary>
/// Reads a number from a text reader.
/// </summary>
/// <param name="input">The input to read from.</param>
/// <returns>The number read.</returns>
public static double ReadNumber(TextReader input)
{
// TODO: Check whether this is used in the parser (move to ModMaker.Lua) and make this
// consitent with the spec.
StringBuilder build = new StringBuilder();
int c = input.Peek();
int l = c;
bool hex = false;
CultureInfo ci = CultureInfo.CurrentCulture;
if (c == '0')
{
input.Read();
c = input.Peek();
if (c == 'x' || c == 'X')
{
input.Read();
hex = true;
}
}
while (c != -1 && (char.IsNumber((char)c) || (hex && ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) ||
(!hex && (c == ci.NumberFormat.NumberDecimalSeparator[0] || c == '-' || (l != '.' && c == 'e')))))
{
input.Read();
build.Append((char)c);
l = c;
c = input.Peek();
}
return double.Parse(build.ToString(), ci);
}
示例8: AccumulateLiteral
private static string AccumulateLiteral(TextReader reader, bool captureDelimiter, params char[] delimiters)
{
StringBuilder buffer = new StringBuilder();
while (true)
{
var node = reader.Peek();
if (node == -1)
{
throw new InvalidOperationException("Reached end of template before the expression was closed.");
}
else
{
if (delimiters.Contains((char)node))
{
if (captureDelimiter)
{
reader.Read();
}
break;
}
else if ((char)node == '}')
{
break;
}
else
{
buffer.Append((char)reader.Read());
}
}
}
return buffer.ToString();
}
示例9: Escape
/// <summary>
/// Escapes the characters in a String
/// </summary>
/// <param name="reader">the input string reader to escaping.</param>
/// <returns>a string that has been escaped.</returns>
public static string Escape(TextReader reader)
{
var sb = new StringBuilder();
var code = -1;
while ((code = reader.Read()) >= 0)
{
var ch = (char)code;
if (ch == '<')
{
sb.Append(ch);
if (reader.Peek() != -1 && ((char)reader.Peek()) == '/')
{
sb.Append(@"\/");
reader.Read();
}
}
else
{
string escapeChar = null;
if (_escapeSequences.TryGetValue(ch, out escapeChar))
{
sb.Append(escapeChar);
}
else
{
sb.Append(ch);
}
}
}
return sb.ToString();
}
示例10: SeekLine
/// <summary>
/// Seeks the first character of a specified line in the text stream.
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="line">Line number. The current position is assumed to be line #1.</param>
/// <returns>
/// Returns <c>true</c> if the line is found, <b>false</b> otherwise.
/// </returns>
public static bool SeekLine(TextReader reader, int line)
{
ContractUtils.RequiresNotNull(reader, "reader");
if (line < 1) throw new ArgumentOutOfRangeException("line");
if (line == 1) return true;
int current_line = 1;
for (; ; ) {
int c = reader.Read();
if (c == '\r') {
if (reader.Peek() == '\n') {
reader.Read();
}
current_line++;
if (current_line == line) return true;
} else if (c == '\n') {
current_line++;
if (current_line == line) return true;
} else if (c == -1) {
return false;
}
}
}
示例11: AccumulatePartial
private string AccumulatePartial(TextReader reader)
{
StringBuilder buffer = new StringBuilder();
buffer.Append(">");
do
{
reader.Read();
}
while(char.IsWhiteSpace((char)reader.Peek()));
while(true)
{
var peek = (char)reader.Peek();
if (peek == '}' || peek == '~' || char.IsWhiteSpace(peek))
{
break;
}
var node = reader.Read();
if (node == -1)
{
throw new InvalidOperationException("Reached end of template before the expression was closed.");
}
else
{
buffer.Append((char)node);
}
}
return buffer.ToString();
}
示例12: Tokenize
/// <summary>
/// Processes text from TextReader and splits it into tokens.
/// </summary>
/// <param name="reader">The TextReader to read string from</param>
/// <returns>The collection parsed tokens of the input string</returns>
public static IEnumerable<WktToken> Tokenize(TextReader reader)
{
StringBuilder stringBuffer = new StringBuilder();
if (reader.Peek() == -1) {
yield break;
}
char ch = (char)reader.Read();
stringBuffer.Append(ch);
TokenType lastToken = WktTokenizer.GetTokenType(ch);
while (reader.Peek() != -1) {
ch = (char)reader.Read();
TokenType token = WktTokenizer.GetTokenType(ch);
// tokens COMMA, LEFT_PARENTHESIS and RIGHT_PARENTHESIS can not be grupped together
if ((token != lastToken) || token == TokenType.COMMA || token == TokenType.LEFT_PARENTHESIS || token == TokenType.RIGHT_PARENTHESIS) {
yield return new WktToken() { Type = lastToken, Value = stringBuffer.ToString() };
stringBuffer.Clear();
lastToken = token;
}
stringBuffer.Append(ch);
}
yield return new WktToken() { Type = lastToken, Value = stringBuffer.ToString() };
}
示例13: Parse
public static DateTime Parse(TextReader reader, int context)
{
var cur = reader.Read();
if (cur == ',' || cur == ')')
return DateTime.MinValue;
var dt = ParseTimestamp(reader, context);
reader.Read();
return dt;
}
示例14: CorrectIndent
public static void CorrectIndent(TextReader code, int startOffset, int endOffset, Action<int, int, string> documentReplace, DFormattingOptions options = null, ITextEditorOptions textStyle = null, bool formatLastLine = true)
{
textStyle = textStyle ?? TextEditorOptions.Default;
var eng = new IndentEngine(options ?? DFormattingOptions.CreateDStandard(), textStyle.TabsToSpaces, textStyle.IndentSize, textStyle.KeepAlignmentSpaces);
var replaceActions = new List<DFormattingVisitor.TextReplaceAction>();
int originalIndent = 0;
bool hadLineBreak = true;
int n = 0;
for (int i = 0; i <= endOffset && (n = code.Read()) != -1; i++)
{
if(n == '\r' || n == '\n')
{
if (i >= startOffset && !eng.LineBeganInsideString)
replaceActions.Add(new DFormattingVisitor.TextReplaceAction(eng.Position - eng.LineOffset, originalIndent, eng.ThisLineIndent));
hadLineBreak = true;
originalIndent = 0;
if (code.Peek() == '\n')
{
eng.Push((char)code.Read());
i++;
}
}
else if(hadLineBreak)
{
if(n == ' ' || n== '\t')
originalIndent++;
else
hadLineBreak = false;
// If there's code left, format the last line of the selection either
if (i == endOffset && formatLastLine)
endOffset++;
}
eng.Push((char)n);
}
// Also indent the last line if we're at the EOF.
if (code.Peek() == -1 || (formatLastLine && n != '\r' && n != '\n'))
{
if(!eng.LineBeganInsideString)
replaceActions.Add(new DFormattingVisitor.TextReplaceAction(eng.Position - eng.LineOffset, originalIndent, eng.ThisLineIndent));
}
// Perform replacements from the back of the document to the front - to ensure offset consistency
for(int k = replaceActions.Count - 1; k>=0; k--)
{
var rep = replaceActions[k];
if(rep.RemovalLength > 0 || rep.NewText.Length != 0)
documentReplace(rep.Offset, rep.RemovalLength, rep.NewText);
}
}
示例15: ParseImpl
public static object ParseImpl(TextReader reader, GRGEN_LIBGR.AttributeType attrType, GRGEN_LIBGR.IGraph graph)
{
char lookahead = (char)reader.Peek();
if(lookahead == 'o')
{
reader.Read(); // eat 'o'
return new Own();
}
else if(lookahead == 'p')
{
reader.Read(); // eat 'p'
StringBuilder sb = new StringBuilder();
while(reader.Peek() != ',' && reader.Peek() != ')') // attributes are separated by , a node/edge terminated by ) in .grs
sb.Append((char)reader.Read()); // eat non ',', ')'
OwnPown op = new OwnPown();
op.ehe = sb.ToString();
return op;
}
else if(lookahead == 'h')
{
reader.Read(); // eat 'h'
StringBuilder sb = new StringBuilder();
while(reader.Peek() != ';')
sb.Append((char)reader.Read()); // eat non ';'
string ehe = sb.ToString();
sb.Length = 0;
reader.Read(); // eat ';'
while(reader.Peek() != ',' && reader.Peek() != ')') // attributes are separated by , a node/edge terminated by ) in .grs
sb.Append((char)reader.Read()); // eat non ',',')'
OwnPownHome oph = new OwnPownHome();
oph.ehe = ehe;
oph.aha = sb.ToString();
return oph;
}
else
{
if(reader.Peek() == 'n')
{
reader.Read();
if(reader.Peek() == 'u')
{
reader.Read();
if(reader.Peek() == 'l')
{
reader.Read();
if(reader.Peek() == 'l')
{
reader.Read();
return null;
}
}
}
}
throw new Exception("parsing failure");
}
}