diff --git a/lib/c/string.zig b/lib/c/string.zig index 0633f7e654..e846c4d960 100644 --- a/lib/c/string.zig +++ b/lib/c/string.zig @@ -6,6 +6,12 @@ comptime { @export(&strcmp, .{ .name = "strcmp", .linkage = common.linkage, .visibility = common.visibility }); @export(&strlen, .{ .name = "strlen", .linkage = common.linkage, .visibility = common.visibility }); @export(&strncmp, .{ .name = "strncmp", .linkage = common.linkage, .visibility = common.visibility }); + @export(&strcasecmp, .{ .name = "strcasecmp", .linkage = common.linkage, .visibility = common.visibility }); + @export(&strncasecmp, .{ .name = "strncasecmp", .linkage = common.linkage, .visibility = common.visibility }); + @export(&__strcasecmp_l, .{ .name = "__strcasecmp_l", .linkage = common.linkage, .visibility = common.visibility }); + @export(&__strncasecmp_l, .{ .name = "__strncasecmp_l", .linkage = common.linkage, .visibility = common.visibility }); + @export(&__strcasecmp_l, .{ .name = "strcasecmp_l", .linkage = .weak, .visibility = common.visibility }); + @export(&__strncasecmp_l, .{ .name = "strncasecmp_l", .linkage = .weak, .visibility = common.visibility }); } fn strcmp(s1: [*:0]const c_char, s2: [*:0]const c_char) callconv(.c) c_int { @@ -33,6 +39,64 @@ fn strncmp(s1: [*:0]const c_char, s2: [*:0]const c_char, n: usize) callconv(.c) return @as(c_int, l[0]) - @as(c_int, r[0]); } +fn strcasecmp(s1: [*:0]const c_char, s2: [*:0]const c_char) callconv(.c) c_int { + const toLower = std.ascii.toLower; + var l: [*:0]const u8 = @ptrCast(s1); + var r: [*:0]const u8 = @ptrCast(s2); + + while (l[0] != 0 and r[0] != 0 and (l[0] == r[0] or toLower(l[0]) == toLower(r[0]))) { + l += 1; + r += 1; + } + + return @as(c_int, toLower(l[0])) - @as(c_int, toLower(r[0])); +} + +fn __strcasecmp_l(s1: [*:0]const c_char, s2: [*:0]const c_char, locale: *anyopaque) callconv(.c) c_int { + _ = locale; + return strcasecmp(s1, s2); +} + +fn strncasecmp(s1: [*:0]const c_char, s2: [*:0]const c_char, n: usize) callconv(.c) c_int { + const toLower = std.ascii.toLower; + var l: [*:0]const u8 = @ptrCast(s1); + var r: [*:0]const u8 = @ptrCast(s2); + var i = n - 1; + + while (l[0] != 0 and r[0] != 0 and i != 0 and (l[0] == r[0] or toLower(l[0]) == toLower(r[0]))) { + l += 1; + r += 1; + i -= 1; + } + + return @as(c_int, toLower(l[0])) - @as(c_int, toLower(r[0])); +} + +fn __strncasecmp_l(s1: [*:0]const c_char, s2: [*:0]const c_char, n: usize, locale: *anyopaque) callconv(.c) c_int { + _ = locale; + return strncasecmp(s1, s2, n); +} + +test strcasecmp { + try std.testing.expect(strcasecmp(@ptrCast("a"), @ptrCast("b")) < 0); + try std.testing.expect(strcasecmp(@ptrCast("b"), @ptrCast("a")) > 0); + try std.testing.expect(strcasecmp(@ptrCast("A"), @ptrCast("b")) < 0); + try std.testing.expect(strcasecmp(@ptrCast("b"), @ptrCast("A")) > 0); + try std.testing.expect(strcasecmp(@ptrCast("A"), @ptrCast("A")) == 0); + try std.testing.expect(strcasecmp(@ptrCast("B"), @ptrCast("b")) == 0); + try std.testing.expect(strcasecmp(@ptrCast("bb"), @ptrCast("AA")) > 0); +} + +test strncasecmp { + try std.testing.expect(strncasecmp(@ptrCast("a"), @ptrCast("b"), 1) < 0); + try std.testing.expect(strncasecmp(@ptrCast("b"), @ptrCast("a"), 1) > 0); + try std.testing.expect(strncasecmp(@ptrCast("A"), @ptrCast("b"), 1) < 0); + try std.testing.expect(strncasecmp(@ptrCast("b"), @ptrCast("A"), 1) > 0); + try std.testing.expect(strncasecmp(@ptrCast("A"), @ptrCast("A"), 1) == 0); + try std.testing.expect(strncasecmp(@ptrCast("B"), @ptrCast("b"), 1) == 0); + try std.testing.expect(strncasecmp(@ptrCast("bb"), @ptrCast("AA"), 2) > 0); +} + test strncmp { try std.testing.expect(strncmp(@ptrCast("a"), @ptrCast("b"), 1) < 0); try std.testing.expect(strncmp(@ptrCast("a"), @ptrCast("c"), 1) < 0); diff --git a/lib/libc/musl/src/string/strcasecmp.c b/lib/libc/musl/src/string/strcasecmp.c deleted file mode 100644 index 002c6aa15b..0000000000 --- a/lib/libc/musl/src/string/strcasecmp.c +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include - -int strcasecmp(const char *_l, const char *_r) -{ - const unsigned char *l=(void *)_l, *r=(void *)_r; - for (; *l && *r && (*l == *r || tolower(*l) == tolower(*r)); l++, r++); - return tolower(*l) - tolower(*r); -} - -int __strcasecmp_l(const char *l, const char *r, locale_t loc) -{ - return strcasecmp(l, r); -} - -weak_alias(__strcasecmp_l, strcasecmp_l); diff --git a/lib/libc/musl/src/string/strncasecmp.c b/lib/libc/musl/src/string/strncasecmp.c deleted file mode 100644 index e0ef93c20d..0000000000 --- a/lib/libc/musl/src/string/strncasecmp.c +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include - -int strncasecmp(const char *_l, const char *_r, size_t n) -{ - const unsigned char *l=(void *)_l, *r=(void *)_r; - if (!n--) return 0; - for (; *l && *r && n && (*l == *r || tolower(*l) == tolower(*r)); l++, r++, n--); - return tolower(*l) - tolower(*r); -} - -int __strncasecmp_l(const char *l, const char *r, size_t n, locale_t loc) -{ - return strncasecmp(l, r, n); -} - -weak_alias(__strncasecmp_l, strncasecmp_l); diff --git a/src/libs/musl.zig b/src/libs/musl.zig index bc89e20c21..6e48f3f49f 100644 --- a/src/libs/musl.zig +++ b/src/libs/musl.zig @@ -1853,7 +1853,6 @@ const src_files = [_][]const u8{ "musl/src/string/rindex.c", "musl/src/string/stpcpy.c", "musl/src/string/stpncpy.c", - "musl/src/string/strcasecmp.c", "musl/src/string/strcasestr.c", "musl/src/string/strcat.c", "musl/src/string/strchr.c", @@ -1864,7 +1863,6 @@ const src_files = [_][]const u8{ "musl/src/string/strerror_r.c", "musl/src/string/strlcat.c", "musl/src/string/strlcpy.c", - "musl/src/string/strncasecmp.c", "musl/src/string/strncat.c", "musl/src/string/strncpy.c", "musl/src/string/strndup.c", diff --git a/src/libs/wasi_libc.zig b/src/libs/wasi_libc.zig index 2657c2e1f8..2d345071ef 100644 --- a/src/libs/wasi_libc.zig +++ b/src/libs/wasi_libc.zig @@ -1055,7 +1055,6 @@ const libc_top_half_src_files = [_][]const u8{ "musl/src/string/rindex.c", "musl/src/string/stpcpy.c", "musl/src/string/stpncpy.c", - "musl/src/string/strcasecmp.c", "musl/src/string/strcasestr.c", "musl/src/string/strcat.c", "musl/src/string/strchr.c", @@ -1066,7 +1065,6 @@ const libc_top_half_src_files = [_][]const u8{ "musl/src/string/strerror_r.c", "musl/src/string/strlcat.c", "musl/src/string/strlcpy.c", - "musl/src/string/strncasecmp.c", "musl/src/string/strncat.c", "musl/src/string/strncpy.c", "musl/src/string/strndup.c",