aboutsummaryrefslogtreecommitdiff
path: root/lib/c/string.zig
diff options
context:
space:
mode:
authorAlex Rønne Petersen <alex@alexrp.com>2025-04-10 19:17:29 +0200
committerAlex Rønne Petersen <alex@alexrp.com>2025-04-11 17:12:31 +0200
commit1f896c1bf89aa0e3d2a0dce1f4cf6ba6ce5ae9ed (patch)
tree66d5586f636c37b65a1b99de3c0665bab97ec3f0 /lib/c/string.zig
parentee0ff134e9f82bf87751a5174c27b191c04e16c0 (diff)
downloadzig-1f896c1bf89aa0e3d2a0dce1f4cf6ba6ce5ae9ed.tar.gz
zig-1f896c1bf89aa0e3d2a0dce1f4cf6ba6ce5ae9ed.zip
Introduce libzigc for libc function implementations in Zig.
This lays the groundwork for #2879. This library will be built and linked when a static libc is going to be linked into the compilation. Currently, that means musl, wasi-libc, and MinGW-w64. As a demonstration, this commit removes the musl C code for a few string functions and implements them in libzigc. This means that those libzigc functions are now load-bearing for musl and wasi-libc. Note that if a function has an implementation in compiler-rt already, libzigc should not implement it. Instead, as we recently did for memcpy/memmove, we should delete the libc copy and rely on the compiler-rt implementation. I repurposed the existing "universal libc" code to do this. That code hadn't seen development beyond basic string functions in years, and was only usable-ish on freestanding. I think that if we want to seriously pursue the idea of Zig providing a freestanding libc, we should do so only after defining clear goals (and non-goals) for it. See also #22240 for a similar case.
Diffstat (limited to 'lib/c/string.zig')
-rw-r--r--lib/c/string.zig45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/c/string.zig b/lib/c/string.zig
new file mode 100644
index 0000000000..0633f7e654
--- /dev/null
+++ b/lib/c/string.zig
@@ -0,0 +1,45 @@
+const builtin = @import("builtin");
+const std = @import("std");
+const common = @import("common.zig");
+
+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 });
+}
+
+fn strcmp(s1: [*:0]const c_char, s2: [*:0]const c_char) callconv(.c) c_int {
+ // We need to perform unsigned comparisons.
+ return switch (std.mem.orderZ(u8, @ptrCast(s1), @ptrCast(s2))) {
+ .lt => -1,
+ .eq => 0,
+ .gt => 1,
+ };
+}
+
+fn strncmp(s1: [*:0]const c_char, s2: [*:0]const c_char, n: usize) callconv(.c) c_int {
+ if (n == 0) return 0;
+
+ 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]) {
+ l += 1;
+ r += 1;
+ i -= 1;
+ }
+
+ return @as(c_int, l[0]) - @as(c_int, r[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);
+ try std.testing.expect(strncmp(@ptrCast("b"), @ptrCast("a"), 1) > 0);
+ try std.testing.expect(strncmp(@ptrCast("\xff"), @ptrCast("\x02"), 1) > 0);
+}
+
+fn strlen(s: [*:0]const c_char) callconv(.c) usize {
+ return std.mem.len(s);
+}