Skip to content

Instantly share code, notes, and snippets.

@dgfitch
Created May 13, 2010 14:41
Show Gist options
  • Save dgfitch/399896 to your computer and use it in GitHub Desktop.
Save dgfitch/399896 to your computer and use it in GitHub Desktop.
(* In this weird case I need to write a stream but also keep track of how long ago a linefeed was written *)
member s.WriteStream (from:Stream) =
use reader = new StreamReader(from)
let mutable buffer = Array.zeroCreate<char>(BUFFER_LENGTH)
let mutable len = 0
while not reader.EndOfStream do
len <- reader.ReadBlock(buffer, 0, buffer.Length)
s.OutputWriter.Write(buffer, 0, len)
(* Now, walk backwards through the buffer looking for linefeeds *)
match
buffer
|> Array.rev
|> Array.tryFindIndex ((=) '\n')
with
| Some index -> s.Column := index - (BUFFER_LENGTH - len)
| None -> s.Column := !s.Column + len
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment