public string ReadValue(TextFileContext context) { Ensure.ArgumentNotNull(context, "context"); context.EnsureNotAtEndOfLine(size); context.CurrentLinePosition += size; return context.CurrentLine.Substring(context.CurrentLinePosition - size, size); }
public string ReadValue(TextFileContext context) { Ensure.ArgumentNotNull(context, "context"); context.EnsureNotAtEndOfLine(); // TODO: Test performance of this under load, may need to switch to unsafe code int startPosition = context.CurrentLinePosition; bool isQuoted = false; int endExcess = 0; var quotePositions = new List<int>(); var escapePositions = new List<int>(); while (!context.Eol) { if (context.IsTokenAtCurrentPosition(escapedQuoteToken)) { escapePositions.Add(context.CurrentLinePosition); context.CurrentLinePosition += escapedQuoteToken.Length; continue; } if (context.IsTokenAtCurrentPosition(quoteToken)) { quotePositions.Add(context.CurrentLinePosition); context.CurrentLinePosition += quoteToken.Length; isQuoted = !isQuoted; continue; } var foundSeparator = isQuoted ? null : delimiters .Where(s => context.IsTokenAtCurrentPosition(s)) .FirstOrDefault(); if (foundSeparator != null) { context.CurrentLinePosition += foundSeparator.Length; endExcess = foundSeparator.Length; break; } ++context.CurrentLinePosition; } var value = context.CurrentLine.Substring(startPosition, context.CurrentLinePosition - startPosition - endExcess); if (quotePositions.Any()) value = RemoveTokens(value, quoteToken, quotePositions.Select(p => p - startPosition)); if (escapePositions.Any()) value = RemoveTokens(value, escapeToken, escapePositions.Select(p => p - startPosition)); return value; }