Archived
1
0
Fork 0

Fixed tricky field loading bug

This commit is contained in:
Julien Dessaux 2021-10-07 00:09:53 +02:00
parent cca0eb2117
commit e1b1a60bde
2 changed files with 17 additions and 9 deletions

View file

@ -79,9 +79,10 @@ proc Load*(filename: string): ref Field =
f.lines.add(Line()) f.lines.add(Line())
var l = addr f.lines[0] var l = addr f.lines[0]
var trailingSpaces = 0 var trailingSpaces = 0
var data: array[255, char] var data: array[4096, char]
var lastReadIsCR = false
while true: while true:
let n = file.readChars(data, 0, 255) let n = file.readChars(data, 0, 4096)
if n <= 0: if n <= 0:
if f.ly == 0: if f.ly == 0:
if l.l == 0: # we got en empty file! if l.l == 0: # we got en empty file!
@ -97,21 +98,29 @@ proc Load*(filename: string): ref Field =
if data[i] == char(12): if data[i] == char(12):
inc i inc i
continue continue
if lastReadIsCR and data[i] == '\n':
inc i
lastReadIsCR = false
continue
if data[i] == '\n' or data[i] == '\r': if data[i] == '\n' or data[i] == '\r':
if f.ly == 0: if f.ly == 0:
if l.l == 0: if l.l == 0:
return nil return nil
f.x = l.x f.x = l.x
inc f.ly inc f.ly
if f.x > l.x: if l.l > 0:
f.x = l.x if f.x > l.x:
if f.lx < l.l+l.x-f.x: f.x = l.x
f.lx = l.l+l.x-f.x if f.lx < l.l+l.x-f.x:
f.lx = l.l+l.x-f.x
f.lines.add(Line()) f.lines.add(Line())
l = addr f.lines[^1] l = addr f.lines[^1]
trailingSpaces = 0 trailingSpaces = 0
if i+1 < n and data[i] == '\r' and data[i+1] == '\n': if i+1 < n:
inc i if data[i] == '\r' and data[i+1] == '\n':
inc i
else:
lastReadIsCR = true
else: else:
if data[i] == ' ': if data[i] == ' ':
if l.l == 0: # trim leading spaces if l.l == 0: # trim leading spaces

View file

@ -2,7 +2,6 @@ import defaultIO
import field import field
import stackStack import stackStack
import math
import os import os
import random import random
import times import times