fix: switch_case does nothing if there are non-cased chars in range

This commit is contained in:
CJ van den Berg 2026-02-15 21:51:38 +01:00
parent 80429f6055
commit 7471848ef2
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 24 additions and 7 deletions

View file

@ -30,8 +30,8 @@
.hash = "fuzzig-0.1.1-Ji0xivxIAQBD0g8O_NV_0foqoPf3elsg9Sc3pNfdVH4D", .hash = "fuzzig-0.1.1-Ji0xivxIAQBD0g8O_NV_0foqoPf3elsg9Sc3pNfdVH4D",
}, },
.vaxis = .{ .vaxis = .{
.url = "git+https://github.com/neurocyte/libvaxis?ref=main#95034c7114601178467b42e69588cdd4c1d39eb1", .url = "git+https://github.com/neurocyte/libvaxis?ref=main#4fc5a651a85574c3dfdddebc1d9038fd6bb4e067",
.hash = "vaxis-0.5.1-BWNV_PJJCQArtOy_n76jGWoqoBpnM5FKElA_i1IaYYcF", .hash = "vaxis-0.5.1-BWNV_EBKCQDqoQZvUE22SBMKYTlZtIIEdLx2X8CfRDHf",
}, },
.zeit = .{ .zeit = .{
.url = "git+https://github.com/rockorager/zeit?ref=zig-0.15#ed2ca60db118414bda2b12df2039e33bad3b0b88", .url = "git+https://github.com/rockorager/zeit?ref=zig-0.15#ed2ca60db118414bda2b12df2039e33bad3b0b88",

View file

@ -150,12 +150,14 @@ fn utf8_transform(comptime field: uucode.FieldEnum, allocator: std.mem.Allocator
return result.toOwnedSlice(); return result.toOwnedSlice();
} }
fn utf8_predicate(comptime field: uucode.FieldEnum, text: []const u8) bool { fn utf8_predicate_all(comptime field: uucode.FieldEnum, text: []const u8) bool {
const view: Utf8View = .initUnchecked(text); const view: Utf8View = .initUnchecked(text);
var it = view.iterator(); var it = view.iterator();
while (it.nextCodepoint()) |cp| { while (it.nextCodepoint()) |cp| {
const result = switch (field) { const result = switch (field) {
.is_lowercase => uucode.get(field, cp), .is_lowercase => uucode.get(field, cp),
.changes_when_casefolded => uucode.get(field, cp),
.changes_when_lowercased => uucode.get(field, cp),
else => @compileError(@tagName(field) ++ " is not a unicode predicate"), else => @compileError(@tagName(field) ++ " is not a unicode predicate"),
}; };
if (!result) return false; if (!result) return false;
@ -163,6 +165,21 @@ fn utf8_predicate(comptime field: uucode.FieldEnum, text: []const u8) bool {
return true; return true;
} }
fn utf8_predicate_any(comptime field: uucode.FieldEnum, text: []const u8) bool {
const view: Utf8View = .initUnchecked(text);
var it = view.iterator();
while (it.nextCodepoint()) |cp| {
const result = switch (field) {
.is_lowercase => uucode.get(field, cp),
.changes_when_casefolded => uucode.get(field, cp),
.changes_when_lowercased => uucode.get(field, cp),
else => @compileError(@tagName(field) ++ " is not a unicode predicate"),
};
if (result) return true;
}
return false;
}
pub fn to_upper(allocator: std.mem.Allocator, text: []const u8) TransformError![]u8 { pub fn to_upper(allocator: std.mem.Allocator, text: []const u8) TransformError![]u8 {
return utf8_transform(.simple_uppercase_mapping, allocator, text); return utf8_transform(.simple_uppercase_mapping, allocator, text);
} }
@ -184,14 +201,14 @@ pub fn case_folded_write_partial(writer: *std.Io.Writer, text: []const u8) Trans
} }
pub fn switch_case(allocator: std.mem.Allocator, text: []const u8) TransformError![]u8 { pub fn switch_case(allocator: std.mem.Allocator, text: []const u8) TransformError![]u8 {
return if (utf8_predicate(.is_lowercase, text)) return if (utf8_predicate_any(.changes_when_lowercased, text))
to_upper(allocator, text) to_lower(allocator, text)
else else
to_lower(allocator, text); to_upper(allocator, text);
} }
pub fn is_lowercase(text: []const u8) bool { pub fn is_lowercase(text: []const u8) bool {
return utf8_predicate(.is_lowercase, text); return utf8_predicate_all(.is_lowercase, text);
} }
const std = @import("std"); const std = @import("std");