Fixed tricky field loading bug

This commit is contained in:
Julien Dessaux 2021-10-07 18:17:03 +02:00
parent d141d68c1b
commit 71099a8a4e

View file

@ -32,6 +32,7 @@ func Load(fd io.Reader) (*Field, error) {
f := new(Field) f := new(Field)
l := new(Line) l := new(Line)
trailingSpaces := 0 trailingSpaces := 0
lastReadIsCR := false
for { for {
data := make([]byte, 4096) data := make([]byte, 4096)
if n, errRead := fd.Read(data); errRead != nil { if n, errRead := fd.Read(data); errRead != nil {
@ -61,6 +62,10 @@ func Load(fd io.Reader) (*Field, error) {
if data[i] == ' ' { if data[i] == ' ' {
continue continue
} }
if lastReadIsCR && data[i] == '\n' {
lastReadIsCR = false
continue
}
if data[i] == '\n' || data[i] == '\r' { if data[i] == '\n' || data[i] == '\r' {
if f.ly == 0 { if f.ly == 0 {
if l.l == 0 { if l.l == 0 {
@ -69,17 +74,23 @@ func Load(fd io.Reader) (*Field, error) {
f.x = l.x f.x = l.x
} }
f.ly++ f.ly++
if f.x > l.x { if l.l > 0 {
f.x = l.x if f.x > l.x {
} f.x = l.x
if f.lx < l.l+l.x-f.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 = append(f.lines, *l) f.lines = append(f.lines, *l)
l = new(Line) l = new(Line)
trailingSpaces = 0 trailingSpaces = 0
if i+1 < n && data[i] == '\r' && data[i+1] == '\n' { if data[i] == '\r' {
i++ if i+1 < n && data[i+1] == '\n' {
i++
} else {
lastReadIsCR = true
}
} }
} else { } else {
if l.l == 0 && data[i] == ' ' { if l.l == 0 && data[i] == ' ' {