summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ninep/addr.zig69
1 files changed, 27 insertions, 42 deletions
diff --git a/src/ninep/addr.zig b/src/ninep/addr.zig
index 7753c94d..710dca38 100644
--- a/src/ninep/addr.zig
+++ b/src/ninep/addr.zig
@@ -7,9 +7,7 @@ const pane_files = @import("pane.zig");
pub const Range = pane_files.State.Range;
-fn clip(n: usize) u32 {
- return std.math.cast(u32, n) orelse std.math.maxInt(u32);
-}
+const clip = pane_files.clip;
pub const e_no_match = "no match for regexp";
pub const e_range = "address out of range";
@@ -149,7 +147,10 @@ pub const Addr = struct {
// `-/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;
+ r = (a.regexp(r, a.pattern(c), back) catch {
+ a.err = e_slow;
+ return null;
+ }) orelse return null;
dir = 0;
size = .line;
},
@@ -239,12 +240,12 @@ pub const Addr = struct {
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;
- };
- }
+ // 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;
@@ -255,39 +256,30 @@ pub const Addr = struct {
/// 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) });
+ /// 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); null (the outer) when too slow.
- fn backward(a: *Addr, rx: *regexp_.Regex, limit: u32) ??Range {
+ /// (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 {
- a.err = e_slow;
- return null;
- }) orelse break;
+ 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 @as(?Range, before orelse last);
+ return before orelse last;
}
- fn regexp(a: *Addr, r: Range, pat: []const u8, back: bool) ?Range {
+ 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;
@@ -308,18 +300,18 @@ pub const Addr = struct {
// 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;
+ var f = try a.backward(&rx, r.q0);
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;
+ 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 = a.forward(&rx, from, hi) orelse return null;
+ 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 = a.forward(&rx, next, hi) orelse return null;
+ f = try a.forward(&rx, next, hi);
};
break :found f;
};
@@ -673,14 +665,7 @@ test "a line just past the last newline is an empty one there, as sam's lineaddr
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" {
+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;
@@ -697,6 +682,6 @@ test "nthLineAfter finds what a byte at a time finds" {
if (text[p] == '\n') k += 1;
p += 1;
} else p;
- try std.testing.expectEqual(want, nthLineAfter(text, from, count));
+ try std.testing.expectEqual(want, if (modal.lineStartExact(text[from..], count)) |at| from + at else null);
}
}