Skip to content

Instantly share code, notes, and snippets.

@rbehrends

rbehrends/rl.nim Secret

Created May 1, 2014 19:51
Show Gist options
  • Save rbehrends/c7fbe8aa8660e3d4379a to your computer and use it in GitHub Desktop.
Save rbehrends/c7fbe8aa8660e3d4379a to your computer and use it in GitHub Desktop.
proc linesWithEndings(file: var TFile): seq[string] =
var line = ""
var buf: array[8192, char]
var pendingCR = false
result = newSeq[string]()
while not file.endOfFile:
let n = readChars(file, buf, 0, sizeof(buf))
for i in 0.. <n:
let ch = buf[i]
case ch
of '\r':
if pendingCR:
add(result, line)
line.setLen(0)
pendingCR = true
add(line, ch)
of '\L':
pendingCR = false
add(line, ch)
add(result, line)
line.setLen(0)
else:
if pendingCR:
pendingCR = false
add(result, line)
line.setLen(0)
add(line, ch)
if len(line) > 0:
add(result, line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment