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.zig42
1 files changed, 32 insertions, 10 deletions
diff --git a/src/ninep/addr.zig b/src/ninep/addr.zig
index 86d2d49e..7753c94d 100644
--- a/src/ninep/addr.zig
+++ b/src/ninep/addr.zig
@@ -239,21 +239,15 @@ pub const Addr = struct {
k = @intFromBool(a.text[p] == '\n');
p += 1;
}
- while (k < n) {
- if (p >= nc) {
+ if (k < n) {
+ p = nthLineAfter(a.text, p, n - k) orelse {
a.err = e_range;
return null;
- }
- if (a.text[p] == '\n') k += 1;
- p += 1;
+ };
}
p1 = p;
}
- while (p < nc) {
- const ch = a.text[p];
- p += 1;
- if (ch == '\n') break;
- }
+ p = if (std.mem.indexOfScalarPos(u8, a.text, p, '\n')) |nl| nl + 1 else nc;
return .{ .q0 = clip(p1), .q1 = clip(p) };
}
@@ -678,3 +672,31 @@ test "a line just past the last newline is an empty one there, as sam's lineaddr
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));
+ }
+}