fix: make switch_case flip each character individually

This commit is contained in:
CJ van den Berg 2024-10-15 22:27:59 +02:00
parent 252ad3c269
commit 88cf5670aa
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -3933,18 +3933,34 @@ pub const Editor = struct {
saved.cursor = sel.end; saved.cursor = sel.end;
break :ret sel; break :ret sel;
}; };
var sfa = std.heap.stackFallback(4096, self.allocator); const cd = CaseData.init(self.allocator) catch return error.Stop;
const cut_text = copy_selection(root, sel.*, sfa.get(), self.metrics) catch return error.Stop; var result = std.ArrayList(u8).init(self.allocator);
defer allocator.free(cut_text); defer result.deinit();
const cd = CaseData.init(allocator) catch return error.Stop; const writer: struct {
defer cd.deinit(); allocator: Allocator,
const flipped = (if (cd.isLowerStr(cut_text)) cd: *const CaseData,
cd.toUpperStr(allocator, cut_text) result: *std.ArrayList(u8),
else
cd.toLowerStr(allocator, cut_text)) catch return error.Stop; const Error = (error{ Stop, OutOfMemory } || @typeInfo(@typeInfo(@TypeOf(CaseData.toUpperStr)).Fn.return_type.?).ErrorUnion.error_set);
defer allocator.free(flipped); pub fn write(writer: *@This(), bytes: []const u8) Error!void {
const flipped = if (writer.cd.isLowerStr(bytes))
try writer.cd.toUpperStr(writer.allocator, bytes)
else
try writer.cd.toLowerStr(writer.allocator, bytes);
defer writer.allocator.free(flipped);
return writer.result.appendSlice(flipped);
}
fn map_error(e: anyerror, _: ?*std.builtin.StackTrace) Error {
return @errorCast(e);
}
} = .{
.allocator = self.allocator,
.cd = &cd,
.result = &result,
};
self.write_range(root, sel.*, writer, @TypeOf(writer).map_error, null) catch return error.Stop;
root = try self.delete_selection(root, cursel, allocator); root = try self.delete_selection(root, cursel, allocator);
root = self.insert(root, cursel, flipped, allocator) catch return error.Stop; root = self.insert(root, cursel, writer.result.items, allocator) catch return error.Stop;
cursel.* = saved; cursel.* = saved;
return root; return root;
} }