refactor: move all zg LetterCasing usage to Buffer.unicode

This commit is contained in:
CJ van den Berg 2025-11-23 22:15:13 +01:00
parent 57c5066451
commit 2ff0521040
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 26 additions and 10 deletions

View file

@ -90,12 +90,32 @@ pub const LetterCasing = @import("LetterCasing");
var letter_casing: ?LetterCasing = null;
var letter_casing_arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
pub fn get_letter_casing() *LetterCasing {
fn get_letter_casing() *LetterCasing {
if (letter_casing) |*cd| return cd;
letter_casing = LetterCasing.init(letter_casing_arena.allocator()) catch @panic("LetterCasing.init");
return &letter_casing.?;
}
pub fn to_upper(allocator: std.mem.Allocator, text: []const u8) error{ OutOfMemory, Utf8CannotEncodeSurrogateHalf, CodepointTooLarge }![]u8 {
return get_letter_casing().toUpperStr(allocator, text);
}
pub fn to_lower(allocator: std.mem.Allocator, text: []const u8) error{ OutOfMemory, Utf8CannotEncodeSurrogateHalf, CodepointTooLarge }![]u8 {
return get_letter_casing().toLowerStr(allocator, text);
}
pub fn case_fold(allocator: std.mem.Allocator, text: []const u8) error{ OutOfMemory, Utf8CannotEncodeSurrogateHalf, CodepointTooLarge }![]u8 {
return get_letter_casing().toLowerStr(allocator, text);
}
pub fn switch_case(allocator: std.mem.Allocator, text: []const u8) error{ OutOfMemory, Utf8CannotEncodeSurrogateHalf, CodepointTooLarge }![]u8 {
const letter_casing_ = get_letter_casing();
return if (letter_casing_.isLowerStr(text))
letter_casing_.toUpperStr(allocator, text)
else
letter_casing_.toLowerStr(allocator, text);
}
const spinner = [_][]const u8{
"",
"",

View file

@ -6182,7 +6182,7 @@ pub const Editor = struct {
const sfa_allocator = sfa.get();
const cut_text = copy_selection(root, sel.*, sfa_allocator, self.metrics) catch return error.Stop;
defer sfa_allocator.free(cut_text);
const ucased = Buffer.unicode.get_letter_casing().toUpperStr(sfa_allocator, cut_text) catch return error.Stop;
const ucased = Buffer.unicode.to_upper(sfa_allocator, cut_text) catch return error.Stop;
defer sfa_allocator.free(ucased);
root = try self.delete_selection(root, cursel, allocator);
root = self.insert(root, cursel, ucased, allocator) catch return error.Stop;
@ -6211,7 +6211,7 @@ pub const Editor = struct {
const sfa_allocator = sfa.get();
const cut_text = copy_selection(root, sel.*, sfa_allocator, self.metrics) catch return error.Stop;
defer sfa_allocator.free(cut_text);
const ucased = Buffer.unicode.get_letter_casing().toLowerStr(sfa_allocator, cut_text) catch return error.Stop;
const ucased = Buffer.unicode.to_lower(sfa_allocator, cut_text) catch return error.Stop;
defer sfa_allocator.free(ucased);
root = try self.delete_selection(root, cursel, buffer_allocator);
root = self.insert(root, cursel, ucased, buffer_allocator) catch return error.Stop;
@ -6241,11 +6241,7 @@ pub const Editor = struct {
root.write_range(sel.*, &range.writer, null, self.metrics) catch return error.Stop;
const bytes = range.written();
const letter_casing = Buffer.unicode.get_letter_casing();
const flipped = if (letter_casing.isLowerStr(bytes))
letter_casing.toUpperStr(self.allocator, bytes) catch return error.Stop
else
letter_casing.toLowerStr(self.allocator, bytes) catch return error.Stop;
const flipped = Buffer.unicode.switch_case(self.allocator, bytes) catch return error.Stop;
defer self.allocator.free(flipped);
root = try self.delete_selection(root, cursel, allocator);

View file

@ -248,9 +248,9 @@ pub fn Create(options: type) type {
}
fn prefix_compare_icase(allocator: std.mem.Allocator, prefix: []const u8, str: []const u8) error{OutOfMemory}!bool {
const icase_prefix = Buffer.unicode.get_letter_casing().toLowerStr(allocator, prefix) catch try allocator.dupe(u8, prefix);
const icase_prefix = Buffer.unicode.case_fold(allocator, prefix) catch try allocator.dupe(u8, prefix);
defer allocator.free(icase_prefix);
const icase_str = Buffer.unicode.get_letter_casing().toLowerStr(allocator, str) catch try allocator.dupe(u8, str);
const icase_str = Buffer.unicode.case_fold(allocator, str) catch try allocator.dupe(u8, str);
defer allocator.free(icase_str);
if (icase_str.len < icase_prefix.len) return false;
return std.mem.eql(u8, icase_prefix, icase_str[0..icase_prefix.len]);