@@ -1,6 +1,7 @@
11 const std = @import("std");
22 const render = @import("render.zig");
33 const input = @import("input.zig");
4+const utf8 = @import("../text.zig");
45
56 const TAB = 4;
67
@@ -8,25 +9,12 @@ fn isSpace(cp: u21) bool {
89 return cp == ' ' or cp == '\t' or cp == '\n' or cp == '\r' or cp == 0x0b or cp == 0x0c or cp == 0xa0;
910 }
1011
11-fn cpLen(line: []const u8) usize {
12- var i: usize = 0;
13- var count: usize = 0;
14- while (i < line.len) {
15- const n = std.unicode.utf8ByteSequenceLength(line[i]) catch 1;
16- i += @min(@as(usize, n), line.len - i);
17- count += 1;
18- }
19- return count;
20-}
21-
2212 fn cpAt(line: []const u8, col: usize) u21 {
2313 var i: usize = 0;
2414 var count: usize = 0;
25- while (i < line.len) {
26- const n = std.unicode.utf8ByteSequenceLength(line[i]) catch 1;
27- const len = @min(@as(usize, n), line.len - i);
28- if (count == col) return std.unicode.utf8Decode(line[i .. i + len]) catch line[i];
29- i += len;
15+ while (utf8.decodeAt(line, i)) |d| {
16+ if (count == col) return d.cp;
17+ i += d.len;
3018 count += 1;
3119 }
3220 return 0;
@@ -35,10 +23,8 @@ fn cpAt(line: []const u8, col: usize) u21 {
3523 fn byteOf(line: []const u8, col: usize) usize {
3624 var i: usize = 0;
3725 var count: usize = 0;
38- while (i < line.len and count < col) {
39- const n = std.unicode.utf8ByteSequenceLength(line[i]) catch 1;
40- i += @min(@as(usize, n), line.len - i);
41- count += 1;
26+ while (count < col) : (count += 1) {
27+ i += (utf8.decodeAt(line, i) orelse break).len;
4228 }
4329 return i;
4430 }
@@ -57,7 +43,7 @@ fn wordStart(line: []const u8, col: usize) usize {
5743 }
5844
5945 fn wordEnd(line: []const u8, col: usize) usize {
60- const n = cpLen(line);
46+ const n = utf8.cpLen(line);
6147 var i = col;
6248 while (i < n and isSpace(cpAt(line, i))) i += 1;
6349 while (i < n and !isSpace(cpAt(line, i))) i += 1;
@@ -121,7 +107,7 @@ pub const Editor = struct {
121107 }
122108 if (self.lines.items.len == 0) self.lines.append(self.a, .empty) catch {};
123109 self.row = self.lines.items.len - 1;
124- self.col = cpLen(self.lines.items[self.row].items);
110+ self.col = utf8.cpLen(self.lines.items[self.row].items);
125111 }
126112
127113 pub fn handle(self: *Editor, key: input.Key) Action {
@@ -176,7 +162,7 @@ pub const Editor = struct {
176162 return .changed;
177163 },
178164 .end => {
179- self.col = cpLen(self.lines.items[self.row].items);
165+ self.col = utf8.cpLen(self.lines.items[self.row].items);
180166 return .changed;
181167 },
182168 .doc_start => {
@@ -186,7 +172,7 @@ pub const Editor = struct {
186172 },
187173 .doc_end => {
188174 self.row = self.lines.items.len - 1;
189- self.col = cpLen(self.lines.items[self.row].items);
175+ self.col = utf8.cpLen(self.lines.items[self.row].items);
190176 return .changed;
191177 },
192178 else => return .none,
@@ -201,15 +187,9 @@ pub const Editor = struct {
201187 while (render.nextCluster(line, i)) |cluster| {
202188 const tab = cluster.text.len == 1 and cluster.text[0] == '\t';
203189 const advance = if (tab) TAB - (col % TAB) else cluster.width;
204- var cps: usize = 0;
205- var j: usize = 0;
206- while (j < cluster.text.len) {
207- const n = std.unicode.utf8ByteSequenceLength(cluster.text[j]) catch 1;
208- j += @min(@as(usize, n), cluster.text.len - j);
209- cps += 1;
210- }
211- var m: usize = 0;
190+ const cps = utf8.cpLen(cluster.text);
212191 col += advance;
192+ var m: usize = 0;
213193 while (m < cps) : (m += 1) out.append(self.s, col) catch {};
214194 i += cluster.text.len;
215195 }
@@ -278,7 +258,7 @@ pub const Editor = struct {
278258 const at = byteOf(line.items, self.col);
279259 if (std.mem.indexOfScalar(u8, draft, '\n') == null) {
280260 line.insertSlice(self.a, at, draft) catch {};
281- self.col += cpLen(draft);
261+ self.col += utf8.cpLen(draft);
282262 return;
283263 }
284264 const tail = self.a.dupe(u8, line.items[at..]) catch "";
@@ -292,7 +272,7 @@ pub const Editor = struct {
292272 fresh.append(self.a, l) catch {};
293273 }
294274 const lasti = fresh.items.len - 1;
295- const anchor = cpLen(fresh.items[lasti].items);
275+ const anchor = utf8.cpLen(fresh.items[lasti].items);
296276 fresh.items[lasti].appendSlice(self.a, tail) catch {};
297277 var at_row = self.row + 1;
298278 for (fresh.items) |l| {
@@ -303,19 +283,23 @@ pub const Editor = struct {
303283 self.col = anchor;
304284 }
305285
286+ /// Replace the code points in `[from, to)` of `line` with `text`.
287+ fn replaceCols(self: *Editor, line: *Line, from: usize, to: usize, text: []const u8) void {
288+ const start = byteOf(line.items, from);
289+ const end = byteOf(line.items, to);
290+ line.replaceRange(self.a, start, end - start, text) catch {};
291+ }
292+
306293 fn backspace(self: *Editor) void {
307294 if (self.col > 0) {
308- const line = &self.lines.items[self.row];
309- const from = byteOf(line.items, self.col - 1);
310- const to = byteOf(line.items, self.col);
311- line.replaceRange(self.a, from, to - from, "") catch {};
295+ self.replaceCols(&self.lines.items[self.row], self.col - 1, self.col, "");
312296 self.col -= 1;
313297 return;
314298 }
315299 if (self.row > 0) {
316300 const prev = &self.lines.items[self.row - 1];
317301 const cur = self.lines.items[self.row];
318- const previous = cpLen(prev.items);
302+ const previous = utf8.cpLen(prev.items);
319303 prev.appendSlice(self.a, cur.items) catch {};
320304 _ = self.lines.orderedRemove(self.row);
321305 self.row -= 1;
@@ -325,11 +309,9 @@ pub const Editor = struct {
325309
326310 fn deleteForward(self: *Editor) void {
327311 const line = &self.lines.items[self.row];
328- const n = cpLen(line.items);
312+ const n = utf8.cpLen(line.items);
329313 if (self.col < n) {
330- const from = byteOf(line.items, self.col);
331- const to = byteOf(line.items, self.col + 1);
332- line.replaceRange(self.a, from, to - from, "") catch {};
314+ self.replaceCols(line, self.col, self.col + 1, "");
333315 return;
334316 }
335317 if (self.row < self.lines.items.len - 1) {
@@ -345,12 +327,12 @@ pub const Editor = struct {
345327 self.col -= 1;
346328 } else if (self.row > 0) {
347329 self.row -= 1;
348- self.col = cpLen(self.lines.items[self.row].items);
330+ self.col = utf8.cpLen(self.lines.items[self.row].items);
349331 }
350332 }
351333
352334 fn right(self: *Editor) void {
353- if (self.col < cpLen(self.lines.items[self.row].items)) {
335+ if (self.col < utf8.cpLen(self.lines.items[self.row].items)) {
354336 self.col += 1;
355337 } else if (self.row < self.lines.items.len - 1) {
356338 self.row += 1;
@@ -398,7 +380,7 @@ pub const Editor = struct {
398380 if (self.row == 0) return;
399381 self.row -= 1;
400382 const previous = self.lines.items[self.row].items;
401- self.col = wordStart(previous, cpLen(previous));
383+ self.col = wordStart(previous, utf8.cpLen(previous));
402384 }
403385
404386 fn wordRight(self: *Editor) void {
@@ -414,11 +396,8 @@ pub const Editor = struct {
414396 }
415397
416398 fn wordBack(self: *Editor) void {
417- const line = &self.lines.items[self.row];
418- const start = wordStart(line.items, self.col);
419- const from = byteOf(line.items, start);
420- const to = byteOf(line.items, self.col);
421- line.replaceRange(self.a, from, to - from, "") catch {};
399+ const start = wordStart(self.lines.items[self.row].items, self.col);
400+ self.replaceCols(&self.lines.items[self.row], start, self.col, "");
422401 self.col = start;
423402 }
424403
@@ -429,10 +408,8 @@ pub const Editor = struct {
429408 if (isSpace(cpAt(line.items, self.col - 1))) return false;
430409 const word = sub(self.s, line.items, start, self.col);
431410 const completed = step(word) orelse return false;
432- const from = byteOf(line.items, start);
433- const to = byteOf(line.items, self.col);
434- line.replaceRange(self.a, from, to - from, completed) catch {};
435- self.col = start + cpLen(completed);
411+ self.replaceCols(line, start, self.col, completed);
412+ self.col = start + utf8.cpLen(completed);
436413 return true;
437414 }
438415 };