summaryrefslogtreecommitdiff
path: root/core/ninep/addr.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-18 22:03:14 -0300
committerGabriel Schneider <[email protected]>2026-11-18 22:23:51 -0300
commit99f5b10eccd95aa0066ea6fe6bb75f367ab56a47 (patch)
tree813596ea84318a7bae4fd7416ba463de8e3b7107 /core/ninep/addr.zig
parent92d553123673376adf6126ab90d49a22c5b833d2 (diff)
downloadpardes-99f5b10eccd95aa0066ea6fe6bb75f367ab56a47.tar.gz
pardes-99f5b10eccd95aa0066ea6fe6bb75f367ab56a47.zip
core: src/ becomes core/, pardes.zig its root.zig (the reorganisation's tree, step 2d), and every mention of a src/ path follows: the build's paths, the core's imports of its root, the tests that read the repository's files by path (lspbench's, look.zig's spans over core/look.zig), the comments that cite files, the documents and the manual (the typst corpus reads core/tutor.txt and core/plumbing); left as they were: the /src tree's served names (src/pardes.zig and the rest are the 9P tree's, not the repository's), the CHANGELOG's history, fixtures that are only strings; fs-unit-test runs from the core's root (a module of the core rooted at fs.zig had the runtime's and the devices' `core` be fs.zig); nothing else changes: the ledger (pardes.test.* now root.test.*), frames byte-identical, replicas and process-test the base's
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/ninep/addr.zig')
-rw-r--r--core/ninep/addr.zig680
1 files changed, 680 insertions, 0 deletions
diff --git a/core/ninep/addr.zig b/core/ninep/addr.zig
new file mode 100644
index 00000000..72a2d815
--- /dev/null
+++ b/core/ninep/addr.zig
@@ -0,0 +1,680 @@
+//! 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 regexp_ = @import("../regexp.zig");
+const modal = @import("../modal.zig");
+const pane_files = @import("pane.zig");
+
+pub const Range = pane_files.State.Range;
+
+const clip = pane_files.clip;
+
+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,
+ lim: ?Range,
+ 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 };
+
+ pub fn address(a: *Addr, ar_in: Range) ?Range {
+ const start = a.i;
+ var ar = ar_in;
+ var r = ar_in;
+ var dir: u8 = 0;
+ var size: Size = .line;
+ var c: u8 = 0;
+ while (a.i < a.expr.len) {
+ const prevc = c;
+ c = a.expr[a.i];
+ a.i += 1;
+ switch (c) {
+ ',', ';' => {
+ if (c == ';') ar = r;
+ if (prevc == 0) r.q0 = 0; // lhs defaults to 0
+ if (a.i >= a.expr.len) {
+ r.q1 = clip(a.text.len); // rhs defaults to $
+ } else {
+ if (a.depth >= max_depth) return null;
+ a.depth += 1;
+ const nr = a.address(ar) orelse return null;
+ a.depth -= 1;
+ r.q1 = nr.q1;
+ }
+ return r;
+ },
+ '+', '-' => {
+ if (prevc == '+' or prevc == '-') {
+ const nc = if (a.i < a.expr.len) a.expr[a.i] else 0;
+ if (nc != '#' and nc != '/' and nc != '?')
+ r = a.number(r, 1, prevc, .line) orelse return null;
+ }
+ dir = c;
+ },
+ '.', '$' => {
+ if (a.i != start + 1) {
+ a.i -= 1;
+ return r;
+ }
+ r = if (c == '.') ar else .{ .q0 = clip(a.text.len), .q1 = clip(a.text.len) };
+ dir = if (a.i < a.expr.len) '+' else 0;
+ },
+ '#', '0'...'9' => {
+ var digit = c;
+ if (c == '#') {
+ if (a.i >= a.expr.len or a.expr[a.i] < '0' or a.expr[a.i] > '9') {
+ a.i -= 1;
+ return r;
+ }
+ digit = a.expr[a.i];
+ a.i += 1;
+ size = .char;
+ }
+ var n: u64 = digit - '0';
+ while (a.i < a.expr.len) : (a.i += 1) {
+ const d = a.expr[a.i];
+ 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;
+ },
+ '/', '?' => {
+ // `-/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) catch {
+ a.err = e_slow;
+ return null;
+ }) orelse return null;
+ dir = 0;
+ size = .line;
+ },
+ else => {
+ a.i -= 1;
+ return r;
+ },
+ }
+ }
+ if (dir != 0) r = a.number(r, 1, dir, .line) orelse return null;
+ return r;
+ }
+
+ fn pattern(a: *Addr, delim: u8) []const u8 {
+ const s = a.i;
+ while (a.i < a.expr.len) {
+ const c = a.expr[a.i];
+ if (c == '\n') break;
+ a.i += 1;
+ if (c == '\\') {
+ if (a.i < a.expr.len) a.i += 1;
+ continue;
+ }
+ if (c == delim) return a.expr[s .. a.i - 1];
+ }
+ return a.expr[s..a.i];
+ }
+
+ fn number(a: *Addr, r_in: Range, n: u32, dir: u8, size: Size) ?Range {
+ var r = r_in;
+ if (size == .char) {
+ var off: i64 = n;
+ if (dir == '+') {
+ off = @as(i64, r.q1) + n;
+ } else if (dir == '-') {
+ 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))) {
+ a.err = e_range;
+ return null;
+ }
+ const g = clip(modal.runeStart(a.text, @intCast(off)));
+ return .{ .q0 = g, .q1 = g };
+ }
+ var line: i64 = n;
+ var q0: usize = r.q0;
+ var q1: usize = r.q1;
+ switch (dir) {
+ '-' => {
+ if (q0 < a.text.len) while (q0 > 0 and a.text[q0 - 1] != '\n') {
+ q0 -= 1;
+ };
+ q1 = q0;
+ while (line > 0 and q0 > 0) {
+ if (a.text[q0 - 1] == '\n') {
+ line -= 1;
+ q1 = q0;
+ }
+ q0 -= 1;
+ }
+ 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) };
+ },
+ else => {},
+ }
+ // 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;
+ }
+ // Counted a block at a time (modal.lineStartExact), where a byte
+ // at a time made `echo 7000 > addr` walk every byte above it.
+ if (k < n) p += modal.lineStartExact(a.text[p..], n - k) orelse {
+ a.err = e_range;
+ return null;
+ };
+ p1 = p;
+ }
+ 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).
+ fn forward(a: *Addr, rx: *regexp_.Regex, from: usize, hi: usize) error{Slow}!?Range {
+ const ahead = rx.find(a.text, from, hi, hi) catch return error.Slow;
+ const m = ahead orelse (if (a.lim != null or from == 0) null else rx.find(a.text, 0, from - 1, hi) catch return error.Slow) orelse return null;
+ return .{ .q0 = clip(m.start), .q1 = clip(m.end) };
+ }
+
+ /// The last match ending at or before `limit`, else the last of all
+ /// (a search wraps).
+ fn backward(a: *Addr, rx: *regexp_.Regex, limit: u32) error{Slow}!?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 return error.Slow) 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 before orelse last;
+ }
+
+ fn regexp(a: *Addr, r: Range, pat: []const u8, back: bool) error{Slow}!?Range {
+ if (pat.len == 0) {
+ a.err = "no previous regular expression";
+ return null;
+ }
+ var rx = regexp_.Regex.compile(pat) catch |err| {
+ a.err = if (err == error.Bad) e_regexp else regexp_.Regex.message(err);
+ 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 = try a.backward(&rx, r.q0);
+ if (f) |m| if (m.q0 == m.q1 and m.q1 == r.q0) {
+ f = try a.backward(&rx, if (r.q0 == 0) clip(a.text.len) else r.q0 - 1);
+ };
+ 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 = try a.forward(&rx, from, hi);
+ 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 = try a.forward(&rx, next, hi);
+ };
+ 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;
+ };
+ }
+};
+
+const testing = std.testing;
+const th = @import("testing.zig");
+const tree = @import("tree.zig");
+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.message(error.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.message(error.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");
+ defer p.deinit();
+ const serial = th.serialOf(p);
+ const addr = Node.of(serial, .addr);
+
+ const Case = struct { expr: []const u8, q0: u32, q1: u32 };
+ for ([_]Case{
+ .{ .expr = "#0", .q0 = 0, .q1 = 0 },
+ .{ .expr = "#5", .q0 = 5, .q1 = 5 },
+ .{ .expr = "0", .q0 = 0, .q1 = 0 },
+ .{ .expr = "1", .q0 = 0, .q1 = 4 },
+ .{ .expr = "2", .q0 = 4, .q1 = 8 },
+ .{ .expr = "$", .q0 = 14, .q1 = 14 },
+ .{ .expr = ",", .q0 = 0, .q1 = 14 },
+ .{ .expr = "1,2", .q0 = 0, .q1 = 8 },
+ .{ .expr = "#1,#4", .q0 = 1, .q1 = 4 },
+ .{ .expr = "2+1", .q0 = 8, .q1 = 14 },
+ .{ .expr = "$-1", .q0 = 8, .q1 = 14 },
+ .{ .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);
+ try testing.expectEqual(Status.ok, w.reply.status);
+ const got = th.rd(p, addr, 0, 64);
+ var want: [32]u8 = undefined;
+ try testing.expectEqualStrings(
+ try std.fmt.bufPrint(&want, "{d:>11} {d:>11} ", .{ c.q0, c.q1 }),
+ got.bytes,
+ );
+ }
+
+ _ = th.wr(p, addr, "1");
+ _ = th.wr(p, addr, ".");
+ 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.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");
+ 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.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 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 = &@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);
+}
+
+test "lineStartExact past an offset 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, if (modal.lineStartExact(text[from..], count)) |at| from + at else null);
+ }
+}