summaryrefslogtreecommitdiff
path: root/src/modal.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/modal.zig')
-rw-r--r--src/modal.zig139
1 files changed, 105 insertions, 34 deletions
diff --git a/src/modal.zig b/src/modal.zig
index 82f1a3a0..ad06833f 100644
--- a/src/modal.zig
+++ b/src/modal.zig
@@ -989,13 +989,77 @@ pub fn lineCount(content: []const u8) usize {
// byte offset of the start of line `row` (0-based). row may == lineCount()
// (== content.len, the end).
pub fn lineStartOffset(content: []const u8, row: usize) usize {
+ // Newlines counted 32 bytes at a time, the last block byte by byte: one
+ // indexOfScalarPos per line was a call per line, 7000 of them to find
+ // line 7000 of a source, for every cursor move, selection and language
+ // request there.
+ const block = 32;
+ const Block = @Vector(block, u8);
var off: usize = 0;
- var r: usize = 0;
- while (r < row) : (r += 1) {
- const nl = std.mem.indexOfScalarPos(u8, content, off, '\n') orelse return content.len;
- off = nl + 1;
+ var left = row;
+ if (left == 0) return 0;
+ while (off + block <= content.len) : (off += block) {
+ const chunk: Block = content[off..][0..block].*;
+ const found: usize = @popCount(@as(u32, @bitCast(chunk == @as(Block, @splat('\n')))));
+ if (found >= left) break;
+ left -= found;
}
- return off;
+ while (off < content.len) : (off += 1) if (content[off] == '\n') {
+ left -= 1;
+ if (left == 0) return off + 1;
+ };
+ return content.len;
+}
+
+test "lineStartOffset by blocks and offsetAt agree with a line at a time" {
+ var prng: std.Random.DefaultPrng = .init(0x9e3779b9);
+ const random = prng.random();
+ var buf: [300]u8 = undefined;
+ for (0..2000) |_| {
+ const len = random.uintLessThan(usize, buf.len);
+ for (buf[0..len]) |*b| b.* = if (random.uintLessThan(u8, 4) == 0) '\n' else 'a';
+ const text = buf[0..len];
+ for (0..len + 3) |row| {
+ var off: usize = 0;
+ var r: usize = 0;
+ const want = while (r < row) : (r += 1) {
+ off = (std.mem.indexOfScalarPos(u8, text, off, '\n') orelse break text.len) + 1;
+ } else off;
+ try std.testing.expectEqual(want, lineStartOffset(text, row));
+ const cursor: Cursor = .{ .row = row, .col = random.uintLessThan(usize, 6) };
+ const clamped = lineStartOffset(text, @min(row, cursorLineCount(text) - 1));
+ const end = std.mem.indexOfScalarPos(u8, text, clamped, '\n') orelse text.len;
+ try std.testing.expectEqual(clamped + graphemeStart(text[clamped..end], @min(cursor.col, end - clamped)), offsetAt(text, cursor));
+ // lineSpan, as it was: a line at a time, null past the last.
+ const span: ?LineSpan = span: {
+ if (text.len == 0) break :span null;
+ var start: usize = 0;
+ for (0..row) |_| start = (std.mem.indexOfScalarPos(u8, text, start, '\n') orelse break :span null) + 1;
+ if (start == text.len and !(row == 0 or text[text.len - 1] == '\n')) break :span null;
+ break :span .{ .start = start, .end = std.mem.indexOfScalarPos(u8, text, start, '\n') orelse text.len };
+ };
+ try std.testing.expectEqual(span, lineSpan(text, row));
+ // rangeBytes, as it was: each line looked for from the top, twice.
+ const other: Cursor = .{ .row = random.uintLessThan(usize, len + 3), .col = random.uintLessThan(usize, 6) };
+ var lo = cursor;
+ var hi = other;
+ if (hi.row < lo.row or (hi.row == lo.row and hi.col < lo.col)) std.mem.swap(Cursor, &lo, &hi);
+ const lo_line = lineSlice(text, lo.row);
+ const hi_line = lineSlice(text, hi.row);
+ const s0 = lineStartOffset(text, lo.row) + graphemeStart(lo_line, @min(lo.col, lo_line.len));
+ var e0 = lineStartOffset(text, hi.row) + graphemeStart(hi_line, @min(hi.col, hi_line.len));
+ if (e0 < text.len) e0 = nextGrapheme(text, e0);
+ const got = rangeBytes(text, cursor, other);
+ try std.testing.expectEqual(s0, got.s);
+ try std.testing.expectEqual(@max(s0, e0), got.e);
+ }
+ }
+}
+
+/// The line that starts at `start` (lineSlice's, once its start is known).
+fn lineFrom(content: []const u8, start: usize) []const u8 {
+ if (start >= content.len) return "";
+ return content[start .. std.mem.indexOfScalarPos(u8, content, start, '\n') orelse content.len];
}
// the text of line `row` (no terminator), a slice into `content`.
@@ -1011,23 +1075,29 @@ pub fn lineSlice(content: []const u8, row: usize) []const u8 {
/// A bounded scan of one line; null if the row does not exist.
pub const LineSpan = struct { start: usize, end: usize };
+/// Just past the `row`-th newline; null if the text has fewer.
+pub fn lineStartExact(content: []const u8, row: usize) ?usize {
+ const at = lineStartOffset(content, row);
+ // At the end: either the row-th newline was the last byte, or there were fewer.
+ if (at == content.len and std.mem.count(u8, content, "\n") < row) return null;
+ return at;
+}
+
pub fn lineSpan(content: []const u8, row: usize) ?LineSpan {
// Match lineCount: empty content has no lines; a trailing newline adds one.
if (content.len == 0) return null;
- var start: usize = 0;
- var r: usize = 0;
- while (r < row) : (r += 1) {
- const nl = std.mem.indexOfScalarPos(u8, content, start, '\n') orelse return null;
- start = nl + 1;
- }
- if (start > content.len) return null;
+ const start = lineStartExact(content, row) orelse return null;
if (start == content.len and !(row == 0 or content.len == 0 or content[content.len - 1] == '\n')) return null;
const end = std.mem.indexOfScalarPos(u8, content, start, '\n') orelse content.len;
return .{ .start = start, .end = end };
}
-fn spliceAlloc(alloc: std.mem.Allocator, content: []const u8, start: usize, end: usize, replacement: []const u8) ![]u8 {
- const out = try alloc.alloc(u8, content.len - (end - start) + replacement.len);
+pub fn spliceAlloc(alloc: std.mem.Allocator, content: []const u8, start: usize, end: usize, replacement: []const u8) ![]u8 {
+ // Raw: every byte is written below, where `alloc` fills a safe build's
+ // new memory first, a whole file's worth per keystroke.
+ const len = content.len - (end - start) + replacement.len;
+ if (len == 0) return alloc.alloc(u8, 0);
+ const out = (alloc.rawAlloc(len, .of(u8), @returnAddress()) orelse return error.OutOfMemory)[0..len];
@memcpy(out[0..start], content[0..start]);
@memcpy(out[start..][0..replacement.len], replacement);
@memcpy(out[start + replacement.len ..], content[end..]);
@@ -1036,13 +1106,18 @@ fn spliceAlloc(alloc: std.mem.Allocator, content: []const u8, start: usize, end:
/// insert `text` at (row, col). col is clamped to the line length.
pub fn insertAt(alloc: std.mem.Allocator, content: []const u8, c: Cursor, text: []const u8) ![]u8 {
+ const off = insertOffset(content, c);
+ return spliceAlloc(alloc, content, off, off, text);
+}
+
+/// Where insertAt puts its text: col clamped to the line, no further.
+pub fn insertOffset(content: []const u8, c: Cursor) usize {
// Only an out-of-range row requires counting the whole document.
const span = lineSpan(content, c.row) orelse blk: {
const last = lineCount(content) -| 1;
break :blk lineSpan(content, last) orelse LineSpan{ .start = content.len, .end = content.len };
};
- const off = span.start + @min(c.col, span.end - span.start);
- return spliceAlloc(alloc, content, off, off, text);
+ return span.start + @min(c.col, span.end - span.start);
}
// delete the character at (row, col). no-op if col is past the line end.
@@ -1105,12 +1180,15 @@ fn rangeBytes(content: []const u8, a: Cursor, b: Cursor) struct { s: usize, e: u
lo = b;
hi = a;
}
- const lo_line = lineSlice(content, lo.row);
- const hi_line = lineSlice(content, hi.row);
+ // Each line found once, the second from the first.
+ const lo_start = lineStartOffset(content, lo.row);
+ const hi_start = if (hi.row == lo.row) lo_start else lo_start + lineStartOffset(content[lo_start..], hi.row - lo.row);
+ const lo_line = lineFrom(content, lo_start);
+ const hi_line = lineFrom(content, hi_start);
const lo_col = graphemeStart(lo_line, @min(lo.col, lo_line.len));
const hi_col = graphemeStart(hi_line, @min(hi.col, hi_line.len));
- const s = lineStartOffset(content, lo.row) + lo_col;
- var e = lineStartOffset(content, hi.row) + hi_col;
+ const s = lo_start + lo_col;
+ var e = hi_start + hi_col;
if (e < content.len) e = nextGrapheme(content, e); // include the grapheme under the head
return .{ .s = s, .e = @max(s, e) };
}
@@ -1138,12 +1216,12 @@ pub fn clearLine(alloc: std.mem.Allocator, content: []const u8, row: usize) ![]u
// paste `text` as a new line BELOW `row`. Multiline `text` becomes several lines.
pub fn pasteLineBelow(alloc: std.mem.Allocator, content: []const u8, row: usize, text: []const u8) ![]u8 {
- const n = lineCount(content);
- const off = if (row + 1 < n) lineStartOffset(content, row + 1) else content.len;
+ // The next line's start, or the end: no whole-text line count to know which.
+ const off = lineStartExact(content, row + 1) orelse content.len;
const suffix_newline = off < content.len or (content.len > 0 and content[content.len - 1] == '\n');
const prefix_newline = !suffix_newline and off > 0;
const extra: usize = @intFromBool(suffix_newline or prefix_newline);
- const out = try alloc.alloc(u8, content.len + text.len + extra);
+ const out = try @import("memory.zig").unfilled(alloc, content.len + text.len + extra);
var write: usize = 0;
@memcpy(out[write..][0..off], content[0..off]);
write += off;
@@ -1393,8 +1471,10 @@ pub fn positionAt(text: []const u8, off: usize) Cursor {
/// (row, col) -> clamped gap offset; col == line length lands ON the '\n'
pub fn offsetAt(text: []const u8, c: Cursor) usize {
- const row = @min(c.row, cursorLineCount(text) - 1);
- const s = lineStartOffset(text, row);
+ // A row past the last is the last: found back from the end, where
+ // counting every line of the text to clamp it cost a pass over all of it.
+ var s = lineStartOffset(text, c.row);
+ if (s == text.len) s = if (std.mem.lastIndexOfScalar(u8, text, '\n')) |nl| nl + 1 else 0;
// lineEndOffset(text, row) inlined: it starts by walking to `row` again,
// and we are already standing there
const e = std.mem.indexOfScalarPos(u8, text, s, '\n') orelse text.len;
@@ -1783,15 +1863,6 @@ test "selection textobjects distinguish words and paragraph boundaries" {
try std.testing.expectEqual(@as(usize, 7), r.head);
}
-/// leading-whitespace visual width (tab -> next multiple of INDENT_W)
-pub fn indentWidth(line: []const u8) usize {
- var w: usize = 0;
- for (line) |ch| {
- if (ch == ' ') w += 1 else if (ch == '\t') w = (w / INDENT_W + 1) * INDENT_W else break;
- }
- return w;
-}
-
/// A line's indent as it is, tabs and spaces both, as acme's autoindent
/// copies it: what o/O/insert-newline give the new line.
pub fn indentText(line: []const u8) []const u8 {