authorgravatar for 65512175+abm-77@users.noreply.github.comBryson Miller <65512175+abm-77@users.noreply.github.com> 2025-05-15 01:58:33-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-05-15 10:58:33+02:00
log08d534e8d8482b0c59b5311111a6ae70593d783d
treed8f4bca079096d07fe3864d3f86800fb9a36a406
parentbc377183ce687ab186ed1544d01f9680eee55f45
signaturebadge-check Signed by PGP key B5690EEEBB952194

Introduce common `strcasecmp` and `strncasecmp` implementations (#23840)


5 files changed, 64 insertions(+), 37 deletions(-)

lib/c/string.zig+64
...@@ -6,6 +6,12 @@ comptime {...@@ -6,6 +6,12 @@ comptime {
6 @export(&strcmp, .{ .name = "strcmp", .linkage = common.linkage, .visibility = common.visibility });6 @export(&strcmp, .{ .name = "strcmp", .linkage = common.linkage, .visibility = common.visibility });
7 @export(&strlen, .{ .name = "strlen", .linkage = common.linkage, .visibility = common.visibility });7 @export(&strlen, .{ .name = "strlen", .linkage = common.linkage, .visibility = common.visibility });
8 @export(&strncmp, .{ .name = "strncmp", .linkage = common.linkage, .visibility = common.visibility });8 @export(&strncmp, .{ .name = "strncmp", .linkage = common.linkage, .visibility = common.visibility });
9 @export(&strcasecmp, .{ .name = "strcasecmp", .linkage = common.linkage, .visibility = common.visibility });
10 @export(&strncasecmp, .{ .name = "strncasecmp", .linkage = common.linkage, .visibility = common.visibility });
11 @export(&__strcasecmp_l, .{ .name = "__strcasecmp_l", .linkage = common.linkage, .visibility = common.visibility });
12 @export(&__strncasecmp_l, .{ .name = "__strncasecmp_l", .linkage = common.linkage, .visibility = common.visibility });
13 @export(&__strcasecmp_l, .{ .name = "strcasecmp_l", .linkage = .weak, .visibility = common.visibility });
14 @export(&__strncasecmp_l, .{ .name = "strncasecmp_l", .linkage = .weak, .visibility = common.visibility });
9}15}
1016
11fn strcmp(s1: [*:0]const c_char, s2: [*:0]const c_char) callconv(.c) c_int {17fn 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)...@@ -33,6 +39,64 @@ fn strncmp(s1: [*:0]const c_char, s2: [*:0]const c_char, n: usize) callconv(.c)
33 return @as(c_int, l[0]) - @as(c_int, r[0]);39 return @as(c_int, l[0]) - @as(c_int, r[0]);
34}40}
3541
42fn strcasecmp(s1: [*:0]const c_char, s2: [*:0]const c_char) callconv(.c) c_int {
43 const toLower = std.ascii.toLower;
44 var l: [*:0]const u8 = @ptrCast(s1);
45 var r: [*:0]const u8 = @ptrCast(s2);
46
47 while (l[0] != 0 and r[0] != 0 and (l[0] == r[0] or toLower(l[0]) == toLower(r[0]))) {
48 l += 1;
49 r += 1;
50 }
51
52 return @as(c_int, toLower(l[0])) - @as(c_int, toLower(r[0]));
53}
54
55fn __strcasecmp_l(s1: [*:0]const c_char, s2: [*:0]const c_char, locale: *anyopaque) callconv(.c) c_int {
56 _ = locale;
57 return strcasecmp(s1, s2);
58}
59
60fn strncasecmp(s1: [*:0]const c_char, s2: [*:0]const c_char, n: usize) callconv(.c) c_int {
61 const toLower = std.ascii.toLower;
62 var l: [*:0]const u8 = @ptrCast(s1);
63 var r: [*:0]const u8 = @ptrCast(s2);
64 var i = n - 1;
65
66 while (l[0] != 0 and r[0] != 0 and i != 0 and (l[0] == r[0] or toLower(l[0]) == toLower(r[0]))) {
67 l += 1;
68 r += 1;
69 i -= 1;
70 }
71
72 return @as(c_int, toLower(l[0])) - @as(c_int, toLower(r[0]));
73}
74
75fn __strncasecmp_l(s1: [*:0]const c_char, s2: [*:0]const c_char, n: usize, locale: *anyopaque) callconv(.c) c_int {
76 _ = locale;
77 return strncasecmp(s1, s2, n);
78}
79
80test strcasecmp {
81 try std.testing.expect(strcasecmp(@ptrCast("a"), @ptrCast("b")) < 0);
82 try std.testing.expect(strcasecmp(@ptrCast("b"), @ptrCast("a")) > 0);
83 try std.testing.expect(strcasecmp(@ptrCast("A"), @ptrCast("b")) < 0);
84 try std.testing.expect(strcasecmp(@ptrCast("b"), @ptrCast("A")) > 0);
85 try std.testing.expect(strcasecmp(@ptrCast("A"), @ptrCast("A")) == 0);
86 try std.testing.expect(strcasecmp(@ptrCast("B"), @ptrCast("b")) == 0);
87 try std.testing.expect(strcasecmp(@ptrCast("bb"), @ptrCast("AA")) > 0);
88}
89
90test strncasecmp {
91 try std.testing.expect(strncasecmp(@ptrCast("a"), @ptrCast("b"), 1) < 0);
92 try std.testing.expect(strncasecmp(@ptrCast("b"), @ptrCast("a"), 1) > 0);
93 try std.testing.expect(strncasecmp(@ptrCast("A"), @ptrCast("b"), 1) < 0);
94 try std.testing.expect(strncasecmp(@ptrCast("b"), @ptrCast("A"), 1) > 0);
95 try std.testing.expect(strncasecmp(@ptrCast("A"), @ptrCast("A"), 1) == 0);
96 try std.testing.expect(strncasecmp(@ptrCast("B"), @ptrCast("b"), 1) == 0);
97 try std.testing.expect(strncasecmp(@ptrCast("bb"), @ptrCast("AA"), 2) > 0);
98}
99
36test strncmp {100test strncmp {
37 try std.testing.expect(strncmp(@ptrCast("a"), @ptrCast("b"), 1) < 0);101 try std.testing.expect(strncmp(@ptrCast("a"), @ptrCast("b"), 1) < 0);
38 try std.testing.expect(strncmp(@ptrCast("a"), @ptrCast("c"), 1) < 0);102 try std.testing.expect(strncmp(@ptrCast("a"), @ptrCast("c"), 1) < 0);
lib/libc/musl/src/string/strcasecmp.c deleted-16
...@@ -1,16 +0,0 @@
1#include <strings.h>
2#include <ctype.h>
3
4int strcasecmp(const char *_l, const char *_r)
5{
6 const unsigned char *l=(void *)_l, *r=(void *)_r;
7 for (; *l && *r && (*l == *r || tolower(*l) == tolower(*r)); l++, r++);
8 return tolower(*l) - tolower(*r);
9}
10
11int __strcasecmp_l(const char *l, const char *r, locale_t loc)
12{
13 return strcasecmp(l, r);
14}
15
16weak_alias(__strcasecmp_l, strcasecmp_l);
lib/libc/musl/src/string/strncasecmp.c deleted-17
...@@ -1,17 +0,0 @@
1#include <strings.h>
2#include <ctype.h>
3
4int strncasecmp(const char *_l, const char *_r, size_t n)
5{
6 const unsigned char *l=(void *)_l, *r=(void *)_r;
7 if (!n--) return 0;
8 for (; *l && *r && n && (*l == *r || tolower(*l) == tolower(*r)); l++, r++, n--);
9 return tolower(*l) - tolower(*r);
10}
11
12int __strncasecmp_l(const char *l, const char *r, size_t n, locale_t loc)
13{
14 return strncasecmp(l, r, n);
15}
16
17weak_alias(__strncasecmp_l, strncasecmp_l);
src/libs/musl.zig-2
...@@ -1853,7 +1853,6 @@ const src_files = [_][]const u8{...@@ -1853,7 +1853,6 @@ const src_files = [_][]const u8{
1853 "musl/src/string/rindex.c",1853 "musl/src/string/rindex.c",
1854 "musl/src/string/stpcpy.c",1854 "musl/src/string/stpcpy.c",
1855 "musl/src/string/stpncpy.c",1855 "musl/src/string/stpncpy.c",
1856 "musl/src/string/strcasecmp.c",
1857 "musl/src/string/strcasestr.c",1856 "musl/src/string/strcasestr.c",
1858 "musl/src/string/strcat.c",1857 "musl/src/string/strcat.c",
1859 "musl/src/string/strchr.c",1858 "musl/src/string/strchr.c",
...@@ -1864,7 +1863,6 @@ const src_files = [_][]const u8{...@@ -1864,7 +1863,6 @@ const src_files = [_][]const u8{
1864 "musl/src/string/strerror_r.c",1863 "musl/src/string/strerror_r.c",
1865 "musl/src/string/strlcat.c",1864 "musl/src/string/strlcat.c",
1866 "musl/src/string/strlcpy.c",1865 "musl/src/string/strlcpy.c",
1867 "musl/src/string/strncasecmp.c",
1868 "musl/src/string/strncat.c",1866 "musl/src/string/strncat.c",
1869 "musl/src/string/strncpy.c",1867 "musl/src/string/strncpy.c",
1870 "musl/src/string/strndup.c",1868 "musl/src/string/strndup.c",
src/libs/wasi_libc.zig-2
...@@ -1055,7 +1055,6 @@ const libc_top_half_src_files = [_][]const u8{...@@ -1055,7 +1055,6 @@ const libc_top_half_src_files = [_][]const u8{
1055 "musl/src/string/rindex.c",1055 "musl/src/string/rindex.c",
1056 "musl/src/string/stpcpy.c",1056 "musl/src/string/stpcpy.c",
1057 "musl/src/string/stpncpy.c",1057 "musl/src/string/stpncpy.c",
1058 "musl/src/string/strcasecmp.c",
1059 "musl/src/string/strcasestr.c",1058 "musl/src/string/strcasestr.c",
1060 "musl/src/string/strcat.c",1059 "musl/src/string/strcat.c",
1061 "musl/src/string/strchr.c",1060 "musl/src/string/strchr.c",
...@@ -1066,7 +1065,6 @@ const libc_top_half_src_files = [_][]const u8{...@@ -1066,7 +1065,6 @@ const libc_top_half_src_files = [_][]const u8{
1066 "musl/src/string/strerror_r.c",1065 "musl/src/string/strerror_r.c",
1067 "musl/src/string/strlcat.c",1066 "musl/src/string/strlcat.c",
1068 "musl/src/string/strlcpy.c",1067 "musl/src/string/strlcpy.c",
1069 "musl/src/string/strncasecmp.c",
1070 "musl/src/string/strncat.c",1068 "musl/src/string/strncat.c",
1071 "musl/src/string/strncpy.c",1069 "musl/src/string/strncpy.c",
1072 "musl/src/string/strndup.c",1070 "musl/src/string/strndup.c",