From 483d5c6a46950334610b369ee3ec253eea132aa0 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 2 Oct 2021 00:59:38 +0200 Subject: Fixed a file loading bug not covered by the mycology test suite --- pkg/field/field.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'pkg/field/field.go') diff --git a/pkg/field/field.go b/pkg/field/field.go index a31f4da..72890fe 100644 --- a/pkg/field/field.go +++ b/pkg/field/field.go @@ -36,13 +36,19 @@ func Load(fd io.Reader) (*Field, error) { data := make([]byte, 4096) if n, errRead := fd.Read(data); errRead != nil { if errRead == io.EOF { - if f.ly == 0 && l.l == 0 { - return nil, newDecodeError("No instruction on the first line of the file produces an unusable program in Befunge98") + if f.ly == 0 { + if l.l == 0 { + return nil, newDecodeError("No instruction on the first line of the file produces an unusable program in Befunge98") + } + f.x = l.x } if l.l > 0 { f.ly++ - if f.lx-f.x < l.l-l.x { - f.lx = l.l - l.x + f.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 } f.lines = append(f.lines, *l) } @@ -56,12 +62,18 @@ func Load(fd io.Reader) (*Field, error) { continue } if data[i] == '\n' || data[i] == '\r' { - if f.ly == 0 && l.l == 0 { - return nil, newDecodeError("No instruction on the first line of the file produces an unusable program in Befunge98") + if f.ly == 0 { + if l.l == 0 { + return nil, newDecodeError("No instruction on the first line of the file produces an unusable program in Befunge98") + } + f.x = l.x } f.ly++ - if f.lx < l.l { - f.lx = l.l + 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) -- cgit v1.2.3