From 71099a8a4ec8819094a210084d6b28bc4945780e Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 7 Oct 2021 18:17:03 +0200 Subject: Fixed tricky field loading bug --- pkg/field/field.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'pkg/field/field.go') diff --git a/pkg/field/field.go b/pkg/field/field.go index 72890fe..efd59ec 100644 --- a/pkg/field/field.go +++ b/pkg/field/field.go @@ -32,6 +32,7 @@ func Load(fd io.Reader) (*Field, error) { f := new(Field) l := new(Line) trailingSpaces := 0 + lastReadIsCR := false for { data := make([]byte, 4096) if n, errRead := fd.Read(data); errRead != nil { @@ -61,6 +62,10 @@ func Load(fd io.Reader) (*Field, error) { if data[i] == ' ' { continue } + if lastReadIsCR && data[i] == '\n' { + lastReadIsCR = false + continue + } if data[i] == '\n' || data[i] == '\r' { if f.ly == 0 { if l.l == 0 { @@ -69,17 +74,23 @@ func Load(fd io.Reader) (*Field, error) { f.x = l.x } f.ly++ - 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 l.l > 0 { + 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 + } } f.lines = append(f.lines, *l) l = new(Line) trailingSpaces = 0 - if i+1 < n && data[i] == '\r' && data[i+1] == '\n' { - i++ + if data[i] == '\r' { + if i+1 < n && data[i+1] == '\n' { + i++ + } else { + lastReadIsCR = true + } } } else { if l.l == 0 && data[i] == ' ' { -- cgit v1.2.3