aboutsummaryrefslogtreecommitdiff
path: root/lib/c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/c')
-rw-r--r--lib/c/common.zig15
-rw-r--r--lib/c/string.zig45
2 files changed, 60 insertions, 0 deletions
diff --git a/lib/c/common.zig b/lib/c/common.zig
new file mode 100644
index 0000000000..e9536f63b4
--- /dev/null
+++ b/lib/c/common.zig
@@ -0,0 +1,15 @@
+const builtin = @import("builtin");
+const std = @import("std");
+
+pub const linkage: std.builtin.GlobalLinkage = if (builtin.is_test)
+ .internal
+else
+ .strong;
+
+/// Determines the symbol's visibility to other objects.
+/// For WebAssembly this allows the symbol to be resolved to other modules, but will not
+/// export it to the host runtime.
+pub const visibility: std.builtin.SymbolVisibility = if (builtin.cpu.arch.isWasm() and linkage != .internal)
+ .hidden
+else
+ .default;
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);
+}