public override void Write(TextWriter writer, TextSpan span, CancellationToken cancellationToken = default(CancellationToken)) { if (span.Start < 0 || span.Start > _length || span.End > _length) { throw new ArgumentOutOfRangeException(nameof(span)); } int count = span.Length; if (count == 0) { return; } LargeTextWriter chunkWriter = writer as LargeTextWriter; int chunkIndex = GetIndexFromPosition(span.Start); int chunkStartOffset = span.Start - _chunkStartOffsets[chunkIndex]; while (true) { cancellationToken.ThrowIfCancellationRequested(); char[] chunk = _chunks[chunkIndex]; int charsToWrite = Math.Min(chunk.Length - chunkStartOffset, count); if (chunkWriter != null && chunkStartOffset == 0 && charsToWrite == chunk.Length) { // reuse entire chunk chunkWriter.AppendChunk(chunk); } else { writer.Write(chunk, chunkStartOffset, charsToWrite); } count -= charsToWrite; if (count <= 0) { break; } chunkStartOffset = 0; chunkIndex++; } }
private static SourceText FromLargeTextWriter(string source, Encoding encoding, SourceHashAlgorithm checksumAlgorithm) { using (var writer = new LargeTextWriter(encoding, checksumAlgorithm, source.Length)) { writer.Write(source); return writer.ToSourceText(); } }