From 4407b1d67c77228ceef7647f5b36d03eee3baa0e Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 11 Oct 2021 21:43:24 +0200 Subject: Removed a useless len field for field lines --- src/field.nim | 90 +++++++++++++++++++++++++++-------------------------------- 1 file changed, 41 insertions(+), 49 deletions(-) (limited to 'src') diff --git a/src/field.nim b/src/field.nim index 215a0e3..82a8575 100644 --- a/src/field.nim +++ b/src/field.nim @@ -1,6 +1,6 @@ type Line = ref object - x, l: int + x: int columns: seq[int] Field* = object @@ -12,57 +12,54 @@ func Blank*(f: var Field, x, y: int) = if y < f.y or y >= f.y+f.ly: # outside the field return var l = f.lines[y-f.y] - if x < l.x or x >= l.x+l.l: # outside the field + if x < l.x or x >= l.x+l.columns.len: # outside the field return - if x > l.x and x < l.x+l.l-1: # just set the value + if x > l.x and x < l.x+l.columns.len-1: # just set the value l.columns[x-l.x] = int(' ') return - if l.l == 1: # this was the last character on the line + if l.columns.len == 1: # this was the last character on the line if y == f.y: # we need to trim the leading lines var i = 1 - while f.lines[i] == nil or f.lines[i].l == 0: + while f.lines[i] == nil or f.lines[i].columns.len == 0: inc i f.y += i f.lines = f.lines[i.. f.lines[i].x: f.x = f.lines[i].x - if x2 < f.lines[i].x + f.lines[i].l: - x2 = f.lines[i].x + f.lines[i].l + if x2 < f.lines[i].x + f.lines[i].columns.len: + x2 = f.lines[i].x + f.lines[i].columns.len f.lx = x2-f.x func Get*(f: Field, x, y: int): int = if y >= f.y and y < f.y+f.ly: let l = f.lines[y-f.y] - if x >= l.x and x < l.x+l.l: + if x >= l.x and x < l.x+l.columns.len: return l.columns[x-l.x] return int(' ') @@ -83,12 +80,12 @@ proc Load*(filename: string): ref Field = let n = file.readChars(data, 0, 4096) if n <= 0: if f.ly == 0: - if l.l == 0: # we got en empty file! + if l.columns.len == 0: # we got en empty file! return nil f.x = l.x - if l.l > 0: - if f.lx < l.l+l.x-f.x: - f.lx = l.l+l.x-f.x + if l.columns.len > 0: + if f.lx < l.columns.len+l.x-f.x: + f.lx = l.columns.len+l.x-f.x f.lines.add(l) inc f.ly break @@ -103,14 +100,14 @@ proc Load*(filename: string): ref Field = continue if data[i] == '\n' or data[i] == '\r': if f.ly == 0: - if l.l == 0: + if l.columns.len == 0: return nil f.x = l.x - if l.l > 0: + if l.columns.len > 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 + if f.lx < l.columns.len+l.x-f.x: + f.lx = l.columns.len+l.x-f.x inc f.ly f.lines.add(l) l = new(Line) @@ -122,22 +119,20 @@ proc Load*(filename: string): ref Field = lastReadIsCR = true else: if data[i] == ' ': - if l.l == 0: # trim leading spaces + if l.columns.len == 0: # trim leading spaces inc l.x else: inc trailingSpaces else: if trailingSpaces > 0: - let newL = l.l + trailingSpaces + 1 - l.columns.setlen(newL) - for j in l.l..= f.y: if y < f.y+f.ly: # the line exists var l = f.lines[y-f.y] - if l == nil or l.l == 0: # An empty line is a special case + if l == nil or l.columns.len == 0: # An empty line is a special case if l == nil: new(l) l.x = x - l.l = 1 l.columns = @[v] f.lines[y-f.y] = l if f.x > x: @@ -160,35 +154,33 @@ func Set*(f: var Field, x, y, v: int) = if f.lx < x-f.x+1: f.lx = x-f.x+1 elif x >= l.x: - if x < l.x+l.l: # just set the value + if x < l.x+l.columns.len: # just set the value l.columns[x-l.x] = v else: # append columns - let newL = x-l.x+1 - l.columns.setlen(newL) - for i in l.l.. x: f.lx = f.lx + f.x - x f.x = x else: # append lines f.ly = y-f.y+1 f.lines.setlen(f.ly) - f.lines[f.ly-1] = Line(x: x, l: 1, columns: @[v]) + f.lines[f.ly-1] = Line(x: x, columns: @[v]) if f.x > x: f.lx = f.lx + f.x - x f.x = x @@ -197,7 +189,7 @@ func Set*(f: var Field, x, y, v: int) = else: # prepend lines let newLy = f.ly+f.y-y var newlines = newSeq[Line](newLy) - newlines[0] = Line(x: x, l: 1, columns: @[v]) + newlines[0] = Line(x: x, columns: @[v]) for i in f.y-y..