@@ -51,12 +51,6 @@ fn sub(a: std.mem.Allocator, line: []const u8, start: usize, end: usize) []const
5151 return a.dupe(u8, line[s..e]) catch "";
5252 }
5353
54-fn cat(a: std.mem.Allocator, parts: []const []const u8) []const u8 {
55- var out: std.ArrayList(u8) = .empty;
56- for (parts) |p| out.appendSlice(a, p) catch {};
57- return out.items;
58-}
59-
6054 fn wordStart(line: []const u8, col: usize) usize {
6155 var i = col;
6256 while (i > 0 and isSpace(cpAt(line, i - 1))) i -= 1;
@@ -80,32 +74,41 @@ pub const Render = struct {
8074
8175 pub const Action = enum { submit, changed, none };
8276
77+const Line = std.ArrayList(u8);
78+
8379 pub const Editor = struct {
84- lines: std.ArrayList([]const u8) = .empty,
80+ lines: std.ArrayList(Line) = .empty,
8581 row: usize = 0,
8682 col: usize = 0,
8783 width: usize = 80,
88- a: std.mem.Allocator,
84+ arena: std.heap.ArenaAllocator,
85+ a: std.mem.Allocator = undefined,
8986 s: std.mem.Allocator = undefined,
9087
91- pub fn init(a: std.mem.Allocator) Editor {
92- var e = Editor{ .a = a };
93- e.lines.append(a, "") catch {};
94- return e;
88+ pub fn init(backing: std.mem.Allocator) Editor {
89+ return .{ .arena = std.heap.ArenaAllocator.init(backing) };
90+ }
91+
92+ pub fn bind(self: *Editor) void {
93+ self.a = self.arena.allocator();
94+ self.lines = .empty;
95+ self.lines.append(self.a, .empty) catch {};
9596 }
9697
9798 pub fn contents(self: *Editor) []const u8 {
9899 var out: std.ArrayList(u8) = .empty;
99100 for (self.lines.items, 0..) |line, i| {
100101 if (i > 0) out.append(self.a, '\n') catch {};
101- out.appendSlice(self.a, line) catch {};
102+ out.appendSlice(self.a, line.items) catch {};
102103 }
103104 return out.items;
104105 }
105106
106107 pub fn clear(self: *Editor) void {
108+ _ = self.arena.reset(.retain_capacity);
109+ self.a = self.arena.allocator();
107110 self.lines = .empty;
108- self.lines.append(self.a, "") catch {};
111+ self.lines.append(self.a, .empty) catch {};
109112 self.row = 0;
110113 self.col = 0;
111114 }
@@ -113,10 +116,14 @@ pub const Editor = struct {
113116 pub fn setText(self: *Editor, draft: []const u8) void {
114117 self.lines = .empty;
115118 var it = std.mem.splitScalar(u8, draft, '\n');
116- while (it.next()) |line| self.lines.append(self.a, self.a.dupe(u8, line) catch "") catch {};
117- if (self.lines.items.len == 0) self.lines.append(self.a, "") catch {};
119+ while (it.next()) |text| {
120+ var line: Line = .empty;
121+ line.appendSlice(self.a, text) catch {};
122+ self.lines.append(self.a, line) catch {};
123+ }
124+ if (self.lines.items.len == 0) self.lines.append(self.a, .empty) catch {};
118125 self.row = self.lines.items.len - 1;
119- self.col = cpLen(self.lines.items[self.row]);
126+ self.col = cpLen(self.lines.items[self.row].items);
120127 }
121128
122129 pub fn handle(self: *Editor, key: input.Key) Action {
@@ -171,7 +178,7 @@ pub const Editor = struct {
171178 return .changed;
172179 },
173180 .end => {
174- self.col = cpLen(self.lines.items[self.row]);
181+ self.col = cpLen(self.lines.items[self.row].items);
175182 return .changed;
176183 },
177184 .doc_start => {
@@ -181,7 +188,7 @@ pub const Editor = struct {
181188 },
182189 .doc_end => {
183190 self.row = self.lines.items.len - 1;
184- self.col = cpLen(self.lines.items[self.row]);
191+ self.col = cpLen(self.lines.items[self.row].items);
185192 return .changed;
186193 },
187194 else => return .none,
@@ -221,7 +228,7 @@ pub const Editor = struct {
221228
222229 fn caret(self: *Editor) struct { row: usize, col: usize } {
223230 const width = @max(self.width, 1);
224- const line = self.lines.items[self.row];
231+ const line = self.lines.items[self.row].items;
225232 const cell = self.cells(line)[self.col];
226233 const rows = render.wrapLine(self.s, render.expandTabs(self.s, line, TAB), width);
227234 var used: usize = 0;
@@ -242,10 +249,10 @@ pub const Editor = struct {
242249 var caret_line: usize = 0;
243250 var caret_cell: usize = 0;
244251 for (self.lines.items, 0..) |line, i| {
245- heights[i] = physicalRows(render.expandTabs(self.s, line, TAB), self.width);
252+ heights[i] = physicalRows(render.expandTabs(self.s, line.items, TAB), self.width);
246253 if (i == self.row) {
247254 caret_line = i;
248- caret_cell = self.cells(line)[self.col];
255+ caret_cell = self.cells(line.items)[self.col];
249256 }
250257 }
251258 var start = caret_line;
@@ -259,52 +266,59 @@ pub const Editor = struct {
259266 used += heights[end];
260267 end += 1;
261268 }
269+ var rows: std.ArrayList([]const u8) = .empty;
270+ for (self.lines.items[start..end]) |line| rows.append(self.s, line.items) catch {};
262271 return .{
263- .rows = self.lines.items[start..end],
272+ .rows = rows.items,
264273 .cursor_row = caret_line - start,
265274 .cursor_col = caret_cell,
266275 };
267276 }
268277
269278 fn insert(self: *Editor, draft: []const u8) void {
270- const current = self.lines.items[self.row];
271- var parts = std.mem.splitScalar(u8, draft, '\n');
272- const first = parts.next() orelse "";
273- var rest: std.ArrayList([]const u8) = .empty;
274- while (parts.next()) |p| rest.append(self.a, p) catch {};
275- const before = sub(self.a, current, 0, self.col);
276- const after = sub(self.a, current, self.col, cpLen(current));
277- if (rest.items.len == 0) {
278- self.lines.items[self.row] = cat(self.a, &.{ before, first, after });
279- self.col += cpLen(first);
279+ const line = &self.lines.items[self.row];
280+ const at = byteOf(line.items, self.col);
281+ if (std.mem.indexOfScalar(u8, draft, '\n') == null) {
282+ line.insertSlice(self.a, at, draft) catch {};
283+ self.col += cpLen(draft);
280284 return;
281285 }
282- const head = cat(self.a, &.{ before, first });
283- const tail = cat(self.a, &.{ rest.items[rest.items.len - 1], after });
284- self.lines.items[self.row] = head;
285- var insert_at = self.row + 1;
286- for (rest.items[0 .. rest.items.len - 1]) |m| {
287- self.lines.insert(self.a, insert_at, m) catch {};
288- insert_at += 1;
286+ const tail = self.a.dupe(u8, line.items[at..]) catch "";
287+ line.shrinkRetainingCapacity(at);
288+ var parts = std.mem.splitScalar(u8, draft, '\n');
289+ line.appendSlice(self.a, parts.next() orelse "") catch {};
290+ var fresh: std.ArrayList(Line) = .empty;
291+ while (parts.next()) |text| {
292+ var l: Line = .empty;
293+ l.appendSlice(self.a, text) catch {};
294+ fresh.append(self.s, l) catch {};
295+ }
296+ const lasti = fresh.items.len - 1;
297+ const anchor = cpLen(fresh.items[lasti].items);
298+ fresh.items[lasti].appendSlice(self.a, tail) catch {};
299+ var at_row = self.row + 1;
300+ for (fresh.items) |l| {
301+ self.lines.insert(self.a, at_row, l) catch {};
302+ at_row += 1;
289303 }
290- self.lines.insert(self.a, insert_at, tail) catch {};
291- self.row += rest.items.len;
292- self.col = cpLen(rest.items[rest.items.len - 1]);
304+ self.row = at_row - 1;
305+ self.col = anchor;
293306 }
294307
295308 fn backspace(self: *Editor) void {
296309 if (self.col > 0) {
297- const line = self.lines.items[self.row];
298- const before = sub(self.a, line, 0, self.col - 1);
299- const after = sub(self.a, line, self.col, cpLen(line));
300- self.lines.items[self.row] = cat(self.a, &.{ before, after });
310+ const line = &self.lines.items[self.row];
311+ const from = byteOf(line.items, self.col - 1);
312+ const to = byteOf(line.items, self.col);
313+ line.replaceRange(self.a, from, to - from, "") catch {};
301314 self.col -= 1;
302315 return;
303316 }
304317 if (self.row > 0) {
305- const prev = self.lines.items[self.row - 1];
306- const previous = cpLen(prev);
307- self.lines.items[self.row - 1] = cat(self.a, &.{ prev, self.lines.items[self.row] });
318+ const prev = &self.lines.items[self.row - 1];
319+ const cur = self.lines.items[self.row];
320+ const previous = cpLen(prev.items);
321+ prev.appendSlice(self.a, cur.items) catch {};
308322 _ = self.lines.orderedRemove(self.row);
309323 self.row -= 1;
310324 self.col = previous;
@@ -312,16 +326,18 @@ pub const Editor = struct {
312326 }
313327
314328 fn deleteForward(self: *Editor) void {
315- const line = self.lines.items[self.row];
316- const n = cpLen(line);
329+ const line = &self.lines.items[self.row];
330+ const n = cpLen(line.items);
317331 if (self.col < n) {
318- const before = sub(self.a, line, 0, self.col);
319- const after = sub(self.a, line, self.col + 1, n);
320- self.lines.items[self.row] = cat(self.a, &.{ before, after });
332+ const from = byteOf(line.items, self.col);
333+ const to = byteOf(line.items, self.col + 1);
334+ line.replaceRange(self.a, from, to - from, "") catch {};
321335 return;
322336 }
323337 if (self.row < self.lines.items.len - 1) {
324- self.lines.items[self.row] = cat(self.a, &.{ line, self.lines.items[self.row + 1] });
338+ const cur = &self.lines.items[self.row];
339+ const next = self.lines.items[self.row + 1];
340+ cur.appendSlice(self.a, next.items) catch {};
325341 _ = self.lines.orderedRemove(self.row + 1);
326342 }
327343 }
@@ -331,12 +347,12 @@ pub const Editor = struct {
331347 self.col -= 1;
332348 } else if (self.row > 0) {
333349 self.row -= 1;
334- self.col = cpLen(self.lines.items[self.row]);
350+ self.col = cpLen(self.lines.items[self.row].items);
335351 }
336352 }
337353
338354 fn right(self: *Editor) void {
339- if (self.col < cpLen(self.lines.items[self.row])) {
355+ if (self.col < cpLen(self.lines.items[self.row].items)) {
340356 self.col += 1;
341357 } else if (self.row < self.lines.items.len - 1) {
342358 self.row += 1;
@@ -346,7 +362,7 @@ pub const Editor = struct {
346362
347363 fn up(self: *Editor) void {
348364 const width = @max(self.width, 1);
349- const line = self.lines.items[self.row];
365+ const line = self.lines.items[self.row].items;
350366 const ct = self.caret();
351367 if (ct.row > 0) {
352368 self.col = self.colAtCell(line, self.cells(line)[self.col] - width);
@@ -354,7 +370,7 @@ pub const Editor = struct {
354370 }
355371 if (self.row == 0) return;
356372 self.row -= 1;
357- const previous = self.lines.items[self.row];
373+ const previous = self.lines.items[self.row].items;
358374 const expanded = render.expandTabs(self.s, previous, TAB);
359375 const last_row = render.wrapLine(self.s, expanded, width).len - 1;
360376 self.col = self.colAtCell(previous, last_row * width + ct.col);
@@ -362,7 +378,7 @@ pub const Editor = struct {
362378
363379 fn down(self: *Editor) void {
364380 const width = @max(self.width, 1);
365- const line = self.lines.items[self.row];
381+ const line = self.lines.items[self.row].items;
366382 const ct = self.caret();
367383 const expanded = render.expandTabs(self.s, line, TAB);
368384 if (ct.row < render.wrapLine(self.s, expanded, width).len - 1) {
@@ -371,11 +387,11 @@ pub const Editor = struct {
371387 }
372388 if (self.row == self.lines.items.len - 1) return;
373389 self.row += 1;
374- self.col = self.colAtCell(self.lines.items[self.row], ct.col);
390+ self.col = self.colAtCell(self.lines.items[self.row].items, ct.col);
375391 }
376392
377393 fn wordLeft(self: *Editor) void {
378- const line = self.lines.items[self.row];
394+ const line = self.lines.items[self.row].items;
379395 const start = wordStart(line, self.col);
380396 if (start != self.col) {
381397 self.col = start;
@@ -383,12 +399,12 @@ pub const Editor = struct {
383399 }
384400 if (self.row == 0) return;
385401 self.row -= 1;
386- const previous = self.lines.items[self.row];
402+ const previous = self.lines.items[self.row].items;
387403 self.col = wordStart(previous, cpLen(previous));
388404 }
389405
390406 fn wordRight(self: *Editor) void {
391- const line = self.lines.items[self.row];
407+ const line = self.lines.items[self.row].items;
392408 const end = wordEnd(line, self.col);
393409 if (end != self.col) {
394410 self.col = end;
@@ -396,28 +412,28 @@ pub const Editor = struct {
396412 }
397413 if (self.row == self.lines.items.len - 1) return;
398414 self.row += 1;
399- self.col = wordEnd(self.lines.items[self.row], 0);
415+ self.col = wordEnd(self.lines.items[self.row].items, 0);
400416 }
401417
402418 fn wordBack(self: *Editor) void {
403- const line = self.lines.items[self.row];
404- const start = wordStart(line, self.col);
405- const before = sub(self.a, line, 0, start);
406- const after = sub(self.a, line, self.col, cpLen(line));
407- self.lines.items[self.row] = cat(self.a, &.{ before, after });
419+ const line = &self.lines.items[self.row];
420+ const start = wordStart(line.items, self.col);
421+ const from = byteOf(line.items, start);
422+ const to = byteOf(line.items, self.col);
423+ line.replaceRange(self.a, from, to - from, "") catch {};
408424 self.col = start;
409425 }
410426
411427 pub fn completeWord(self: *Editor, step: *const fn (word: []const u8) ?[]const u8) bool {
412- const line = self.lines.items[self.row];
413- const start = wordStart(line, self.col);
428+ const line = &self.lines.items[self.row];
429+ const start = wordStart(line.items, self.col);
414430 if (start == self.col) return false;
415- if (isSpace(cpAt(line, self.col - 1))) return false;
416- const word = sub(self.a, line, start, self.col);
431+ if (isSpace(cpAt(line.items, self.col - 1))) return false;
432+ const word = sub(self.s, line.items, start, self.col);
417433 const completed = step(word) orelse return false;
418- const before = sub(self.a, line, 0, start);
419- const after = sub(self.a, line, self.col, cpLen(line));
420- self.lines.items[self.row] = cat(self.a, &.{ before, completed, after });
434+ const from = byteOf(line.items, start);
435+ const to = byteOf(line.items, self.col);
436+ line.replaceRange(self.a, from, to - from, completed) catch {};
421437 self.col = start + cpLen(completed);
422438 return true;
423439 }