diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 23:11:43 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 01:16:28 -0300 |
| commit | 71df23d63da76c6aedf91a2458d3057ed6da9a35 (patch) | |
| tree | c135e61e9bb510c6c71e6de1f53b3e4451d9a58b /src/File.zig | |
| parent | 090e9acdb744fdb8d6c7efcee41da74fcf7c542c (diff) | |
| download | pardes-71df23d63da76c6aedf91a2458d3057ed6da9a35.tar.gz pardes-71df23d63da76c6aedf91a2458d3057ed6da9a35.zip | |
File: the four anonymous usize comparators handed to std.sort.upperBound are one ascending fn
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/File.zig')
| -rw-r--r-- | src/File.zig | 29 |
1 files changed, 9 insertions, 20 deletions
diff --git a/src/File.zig b/src/File.zig index 1199e2fb..ad76909d 100644 --- a/src/File.zig +++ b/src/File.zig @@ -611,6 +611,11 @@ pub fn lineIndex(gpa: std.mem.Allocator, f: *State) ![]const usize { return starts; } +/// `std.sort`'s order for offsets: a key against an item. +fn ascending(key: usize, item: usize) std.math.Order { + return std.math.order(key, item); +} + /// The line index of `new` from `old`'s: an edit replaces one span, so /// line starts before it stand, starts after it shift, and only the span /// is scanned. Rebuilding scanned the whole file on every keystroke. @@ -623,17 +628,9 @@ fn editedLineStarts(gpa: std.mem.Allocator, starts: []const usize, old: []const const tail = span.tail; // A start s follows the newline at s-1: kept while that newline is in // the common head, shifted while it is in the common tail. - const kept = std.sort.upperBound(usize, starts, head, struct { - fn order(key: usize, item: usize) std.math.Order { - return std.math.order(key, item); - } - }.order); + const kept = std.sort.upperBound(usize, starts, head, ascending); const old_tail_at = old.len - tail; - const moved = starts.len - std.sort.upperBound(usize, starts, old_tail_at, struct { - fn order(key: usize, item: usize) std.math.Order { - return std.math.order(key, item); - } - }.order); + const moved = starts.len - std.sort.upperBound(usize, starts, old_tail_at, ascending); const middle = new[head .. new.len - tail]; const out = try gpa.alloc(usize, kept + std.mem.count(u8, middle, "\n") + moved); @memcpy(out[0..kept], starts[0..kept]); @@ -773,11 +770,7 @@ pub fn textLineCount(t: *Text, text: []const u8) usize { pub fn textPosition(t: *Text, text: []const u8, offset: usize) modal.Cursor { const index = contentIndex(t, text) orelse return modal.positionAt(text, offset); const bounded = @min(offset, text.len); - const row = std.sort.upperBound(usize, index, bounded, struct { - fn cmp(key: usize, item: usize) std.math.Order { - return std.math.order(key, item); - } - }.cmp) - 1; + const row = std.sort.upperBound(usize, index, bounded, ascending) - 1; const start = index[row]; const end = if (row + 1 < index.len) index[row + 1] - 1 else text.len; return .{ .row = row, .col = modal.graphemeStart(text[start..end], @min(bounded - start, end - start)) }; @@ -1062,11 +1055,7 @@ fn positionIn(gpa: std.mem.Allocator, f: *State, at: usize) struct { row: usize, const c = modal.positionAt(f.content, at); return .{ .row = c.row, .col = c.col }; }; - const row = std.sort.upperBound(usize, index, at, struct { - fn order(key: usize, item: usize) std.math.Order { - return std.math.order(key, item); - } - }.order) -| 1; + const row = std.sort.upperBound(usize, index, at, ascending) -| 1; return .{ .row = row, .col = at - index[row] }; } |
