diff options
-rw-r--r-- | pkg/field/field.go | 44 | ||||
-rw-r--r-- | pkg/field/field_test.go | 111 |
2 files changed, 86 insertions, 69 deletions
diff --git a/pkg/field/field.go b/pkg/field/field.go index fde478a..0495635 100644 --- a/pkg/field/field.go +++ b/pkg/field/field.go @@ -1,7 +1,6 @@ package field import ( - "bytes" "io" ) @@ -16,15 +15,17 @@ import ( // y|2,147,483,647 type Field struct { - firstLineIndex int - length int - lines []Line + x int + y int + lx int + ly int + lines []Line } type Line struct { - firstColumnIndex int - length int - columns []byte + x int + l int + columns []int } func LoadFile(fd io.Reader) (*Field, error) { @@ -35,11 +36,14 @@ func LoadFile(fd io.Reader) (*Field, error) { data := make([]byte, 4096) if n, errRead := fd.Read(data); errRead != nil { if errRead == io.EOF { - if f.length == 0 && l.length == 0 { + 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 l.length > 0 { - f.length++ + if l.l > 0 { + f.ly++ + if f.lx < l.l { + f.lx = l.l + } f.lines = append(f.lines, *l) } break @@ -49,10 +53,13 @@ func LoadFile(fd io.Reader) (*Field, error) { } else { for i := 0; i < n; i++ { if data[i] == '\n' || data[i] == '\r' { - if f.length == 0 && l.length == 0 { + 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") } - f.length++ + f.ly++ + if f.lx < l.l { + f.lx = l.l + } f.lines = append(f.lines, *l) l = new(Line) trailingSpaces = 0 @@ -60,17 +67,18 @@ func LoadFile(fd io.Reader) (*Field, error) { i++ } } else { - if l.length == 0 && data[i] == ' ' { - l.firstColumnIndex++ // trim leading spaces + if l.l == 0 && data[i] == ' ' { + l.x++ // trim leading spaces } else { if data[i] == ' ' { trailingSpaces++ } else { - l.columns = append(l.columns, bytes.Repeat([]byte{' '}, trailingSpaces)...) - l.length += trailingSpaces + for j := 0; j < trailingSpaces; j++ { + l.columns = append(l.columns, ' ') + } + l.l += trailingSpaces + 1 trailingSpaces = 0 - l.length++ - l.columns = append(l.columns, data[i]) + l.columns = append(l.columns, int(data[i])) } } } diff --git a/pkg/field/field_test.go b/pkg/field/field_test.go index 52f7d80..767a0dd 100644 --- a/pkg/field/field_test.go +++ b/pkg/field/field_test.go @@ -6,108 +6,117 @@ import ( "testing" "testing/iotest" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestLoadFile(t *testing.T) { // minimal b98 file minimalField := Field{ - firstLineIndex: 0, - length: 1, + x: 0, + y: 0, + lx: 1, + ly: 1, lines: []Line{ Line{ - firstColumnIndex: 0, - length: 1, - columns: []byte{'@'}, + x: 0, + l: 1, + columns: []int{'@'}, }, }, } // hello b98 file helloField := Field{ - firstLineIndex: 0, - length: 1, + x: 0, + y: 0, + lx: 24, + ly: 1, lines: []Line{ Line{ - firstColumnIndex: 0, - length: 24, - columns: []byte{'6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'}, + x: 0, + l: 24, + columns: []int{'6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'}, }, }, } // factorial b98 file factorialField := Field{ - firstLineIndex: 0, - length: 2, + x: 0, + y: 0, + lx: 15, + ly: 2, lines: []Line{ Line{ - firstColumnIndex: 0, - length: 15, - columns: []byte{'&', '>', ':', '1', '-', ':', 'v', ' ', 'v', ' ', '*', '_', '$', '.', '@'}, + x: 0, + l: 15, + columns: []int{'&', '>', ':', '1', '-', ':', 'v', ' ', 'v', ' ', '*', '_', '$', '.', '@'}, }, Line{ - firstColumnIndex: 1, - length: 11, - columns: []byte{'^', ' ', ' ', ' ', ' ', '_', '$', '>', '\\', ':', '^'}, + x: 1, + l: 11, + columns: []int{'^', ' ', ' ', ' ', ' ', '_', '$', '>', '\\', ':', '^'}, }, }, } // dna b98 file dnaField := Field{ - firstLineIndex: 0, - length: 8, + x: 0, + y: 0, + lx: 7, + ly: 8, lines: []Line{ Line{ - firstColumnIndex: 0, - length: 7, - columns: []byte{'7', '^', 'D', 'N', '>', 'v', 'A'}, + x: 0, + l: 7, + columns: []int{'7', '^', 'D', 'N', '>', 'v', 'A'}, }, Line{ - firstColumnIndex: 0, - length: 7, - columns: []byte{'v', '_', '#', 'v', '?', ' ', 'v'}, + x: 0, + l: 7, + columns: []int{'v', '_', '#', 'v', '?', ' ', 'v'}, }, Line{ - firstColumnIndex: 0, - length: 7, - columns: []byte{'7', '^', '<', '"', '"', '"', '"'}, + x: 0, + l: 7, + columns: []int{'7', '^', '<', '"', '"', '"', '"'}, }, Line{ - firstColumnIndex: 0, - length: 7, - columns: []byte{'3', ' ', ' ', 'A', 'C', 'G', 'T'}, + x: 0, + l: 7, + columns: []int{'3', ' ', ' ', 'A', 'C', 'G', 'T'}, }, Line{ - firstColumnIndex: 0, - length: 7, - columns: []byte{'9', '0', '!', '"', '"', '"', '"'}, + x: 0, + l: 7, + columns: []int{'9', '0', '!', '"', '"', '"', '"'}, }, Line{ - firstColumnIndex: 0, - length: 7, - columns: []byte{'4', '*', ':', '>', '>', '>', 'v'}, + x: 0, + l: 7, + columns: []int{'4', '*', ':', '>', '>', '>', 'v'}, }, Line{ - firstColumnIndex: 0, - length: 7, - columns: []byte{'+', '8', '^', '-', '1', ',', '<'}, + x: 0, + l: 7, + columns: []int{'+', '8', '^', '-', '1', ',', '<'}, }, Line{ - firstColumnIndex: 0, - length: 7, - columns: []byte{'>', ' ', ',', '+', ',', '@', ')'}, + x: 0, + l: 7, + columns: []int{'>', ' ', ',', '+', ',', '@', ')'}, }, }, } // \r\n file b98 file rnField := Field{ - firstLineIndex: 0, - length: 1, + x: 0, + y: 0, + lx: 24, + ly: 1, lines: []Line{ Line{ - firstColumnIndex: 0, - length: 24, - columns: []byte{'6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'}, + x: 0, + l: 24, + columns: []int{'6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'}, }, }, } @@ -148,7 +157,7 @@ func TestLoadFile(t *testing.T) { } else { require.NoError(t, err) } - assert.Equal(t, tc.expected, valid, "Invalid value") + require.Equal(t, tc.expected, valid, "Invalid value") }) } } |