summaryrefslogtreecommitdiff
path: root/src/ninep/addr.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/ninep/addr.zig')
-rw-r--r--src/ninep/addr.zig546
1 files changed, 486 insertions, 60 deletions
diff --git a/src/ninep/addr.zig b/src/ninep/addr.zig
index f7642b9e..7753c94d 100644
--- a/src/ninep/addr.zig
+++ b/src/ninep/addr.zig
@@ -1,6 +1,7 @@
-//! The address language of a pane's `addr` file: acme's, with mvzr regexps.
+//! The address language of a pane's `addr` file, acme's (editors/acme/
+//! addr.c), its regular expressions searched as sam searches (regexp.zig).
const std = @import("std");
-const mvzr = @import("mvzr");
+const regexp_ = @import("../regexp.zig");
const modal = @import("../modal.zig");
const pane_files = @import("pane.zig");
@@ -10,15 +11,15 @@ fn clip(n: usize) u32 {
return std.math.cast(u32, n) orelse std.math.maxInt(u32);
}
-fn safePattern(pat: []const u8) bool {
- var i: usize = 0;
- while (i < pat.len) : (i += 1) {
- if (pat[i] != '\\') continue;
- if (i + 1 >= pat.len) return false;
- i += 1;
- }
- return true;
-}
+pub const e_no_match = "no match for regexp";
+pub const e_range = "address out of range";
+pub const e_col_zero = "address out of range: a column counts from 1";
+pub const e_regexp = "bad regular expression";
+/// Not "took too long": 9ns reads errors by their words, and that would be
+/// ENAMETOOLONG.
+pub const e_slow = "regular expression search took too much time, gave up";
+pub const e_syntax = "bad address syntax";
+pub const e_order = "addresses out of order";
pub const Addr = struct {
text: []const u8,
@@ -26,6 +27,8 @@ pub const Addr = struct {
expr: []const u8,
i: usize = 0,
depth: u8 = 0,
+ /// Why `address` answered null.
+ err: []const u8 = e_syntax,
const max_depth = 32;
const Size = enum { char, line };
@@ -89,12 +92,63 @@ pub const Addr = struct {
if (d < '0' or d > '9') break;
n = @min(n * 10 + (d - '0'), std.math.maxInt(u32));
}
+ // pardes's `line:col`, Look's `file:12:5` spelling (look.zig
+ // parsePathLine): the point at that column of that line, the
+ // column in bytes from 1, clamped to the line's end and snapped
+ // to a rune's start, as every address is. Not sam's.
+ if (size == .line and dir == 0 and a.i + 1 < a.expr.len and a.expr[a.i] == ':' and
+ a.expr[a.i + 1] >= '0' and a.expr[a.i + 1] <= '9')
+ {
+ a.i += 1;
+ var col: u64 = 0;
+ while (a.i < a.expr.len and a.expr[a.i] >= '0' and a.expr[a.i] <= '9') : (a.i += 1)
+ col = @min(col * 10 + (a.expr[a.i] - '0'), std.math.maxInt(u32));
+ const lines = modal.cursorLineCount(a.text);
+ if (n == 0 or n > lines or col == 0) {
+ a.err = if (col == 0) e_col_zero else e_range;
+ return null;
+ }
+ const q = clip(modal.runeOffsetAt(a.text, .{ .row = @intCast(n - 1), .col = @intCast(col - 1) }));
+ r = .{ .q0 = q, .q1 = q };
+ // The rows' range, `L:C-L2:C2` or `L:C-C2` (Recent,
+ // +Search, Jumplist): through its last character,
+ // inclusive, as the rows mean it -- so a row pastes in.
+ if (a.i + 1 < a.expr.len and a.expr[a.i] == '-' and a.expr[a.i + 1] >= '1' and a.expr[a.i + 1] <= '9') {
+ a.i += 1;
+ var n2: u64 = 0;
+ while (a.i < a.expr.len and a.expr[a.i] >= '0' and a.expr[a.i] <= '9') : (a.i += 1)
+ n2 = @min(n2 * 10 + (a.expr[a.i] - '0'), std.math.maxInt(u32));
+ var end_line = n;
+ var end_col = n2;
+ if (a.i + 1 < a.expr.len and a.expr[a.i] == ':' and a.expr[a.i + 1] >= '0' and a.expr[a.i + 1] <= '9') {
+ a.i += 1;
+ end_line = n2;
+ end_col = 0;
+ while (a.i < a.expr.len and a.expr[a.i] >= '0' and a.expr[a.i] <= '9') : (a.i += 1)
+ end_col = @min(end_col * 10 + (a.expr[a.i] - '0'), std.math.maxInt(u32));
+ }
+ if (end_line == 0 or end_line > lines or end_col == 0) {
+ a.err = if (end_col == 0) e_col_zero else e_range;
+ return null;
+ }
+ const last = modal.runeOffsetAt(a.text, .{ .row = @intCast(end_line - 1), .col = @intCast(end_col - 1) });
+ const e = clip(@min(a.text.len, last + (std.unicode.utf8ByteSequenceLength(if (last < a.text.len) a.text[last] else 'x') catch 1)));
+ if (e < q) {
+ a.err = e_order;
+ return null;
+ }
+ r.q1 = e;
+ }
+ continue;
+ }
r = a.number(r, @intCast(n), dir, size) orelse return null;
dir = 0;
size = .line;
},
'/', '?' => {
- const back = c == '?';
+ // `-/re/` searches back as `?re?` does (acme's address()
+ // takes the direction a `-` set, addr.c).
+ const back = c == '?' or dir == '-';
r = a.regexp(r, a.pattern(c), back) orelse return null;
dir = 0;
size = .line;
@@ -134,8 +188,11 @@ pub const Addr = struct {
if (r.q0 == 0 and n > 0) r.q0 = clip(a.text.len);
off = @as(i64, r.q0) - n;
}
- if (off < 0 or off > @as(i64, @intCast(a.text.len))) return null;
- const g = clip(modal.graphemeStart(a.text, @intCast(off)));
+ if (off < 0 or off > @as(i64, @intCast(a.text.len))) {
+ a.err = e_range;
+ return null;
+ }
+ const g = clip(modal.runeStart(a.text, @intCast(off)));
return .{ .q0 = g, .q1 = g };
}
var line: i64 = n;
@@ -154,52 +211,125 @@ pub const Addr = struct {
}
q0 -= 1;
}
- if (line > 1) return null;
+ if (line > 1) {
+ a.err = e_range;
+ return null;
+ }
while (q0 > 0 and a.text[q0 - 1] != '\n') q0 -= 1;
return .{ .q0 = clip(q0), .q1 = clip(q1) };
},
- '+' => {
- if (q1 > 0) while (q1 < a.text.len and a.text[q1 - 1] != '\n') {
- q1 += 1;
- };
- q0 = q1;
- },
- else => {
- q0 = 0;
- q1 = 0;
- },
+ else => {},
}
- while (line > 0 and q1 < a.text.len) {
- const ch = a.text[q1];
- q1 += 1;
- if (ch == '\n' or q1 == a.text.len) {
- line -= 1;
- if (line > 0) q0 = q1;
+ // Forward, sam's lineaddr (editors/sam/address.c:180-215), not acme's
+ // number(): line n is found by counting newlines, and a line just
+ // past the last newline is an empty one there, so `1` of an empty
+ // text is #0,#0 and `2` of "a\n" is #2,#2; only past that is it out
+ // of range.
+ const nc = a.text.len;
+ var p: usize = 0;
+ var p1: usize = 0;
+ if (n == 0) {
+ if (dir == 0 or q1 == 0) return .{ .q0 = 0, .q1 = 0 };
+ p1 = q1;
+ p = q1 - 1;
+ } else {
+ var k: u32 = 1;
+ if (dir != 0 and q1 > 0) {
+ p = q1 - 1;
+ k = @intFromBool(a.text[p] == '\n');
+ p += 1;
}
+ if (k < n) {
+ p = nthLineAfter(a.text, p, n - k) orelse {
+ a.err = e_range;
+ return null;
+ };
+ }
+ p1 = p;
}
- if (line > 0) return null;
- return .{ .q0 = clip(q0), .q1 = clip(q1) };
+ p = if (std.mem.indexOfScalarPos(u8, a.text, p, '\n')) |nl| nl + 1 else nc;
+ return .{ .q0 = clip(p1), .q1 = clip(p) };
+ }
+
+ /// acme's regexp(): forward from the end of `r` to the limit, wrapping
+ /// to the start of the text when there is none; backward, the last
+ /// match ending by the start of `r`, else the last one in the text.
+ /// The first match at or after `from`, else, with no limit, the first
+ /// from the top (a search wraps); null (the outer) when too slow.
+ fn forward(a: *Addr, rx: *regexp_.Regex, from: usize, hi: usize) ??Range {
+ const ahead = rx.find(a.text, from, hi, hi) catch {
+ a.err = e_slow;
+ return null;
+ };
+ const m = ahead orelse (if (a.lim != null or from == 0) null else rx.find(a.text, 0, from - 1, hi) catch {
+ a.err = e_slow;
+ return null;
+ }) orelse return @as(?Range, null);
+ return @as(?Range, .{ .q0 = clip(m.start), .q1 = clip(m.end) });
+ }
+
+ /// The last match ending at or before `limit`, else the last of all
+ /// (a search wraps); null (the outer) when too slow.
+ fn backward(a: *Addr, rx: *regexp_.Regex, limit: u32) ??Range {
+ var last: ?Range = null;
+ var before: ?Range = null;
+ var at: usize = 0;
+ while (at <= a.text.len) {
+ const m = (rx.find(a.text, at, a.text.len, a.text.len) catch {
+ a.err = e_slow;
+ return null;
+ }) orelse break;
+ const found_range: Range = .{ .q0 = clip(m.start), .q1 = clip(m.end) };
+ if (found_range.q1 <= limit) before = found_range;
+ last = found_range;
+ at = if (m.end > m.start) m.end else m.end + 1;
+ }
+ return @as(?Range, before orelse last);
}
fn regexp(a: *Addr, r: Range, pat: []const u8, back: bool) ?Range {
- if (pat.len == 0 or !safePattern(pat)) return null;
- const re = mvzr.compile(pat) orelse return null;
- if (back) {
- const hi = @min(@as(usize, r.q0), a.text.len);
- var best: ?mvzr.Match = null;
- var at: usize = 0;
- while (at < hi) {
- const m = re.matchPos(at, a.text[0..hi]) orelse break;
- best = m;
- at = if (m.end > m.start) m.end else m.end + 1;
- }
- const m = best orelse return null;
- return .{ .q0 = clip(m.start), .q1 = clip(m.end) };
+ if (pat.len == 0) {
+ a.err = "no previous regular expression";
+ return null;
}
- const hi = if (a.lim) |l| @min(@as(usize, l.q1), a.text.len) else a.text.len;
- const from = @min(@as(usize, r.q1), hi);
- const m = re.match(a.text[from..hi]) orelse return null;
- return .{ .q0 = clip(from + m.start), .q1 = clip(from + m.end) };
+ var rx = regexp_.Regex.compile(pat) catch |err| {
+ a.err = switch (err) {
+ error.Anchor => regexp_.Regex.e_anchor,
+ error.TooLong => regexp_.Regex.e_long,
+ error.WideRange => regexp_.Regex.e_wide,
+ error.NegatedRunes => regexp_.Regex.e_negated,
+ error.Mixed => regexp_.Regex.e_mixed,
+ error.Bad => e_regexp,
+ };
+ return null;
+ };
+ // sam's nextmatch (editors/sam/address.c:97-119): an empty match
+ // right at the search's start is no progress, so the search is made
+ // again a character on (back: a character before), wrapping, and
+ // repeated `/^/` or `/$/` walks the lines.
+ const found = if (back) found: {
+ var f = a.backward(&rx, r.q0) orelse return null;
+ if (f) |m| if (m.q0 == m.q1 and m.q1 == r.q0) {
+ f = a.backward(&rx, if (r.q0 == 0) clip(a.text.len) else r.q0 - 1) orelse return null;
+ };
+ break :found f;
+ } else found: {
+ const hi = if (a.lim) |l| @min(@as(usize, l.q1), a.text.len) else a.text.len;
+ const from = @min(@as(usize, r.q1), hi);
+ var f = a.forward(&rx, from, hi) orelse return null;
+ if (f) |m| if (m.q0 == m.q1 and m.q0 == from) {
+ const next = if (from + 1 > hi) (if (a.lim != null) break :found null else 0) else from + 1;
+ f = a.forward(&rx, next, hi) orelse return null;
+ };
+ break :found f;
+ };
+ // A byte pattern may match inside a rune: the match covers the
+ // runes it touches.
+ if (found) |f| return .{ .q0 = clip(modal.runeStart(a.text, f.q0)), .q1 = clip(modal.runeEnd(a.text, f.q1)) };
+ return found orelse {
+ a.err = e_no_match;
+ return null;
+ };
}
};
@@ -210,6 +340,224 @@ const Node = tree.Node;
const E = tree.E;
const Status = tree.Status;
+test "an address is evaluated from the current one, as acme's are" {
+ const p = try th.withFile(testing.allocator, "a foo b foo c foo\n");
+ defer p.deinit();
+ const addr = Node.of(th.serialOf(p), .addr);
+ const Step = struct { expr: []const u8, q0: u32, q1: u32 };
+ _ = th.wr(p, addr, "#0");
+ for ([_]Step{
+ // each search starts where the last address ended
+ .{ .expr = "/foo/", .q0 = 2, .q1 = 5 },
+ .{ .expr = "/foo/", .q0 = 8, .q1 = 11 },
+ // `.` is that address, and `-` before a pattern searches back
+ .{ .expr = ".", .q0 = 8, .q1 = 11 },
+ .{ .expr = "-/foo/", .q0 = 2, .q1 = 5 },
+ .{ .expr = "./foo/", .q0 = 8, .q1 = 11 },
+ .{ .expr = "+/foo/", .q0 = 14, .q1 = 17 },
+ // a number then a pattern: the search starts at the number
+ .{ .expr = "#9/foo/", .q0 = 14, .q1 = 17 },
+ }) |c| {
+ try testing.expectEqual(Status.ok, th.wr(p, addr, c.expr).reply.status);
+ try testing.expectEqual(c.q0, p.panes[0].?.fs.addr.q0);
+ try testing.expectEqual(c.q1, p.panes[0].?.fs.addr.q1);
+ }
+ // A range whose end comes before its start is sam's error.
+ try testing.expectEqualStrings(e_order, th.wr(p, addr, "#10,#5").reply.ename);
+
+ // `;` evaluates its right side from its left (acme's addr.c:170-171,
+ // `ar = r`), `,` from the address the whole started at.
+ const q = try th.withFile(testing.allocator, "b a b\n");
+ defer q.deinit();
+ const qaddr = Node.of(th.serialOf(q), .addr);
+ _ = th.wr(q, qaddr, "#0");
+ try testing.expectEqual(Status.ok, th.wr(q, qaddr, "/a/;/b/").reply.status);
+ try testing.expectEqual(@as(u32, 2), q.panes[0].?.fs.addr.q0);
+ try testing.expectEqual(@as(u32, 5), q.panes[0].?.fs.addr.q1);
+ _ = th.wr(q, qaddr, "#0");
+ try testing.expectEqualStrings(e_order, th.wr(q, qaddr, "/a/,/b/").reply.ename);
+}
+
+test "a backward search along one long line takes time in its length, not its square" {
+ const gpa = testing.allocator;
+ const line = try gpa.alloc(u8, 7 * 16800);
+ defer gpa.free(line);
+ for (0..16800) |i| @memcpy(line[i * 7 ..][0..7], "answer ");
+ const p = try th.withFile(gpa, line);
+ defer p.deinit();
+ const t0 = std.Io.Timestamp.now(testing.io, .awake);
+ try testing.expectEqual(Status.ok, th.wr(p, Node.of(th.serialOf(p), .addr), "?a?").reply.status);
+ // Quadratic, it took seconds (6.4 s through 9P in Debug); linear, a
+ // few milliseconds.
+ const took = t0.durationTo(std.Io.Timestamp.now(testing.io, .awake)).toNanoseconds();
+ try testing.expect(took < std.time.ns_per_s);
+}
+
+test "an alternation anchoring some branches and not others is refused with why, a $ not counting" {
+ const p = try th.withFile(testing.allocator, "def\nfoo\n");
+ defer p.deinit();
+ const node = Node.of(th.serialOf(p), .addr);
+ for ([_][]const u8{ "/^def|x/", "/foo$|^zèbre/" }) |pat| {
+ const r = th.wr(p, node, pat);
+ try testing.expectEqual(E.INVAL, r.errno());
+ try testing.expectEqualStrings(regexp_.Regex.e_mixed, r.reply.ename);
+ }
+ try testing.expectEqual(Status.ok, th.wr(p, node, "/foo$|def/").reply.status);
+}
+
+test "a pattern with a newline and an anchor mvzr would misread is refused EINVAL, by addr and by Edit" {
+ const p = try th.withFile(testing.allocator, "a\nfoo\n");
+ defer p.deinit();
+ const r = th.wr(p, Node.of(th.serialOf(p), .addr), "/(^|\\n)foo/");
+ try testing.expectEqual(E.INVAL, r.errno());
+ try testing.expectEqualStrings(regexp_.Regex.e_anchor, r.reply.ename);
+ const e = th.wr(p, Node.of(th.serialOf(p), .ctl), "Edit ,x/(^|\\n)foo/d\n");
+ try testing.expectEqual(E.INVAL, e.errno());
+}
+
+test "a row's range, L:C-L2:C2 or L:C-C2, is an address through its last character" {
+ const p = try th.withFile(testing.allocator, "one\ntwo three\nfour\n");
+ defer p.deinit();
+ const addr = Node.of(th.serialOf(p), .addr);
+ const data = Node.of(th.serialOf(p), .xdata);
+ try testing.expectEqual(Status.ok, th.wr(p, addr, "2:5-2:9").reply.status);
+ try testing.expectEqualStrings("three", th.rd(p, data, 0, 64).bytes);
+ try testing.expectEqual(Status.ok, th.wr(p, addr, "2:1-3").reply.status);
+ try testing.expectEqualStrings("two", th.rd(p, data, 0, 64).bytes);
+ try testing.expectEqual(Status.ok, th.wr(p, addr, "1:2-3:2").reply.status);
+ try testing.expectEqualStrings("ne\ntwo three\nfo", th.rd(p, data, 0, 64).bytes);
+ try testing.expectEqualStrings(e_order, th.wr(p, addr, "2:5-1:1").reply.ename);
+ try testing.expectEqualStrings(e_range, th.wr(p, addr, "2:1-9:1").reply.ename);
+}
+
+test "an empty match at the search's start is passed over, as sam's nextmatch does" {
+ const p = try th.withFile(testing.allocator, "ab\ncd\nef\n");
+ defer p.deinit();
+ const addr = Node.of(th.serialOf(p), .addr);
+ const Case = struct { start: []const u8, expr: []const u8, want: []const u32 };
+ for ([_]Case{
+ // From #3 (the start of line 2), /^/ goes on to line 3.
+ .{ .start = "#3", .expr = "/^/", .want = &.{6} },
+ .{ .start = "#2", .expr = "/$/", .want = &.{ 5, 8 } },
+ .{ .start = "#3", .expr = "/x*/", .want = &.{ 4, 5, 6 } },
+ .{ .start = "#3", .expr = "?^?", .want = &.{0} },
+ }) |c| {
+ _ = th.wr(p, addr, c.start);
+ for (c.want) |q| {
+ try testing.expectEqual(Status.ok, th.wr(p, addr, c.expr).reply.status);
+ try testing.expectEqual(q, p.panes[0].?.fs.addr.q0);
+ try testing.expectEqual(q, p.panes[0].?.fs.addr.q1);
+ }
+ }
+}
+
+test "regular expressions search lines as sam's do, and a search wraps" {
+ const p = try th.withFile(testing.allocator, "alpha beta\nbeta gamma\ngamma\n");
+ defer p.deinit();
+ const serial = th.serialOf(p);
+ const addr = Node.of(serial, .addr);
+ const Case = struct { from: []const u8, expr: []const u8, q0: u32, q1: u32 };
+ for ([_]Case{
+ // ^ and $ at every line's start and end, not only the text's
+ .{ .from = "#0", .expr = "/^beta/", .q0 = 11, .q1 = 15 },
+ .{ .from = "#0", .expr = "/beta$/", .q0 = 6, .q1 = 10 },
+ .{ .from = "#0", .expr = "/^gamma$/", .q0 = 22, .q1 = 27 },
+ // . and [^...] stop at a newline; \n names one
+ .{ .from = "#0", .expr = "/a.*/", .q0 = 0, .q1 = 10 },
+ .{ .from = "#0", .expr = "/a[^x]*/", .q0 = 0, .q1 = 10 },
+ .{ .from = "#0", .expr = "/a\\nbeta/", .q0 = 9, .q1 = 15 },
+ // leftmost; of the alternatives there, mvzr's first
+ .{ .from = "#0", .expr = "/be|beta b/", .q0 = 6, .q1 = 8 },
+ .{ .from = "#0", .expr = "/gam|gamma/", .q0 = 16, .q1 = 19 },
+ .{ .from = "#0", .expr = "/(al)+pha?/", .q0 = 0, .q1 = 5 },
+ // `^` does not match where a search starts in the middle of a line
+ .{ .from = "#1", .expr = "/^/", .q0 = 11, .q1 = 11 },
+ // a search past the last match wraps to the text's start
+ .{ .from = "$", .expr = "/alpha/", .q0 = 0, .q1 = 5 },
+ // backward: the last match that ends by the start of dot
+ .{ .from = "#16", .expr = "?beta?", .q0 = 11, .q1 = 15 },
+ .{ .from = "#0", .expr = "?gamma?", .q0 = 22, .q1 = 27 },
+ }) |c| {
+ _ = th.wr(p, addr, c.from);
+ const w = th.wr(p, addr, c.expr);
+ try testing.expectEqual(Status.ok, w.reply.status);
+ try testing.expectEqual(c.q0, p.panes[0].?.fs.addr.q0);
+ try testing.expectEqual(c.q1, p.panes[0].?.fs.addr.q1);
+ }
+ // $ also matches at the end of text with no newline to end it
+ const q = try th.withFile(testing.allocator, "last line");
+ defer q.deinit();
+ _ = th.wr(q, Node.of(th.serialOf(q), .addr), "#0");
+ try testing.expectEqual(Status.ok, th.wr(q, Node.of(th.serialOf(q), .addr), "/line$/").reply.status);
+ try testing.expectEqual(@as(u32, 5), q.panes[0].?.fs.addr.q0);
+}
+
+test "a refused write to dot after a truncation changes nothing; a truncation with no write empties it at close" {
+ const p = try th.withFile(testing.allocator, "one\ntwo\nthree\n");
+ defer p.deinit();
+ const serial = th.serialOf(p);
+ const dot = Node.of(serial, .dot);
+ try testing.expectEqual(Status.ok, th.wr(p, dot, "2").reply.status);
+ const was = th.rd(p, dot, 0, 64).bytes;
+ var kept: [64]u8 = undefined;
+ @memcpy(kept[0..was.len], was);
+ // A shell's `echo /nomatch/ > dot`: truncate, then the refused write.
+ const h = th.call(p, .{ .tag = 1, .op = .open, .node = dot, .omode = 1 }).reply.handle;
+ _ = th.call(p, .{ .tag = 2, .op = .setattr, .node = dot, .truncate = true });
+ try testing.expectEqual(Status.err, th.call(p, .{ .tag = 3, .op = .write, .node = dot, .handle = h, .data = "/nomatch/" }).reply.status);
+ _ = th.call(p, .{ .tag = 4, .op = .release, .node = dot, .handle = h, .opened = true });
+ try testing.expectEqualStrings(kept[0..was.len], th.rd(p, dot, 0, 64).bytes);
+ // `: > dot`: the truncation alone empties it once its open goes.
+ const h2 = th.call(p, .{ .tag = 5, .op = .open, .node = dot, .omode = 1 }).reply.handle;
+ _ = th.call(p, .{ .tag = 6, .op = .setattr, .node = dot, .truncate = true });
+ _ = th.call(p, .{ .tag = 7, .op = .release, .node = dot, .handle = h2, .opened = true });
+ try testing.expectEqualStrings(" 0 0 ", th.rd(p, dot, 0, 64).bytes);
+}
+
+test "a failed address leaves none, so data refuses rather than act at the last one" {
+ const p = try th.withFile(testing.allocator, "one\ntwo\n");
+ defer p.deinit();
+ const serial = th.serialOf(p);
+ const addr = Node.of(serial, .addr);
+ const data = Node.of(serial, .data);
+ _ = th.wr(p, addr, "#0,#3");
+ try testing.expectEqualStrings(e_no_match, th.wr(p, addr, "/zzz/").reply.ename);
+ try testing.expectEqualStrings(pane_files.e_addr_failed, th.wr(p, data, "ONE").reply.ename);
+ try testing.expectEqualStrings(pane_files.e_addr_failed, th.rd(p, data, 0, 64).reply.ename);
+ try testing.expectEqual(Status.ok, th.rd(p, addr, 0, 64).reply.status);
+ try testing.expectEqualStrings("", th.rd(p, addr, 0, 64).bytes);
+ try testing.expectEqual(E.INVAL, th.call(p, .{ .tag = 1, .op = .setattr, .node = Node.of(serial, .xdata), .truncate = true }).errno());
+ try testing.expectEqualStrings("one\ntwo\n", p.panes[0].?.file.?.content);
+ // A good address gives it one again; truncating addr does not.
+ _ = th.wr(p, addr, "#0,#3");
+ try testing.expectEqual(Status.ok, th.wr(p, data, "ONE").reply.status);
+ try testing.expectEqualStrings("ONE\ntwo\n", p.panes[0].?.file.?.content);
+ _ = th.wr(p, addr, "99");
+ _ = th.call(p, .{ .tag = 1, .op = .setattr, .node = addr, .truncate = true });
+ try testing.expectEqualStrings(pane_files.e_addr_failed, th.wr(p, data, "0").reply.ename);
+}
+
+test "a shell's > on addr searches on from the last address, and 0 starts over" {
+ const p = try th.withFile(testing.allocator, "foo x foo y foo\n");
+ defer p.deinit();
+ const serial = th.serialOf(p);
+ const addr = Node.of(serial, .addr);
+ const data = Node.of(serial, .data);
+ // echo /foo/ > addr; printf foobar > data -- twice over, as a loop does.
+ for (0..3) |_| {
+ _ = th.call(p, .{ .tag = 1, .op = .setattr, .node = addr, .truncate = true });
+ try testing.expectEqual(Status.ok, th.wr(p, addr, "/foo/").reply.status);
+ _ = th.call(p, .{ .tag = 1, .op = .setattr, .node = data, .truncate = true });
+ try testing.expectEqual(Status.ok, th.wr(p, data, "foobar").reply.status);
+ }
+ try testing.expectEqualStrings("foobar x foobar y foobar\n", p.panes[0].?.file.?.content);
+ // .+#1 moves on from where the last write left the address.
+ _ = th.wr(p, addr, "0");
+ _ = th.call(p, .{ .tag = 1, .op = .setattr, .node = addr, .truncate = true });
+ try testing.expectEqual(Status.ok, th.wr(p, addr, ".+#1").reply.status);
+ try testing.expectEqualStrings(" 1 1 ", th.rd(p, addr, 0, 64).bytes);
+}
+
test "the address language, form by form" {
const gpa = testing.allocator;
const p = try th.withFile(gpa, "one\ntwo\nthree\n");
@@ -233,6 +581,11 @@ test "the address language, form by form" {
.{ .expr = "/two/", .q0 = 4, .q1 = 7 },
.{ .expr = "/t.o/", .q0 = 4, .q1 = 7 },
.{ .expr = "1\n", .q0 = 0, .q1 = 4 },
+ // pardes's line:col, composing like any simple address.
+ .{ .expr = "2:2", .q0 = 5, .q1 = 5 },
+ .{ .expr = "2:99", .q0 = 7, .q1 = 7 },
+ .{ .expr = "2:2,3:1", .q0 = 5, .q1 = 8 },
+ .{ .expr = "2:2+#3", .q0 = 8, .q1 = 8 },
}) |c| {
_ = th.wr(p, addr, "#0");
const w = th.wr(p, addr, c.expr);
@@ -247,30 +600,103 @@ test "the address language, form by form" {
_ = th.wr(p, addr, "1");
_ = th.wr(p, addr, ".");
- try testing.expectEqual(@as(u32, 0), p.fs.panes[0].addr.q0);
- try testing.expectEqual(@as(u32, 4), p.fs.panes[0].addr.q1);
+ try testing.expectEqual(@as(u32, 0), p.panes[0].?.fs.addr.q0);
+ try testing.expectEqual(@as(u32, 4), p.panes[0].?.fs.addr.q1);
_ = th.wr(p, addr, "$");
_ = th.wr(p, addr, "?o?");
- try testing.expectEqual(@as(u32, 6), p.fs.panes[0].addr.q0); // the `o` in "two"
- try testing.expectEqual(@as(u32, 7), p.fs.panes[0].addr.q1);
+ try testing.expectEqual(@as(u32, 6), p.panes[0].?.fs.addr.q0); // the `o` in "two"
+ try testing.expectEqual(@as(u32, 7), p.panes[0].?.fs.addr.q1);
+ // limit is its own file: copying addr onto it bounds the search, and
+ // truncating it lifts the bound again.
+ const limit = Node.of(serial, .limit);
_ = th.wr(p, addr, "1");
- _ = th.wr(p, Node.of(serial, .ctl), "limit=addr\n");
+ try testing.expectEqualStrings("", th.rd(p, limit, 0, 64).bytes);
+ var copied: [64]u8 = undefined;
+ const pair = th.rd(p, addr, 0, 64).bytes;
+ @memcpy(copied[0..pair.len], pair);
+ _ = th.wr(p, limit, copied[0..pair.len]);
+ try testing.expectEqual(@as(u32, 4), p.panes[0].?.fs.limit.?.q1);
+ try testing.expectEqualStrings(" 0 4 ", th.rd(p, limit, 0, 64).bytes);
_ = th.wr(p, addr, "#0");
try testing.expectEqual(E.INVAL, th.wr(p, addr, "/three/").errno());
- _ = th.wr(p, Node.of(serial, .ctl), "clean\n");
- _ = th.call(p, .{ .tag = 6, .op = .open, .node = Node.of(serial, .ctl) });
- try testing.expect(p.fs.panes[0].limit == null);
+ _ = th.call(p, .{ .tag = 6, .op = .setattr, .node = limit, .truncate = true });
+ try testing.expect(p.panes[0].?.fs.limit == null);
_ = th.wr(p, addr, "#0");
try testing.expectEqual(Status.ok, th.wr(p, addr, "/three/").reply.status);
- for ([_][]const u8{ "zzz", "#", "//", "/nomatch/", "1 2", "99", "/a\\" }) |bad| {
+ for ([_][]const u8{ "zzz", "#", "//", "/nomatch/", "1 2 3", "99", "/a\\" }) |bad| {
_ = th.wr(p, addr, "#0");
try testing.expectEqual(E.INVAL, th.wr(p, addr, bad).errno());
}
+ // Each failure says which it is.
+ for ([_][2][]const u8{
+ .{ "zzz", e_syntax }, .{ "/nomatch/", e_no_match }, .{ "99", e_range },
+ .{ "#999", e_range }, .{ "/a[/", e_regexp }, .{ "/(a/", e_regexp },
+ .{ "/*a/", e_regexp }, .{ "99:1", e_range }, .{ "2:0", e_col_zero },
+ }) |c| {
+ _ = th.wr(p, addr, "#0");
+ try testing.expectEqualStrings(c[1], th.wr(p, addr, c[0]).reply.ename);
+ }
- const nested = "," ** 4096;
+ const nested = &@as([4096]u8, @splat(','));
_ = th.wr(p, addr, "#0");
try testing.expectEqual(E.INVAL, th.wr(p, addr, nested).errno());
}
+
+test "a line just past the last newline is an empty one there, as sam's lineaddr has it" {
+ const Case = struct { text: []const u8, expr: []const u8, q0: u32, q1: u32 };
+ for ([_]Case{
+ .{ .text = "", .expr = "1", .q0 = 0, .q1 = 0 },
+ .{ .text = "a\n", .expr = "2", .q0 = 2, .q1 = 2 },
+ .{ .text = "a\nb", .expr = "2", .q0 = 2, .q1 = 3 },
+ .{ .text = "a\nb\n", .expr = "1", .q0 = 0, .q1 = 2 },
+ .{ .text = "a\nb\n", .expr = "0", .q0 = 0, .q1 = 0 },
+ .{ .text = "a\nb\n", .expr = "1+1", .q0 = 2, .q1 = 4 },
+ .{ .text = "a\nb\n", .expr = "$", .q0 = 4, .q1 = 4 },
+ }) |c| {
+ var a: Addr = .{ .text = c.text, .lim = null, .expr = c.expr };
+ const r = a.address(.{ .q0 = 0, .q1 = 0 }).?;
+ try testing.expectEqual(c.q0, r.q0);
+ try testing.expectEqual(c.q1, r.q1);
+ }
+ for ([_][2][]const u8{ .{ "", "2" }, .{ "a\n", "3" } }) |c| {
+ var a: Addr = .{ .text = c[0], .lim = null, .expr = c[1] };
+ try testing.expectEqual(null, a.address(.{ .q0 = 0, .q1 = 0 }));
+ try testing.expectEqualStrings(e_range, a.err);
+ }
+ // Edit's addresses are these: 1i on an empty file inserts.
+ const p = try th.withFile(testing.allocator, "");
+ defer p.deinit();
+ try testing.expectEqual(Status.ok, th.wr(p, Node.of(th.serialOf(p), .ctl), "Edit 1i/header/\n").reply.status);
+ try testing.expectEqualStrings("header", p.panes[0].?.file.?.content);
+}
+
+/// Just past the `count`-th newline at or after `from`; null if the text has
+/// fewer. Counted a block at a time (modal.lineStartOffset), where a byte at
+/// a time made `echo 7000 > addr` walk every byte above line 7000.
+fn nthLineAfter(text: []const u8, from: usize, count: usize) ?usize {
+ return from + (modal.lineStartExact(text[from..], count) orelse return null);
+}
+
+test "nthLineAfter finds what a byte at a time finds" {
+ var prng: std.Random.DefaultPrng = .init(0x51ab);
+ const random = prng.random();
+ var buf: [200]u8 = undefined;
+ for (0..3000) |_| {
+ const len = random.uintLessThan(usize, buf.len);
+ for (buf[0..len]) |*b| b.* = if (random.uintLessThan(u8, 3) == 0) '\n' else 'x';
+ const text = buf[0..len];
+ const from = random.uintLessThan(usize, len + 1);
+ const count = 1 + random.uintLessThan(usize, 40);
+ var p = from;
+ var k: usize = 0;
+ const want: ?usize = while (k < count) {
+ if (p >= text.len) break null;
+ if (text[p] == '\n') k += 1;
+ p += 1;
+ } else p;
+ try std.testing.expectEqual(want, nthLineAfter(text, from, count));
+ }
+}