fix: simplify and update Buffer.get_byte_pos for zig-0.15
This commit is contained in:
parent
2867fc9a8e
commit
8f1375745d
1 changed files with 24 additions and 43 deletions
|
@ -1039,53 +1039,34 @@ const Node = union(enum) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_byte_pos(self: *const Node, pos_: Cursor, metrics_: Metrics, eol_mode: EolMode) !usize {
|
pub fn get_byte_pos(self: *const Node, pos_: Cursor, metrics: Metrics, eol_mode: EolMode) !usize {
|
||||||
const Ctx = struct {
|
const ctx_ = struct {
|
||||||
|
pos: usize = 0,
|
||||||
line: usize = 0,
|
line: usize = 0,
|
||||||
abs_col: usize = 0,
|
col: usize = 0,
|
||||||
pos: Cursor,
|
target_line: usize,
|
||||||
byte_pos: usize = 0,
|
target_col: usize,
|
||||||
metrics: Metrics,
|
eol_mode: EolMode,
|
||||||
const Ctx = @This();
|
fn walker(ctx_: *anyopaque, egc: []const u8, wcwidth: usize, _: Metrics) Walker {
|
||||||
const Writer = std.io.Writer(*Ctx, error{Stop}, write);
|
const ctx = @as(*@This(), @ptrCast(@alignCast(ctx_)));
|
||||||
fn write(ctx: *Ctx, bytes: []const u8) error{Stop}!usize {
|
if (ctx.line == ctx.target_line and ctx.col == ctx.target_col) return Walker.stop;
|
||||||
if (ctx.line >= ctx.pos.row) {
|
if (egc[0] == '\n') {
|
||||||
return ctx.get_col_bytes(bytes, bytes.len);
|
ctx.pos += switch (ctx.eol_mode) {
|
||||||
} else for (bytes, 1..) |char, i| {
|
.lf => 1,
|
||||||
ctx.byte_pos += 1;
|
.crlf => 2,
|
||||||
if (char == '\n') {
|
};
|
||||||
ctx.line += 1;
|
ctx.line += 1;
|
||||||
if (ctx.line >= ctx.pos.row)
|
ctx.col = 0;
|
||||||
return ctx.get_col_bytes(bytes[i..], bytes.len);
|
} else {
|
||||||
}
|
ctx.pos += egc.len;
|
||||||
|
ctx.col += wcwidth;
|
||||||
}
|
}
|
||||||
return bytes.len;
|
return Walker.keep_walking;
|
||||||
}
|
|
||||||
fn get_col_bytes(ctx: *Ctx, bytes: []const u8, result: usize) error{Stop}!usize {
|
|
||||||
var buf: []const u8 = bytes;
|
|
||||||
while (buf.len > 0) {
|
|
||||||
if (ctx.abs_col >= ctx.pos.col) return error.Stop;
|
|
||||||
if (buf[0] == '\n') return error.Stop;
|
|
||||||
var cols: c_int = undefined;
|
|
||||||
const egc_bytes = ctx.metrics.egc_length(ctx.metrics, buf, &cols, ctx.abs_col);
|
|
||||||
ctx.abs_col += @intCast(cols);
|
|
||||||
ctx.byte_pos += egc_bytes;
|
|
||||||
buf = buf[egc_bytes..];
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
fn writer(ctx: *Ctx) Writer {
|
|
||||||
return .{ .context = ctx };
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
var ctx: Ctx = .{
|
var ctx: ctx_ = .{ .target_line = pos_.row, .target_col = pos_.col, .eol_mode = eol_mode };
|
||||||
.pos = pos_,
|
self.walk_egc_forward(0, ctx_.walker, &ctx, metrics) catch {};
|
||||||
.metrics = metrics_,
|
return ctx.pos;
|
||||||
};
|
|
||||||
self.store(ctx.writer(), eol_mode) catch |e| switch (e) {
|
|
||||||
error.Stop => return ctx.byte_pos,
|
|
||||||
};
|
|
||||||
return error.NotFound;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn debug_render_chunks(self: *const Node, allocator: std.mem.Allocator, line: usize, metrics_: Metrics) ![]const u8 {
|
pub fn debug_render_chunks(self: *const Node, allocator: std.mem.Allocator, line: usize, metrics_: Metrics) ![]const u8 {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue