fix: switch_case does nothing if there are non-cased chars in range
This commit is contained in:
parent
80429f6055
commit
7471848ef2
2 changed files with 24 additions and 7 deletions
|
|
@ -30,8 +30,8 @@
|
|||
.hash = "fuzzig-0.1.1-Ji0xivxIAQBD0g8O_NV_0foqoPf3elsg9Sc3pNfdVH4D",
|
||||
},
|
||||
.vaxis = .{
|
||||
.url = "git+https://github.com/neurocyte/libvaxis?ref=main#95034c7114601178467b42e69588cdd4c1d39eb1",
|
||||
.hash = "vaxis-0.5.1-BWNV_PJJCQArtOy_n76jGWoqoBpnM5FKElA_i1IaYYcF",
|
||||
.url = "git+https://github.com/neurocyte/libvaxis?ref=main#4fc5a651a85574c3dfdddebc1d9038fd6bb4e067",
|
||||
.hash = "vaxis-0.5.1-BWNV_EBKCQDqoQZvUE22SBMKYTlZtIIEdLx2X8CfRDHf",
|
||||
},
|
||||
.zeit = .{
|
||||
.url = "git+https://github.com/rockorager/zeit?ref=zig-0.15#ed2ca60db118414bda2b12df2039e33bad3b0b88",
|
||||
|
|
|
|||
|
|
@ -150,12 +150,14 @@ fn utf8_transform(comptime field: uucode.FieldEnum, allocator: std.mem.Allocator
|
|||
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);
|
||||
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 false;
|
||||
|
|
@ -163,6 +165,21 @@ fn utf8_predicate(comptime field: uucode.FieldEnum, text: []const u8) bool {
|
|||
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 {
|
||||
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 {
|
||||
return if (utf8_predicate(.is_lowercase, text))
|
||||
to_upper(allocator, text)
|
||||
return if (utf8_predicate_any(.changes_when_lowercased, text))
|
||||
to_lower(allocator, text)
|
||||
else
|
||||
to_lower(allocator, text);
|
||||
to_upper(allocator, text);
|
||||
}
|
||||
|
||||
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");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue