ed

[hare] The standard editor
Log | Files | Refs | README | LICENSE

commit dd873168e8451f83d9f16a87afb4578ba236f571
parent eddd47458d41cb267c74ea07b26dce189f9c844b
Author: Byron Torres <b@torresjrjr.com>
Date:   Mon, 12 Dec 2022 03:34:55 +0000

add assert_nonzero()

Diffstat:
Mcommand.ha | 16++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/command.ha b/command.ha @@ -102,6 +102,13 @@ fn assert_noaddrs(s: *session, lns: []size) (void | unexpectedaddress) = { }; }; +fn assert_nonzero(s: *session, n: size) (void | badaddress) = { + if (n < 1) { + errormsg(s, "Invalid address"); + return badaddress; + }; +}; + fn cmd_append(s: *session, cmd: *command) (void | error) = void; fn cmd_change(s: *session, cmd: *command) (void | error) = void; @@ -113,7 +120,7 @@ fn cmd_delete(s: *session, cmd: *command) (void | error) = { s.buf.cursor, s.buf.cursor, )?; - if (a < 1) return badaddress; + assert_nonzero(s, a)?; buf_delete(&s.buf, a, b); s.buf.cursor = if (len(s.buf.lines) == 1) { @@ -213,7 +220,7 @@ fn cmd_number(s: *session, cmd: *command) (void | error) = { s.buf.cursor, s.buf.cursor, )?; - if (a < 1) return badaddress; + assert_nonzero(s, a)?; for (let n = a; n <= b; n += 1) { fmt::printfln("{}\t{}", n, s.buf.lines[n].text)!; @@ -228,6 +235,7 @@ fn cmd_print(s: *session, cmd: *command) (void | error) = { s.buf.cursor, s.buf.cursor, )?; + assert_nonzero(s, a)?; for (let n = a; n <= b; n += 1) { fmt::println(s.buf.lines[n].text)!; }; @@ -289,6 +297,7 @@ fn cmd_write(s: *session, cmd: *command) (void | error) = { addr_linenum(&s.buf, 1)!, addr_lastline(&s.buf), )?; + assert_nonzero(s, a)?; const fname = if (len(cmd.arg) != 0) { s.buf.filename = cmd.arg; @@ -329,6 +338,7 @@ fn cmd_write(s: *session, cmd: *command) (void | error) = { fn cmd_linenumber(s: *session, cmd: *command) (void | error) = { const n = get_linenum(cmd.linenums, addr_lastline(&s.buf)); + assert_nonzero(s, n)?; fmt::println(n)!; }; @@ -339,6 +349,8 @@ fn cmd_null(s: *session, cmd: *command) (void | error) = { cmd.linenums, addr_nextline(&s.buf, s.buf.cursor), ); + assert_nonzero(s, n)?; + fmt::println(s.buf.lines[n].text)!; s.buf.cursor = n; };