public static long ReadLines(Stream stream, Encoding encoding, NewLineAsString onNewLine, IMetricsHost metrics = null, CancellationToken cancellationToken = default) { unsafe { var pendingLength = 0; byte[] buffer = null; // TODO convert to allocator NewLine newLine = (lineNumber, partial, start, length, x, m) => { if (buffer == null) { buffer = new byte[Constants.ReadAheadSize * 2]; } var target = new Span <byte>(buffer, pendingLength, length); var segment = new ReadOnlySpan <byte>(start, length); segment.CopyTo(target); if (partial) { pendingLength = length; } else { var line = new ReadOnlySpan <byte>(buffer, 0, length + pendingLength); onNewLine?.Invoke(lineNumber, encoding.GetString(line), m); pendingLength = 0; buffer = null; } }; return(ReadOrCountLines(stream, encoding, Constants.Buffer, newLine, cancellationToken, metrics)); } }
public static long ReadLines(Stream stream, Encoding encoding, NewLineAsString onNewLine, ISafeLogger logger = null, IMetricsHost metrics = null, CancellationToken cancellationToken = default) { unsafe { var pendingLength = 0; var buffer = Constants.Buffer; return(ReadOrCountLines(stream, encoding, buffer, OnNewLine, logger, metrics, cancellationToken)); void OnNewLine(long lineNumber, bool partial, byte *start, int length, Encoding x) { var target = new Span <byte>(buffer, pendingLength, length); var segment = new ReadOnlySpan <byte>(start, length); segment.CopyTo(target); if (partial) { pendingLength = length; } else { var line = new ReadOnlySpan <byte>(buffer, 0, length + pendingLength); onNewLine?.Invoke(lineNumber, encoding.GetString(line)); pendingLength = 0; } } } }
public static long ReadLines(Stream stream, Encoding encoding, NewLineAsString onNewLine, IMetricsHost metrics = null, CancellationToken cancellationToken = default) { unsafe { NewLine newLine = (n, s, l, e, m) => { onNewLine?.Invoke(n, e.GetString(s, l), m); }; return(ReadOrCountLines(stream, encoding, Constants.WorkingBuffer, newLine, cancellationToken, metrics)); } }
public static long ReadLines(Stream stream, Encoding encoding, byte[] buffer, NewLineAsString onNewLine, ISafeLogger logger = null, IMetricsHost metrics = null, CancellationToken cancellationToken = default) { unsafe { void NewLine(long n, bool p, byte *s, int l, Encoding e) { onNewLine?.Invoke(n, e.GetString(s, l)); } return(ReadOrCountLines(stream, encoding, buffer, NewLine, logger, metrics, cancellationToken)); } }