Files
zig/lib/c/strings.zig
Alex Rønne Petersen 7ea8f842bc libzigc: move all unit tests from lib/c/ to test/c/
Before:

* test-zigc: run libzigc unit tests (part of test-modules)
* test-libc: run libc-test cases

Now:

* test-libc: run libc API unit tests (part of test-modules)
* test-libc-nsz: run libc-test cases

libc API unit tests (previously referred to as libzigc unit tests) now run for
all supported targets, even those we don't provide libzigc for. The idea is that
this will help us catch bad assumptions in the unit tests, as well as bugs in
other libcs.

I considered this setup:

* test-c: run libc API unit tests (part of test-modules)
* test-libc-nsz: run libc-test cases
* test-libc: both of the above

However, I do not like it because it gives a false sense of security; the full
module and C ABI test suites are still liable to catch libzigc bugs that test-c
and test-libc-nsz might not. So contributors should just run the test steps
outlined in https://codeberg.org/ziglang/zig/issues/30978.

Co-authored-by: rpkak <rpkak@noreply.codeberg.org>
2026-04-17 12:10:37 +02:00

84 lines
2.6 KiB
Zig

const builtin = @import("builtin");
const std = @import("std");
const symbol = @import("../c.zig").symbol;
comptime {
if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
// bcmp is implemented in compiler_rt
symbol(&bcopy, "bcopy");
symbol(&bzero, "bzero");
symbol(&index, "index");
symbol(&rindex, "rindex");
symbol(&ffs, "ffs");
symbol(&ffsl, "ffsl");
symbol(&ffsll, "ffsll");
symbol(&strcasecmp, "strcasecmp");
symbol(&strncasecmp, "strncasecmp");
symbol(&__strcasecmp_l, "__strcasecmp_l");
symbol(&__strncasecmp_l, "__strncasecmp_l");
symbol(&__strcasecmp_l, "strcasecmp_l");
symbol(&__strncasecmp_l, "strncasecmp_l");
}
}
fn bcopy(src: *const anyopaque, dst: *anyopaque, len: usize) callconv(.c) void {
const src_bytes: [*]const u8 = @ptrCast(src);
const dst_bytes: [*]u8 = @ptrCast(dst);
@memmove(dst_bytes[0..len], src_bytes[0..len]);
}
fn bzero(s: *anyopaque, n: usize) callconv(.c) void {
const s_cast: [*]u8 = @ptrCast(s);
@memset(s_cast[0..n], 0);
}
fn index(str: [*:0]const c_char, value: c_int) callconv(.c) ?[*:0]c_char {
return @constCast(str[std.mem.findScalar(u8, std.mem.span(@as([*:0]const u8, @ptrCast(str))), @truncate(@as(c_uint, @bitCast(value)))) orelse return null ..]);
}
fn rindex(str: [*:0]const c_char, value: c_int) callconv(.c) ?[*:0]c_char {
return @constCast(str[std.mem.findScalarLast(u8, std.mem.span(@as([*:0]const u8, @ptrCast(str))), @truncate(@as(c_uint, @bitCast(value)))) orelse return null ..]);
}
fn firstBitSet(comptime T: type, value: T) T {
return @bitSizeOf(T) - @clz(value);
}
fn ffs(i: c_int) callconv(.c) c_int {
return firstBitSet(c_int, i);
}
fn ffsl(i: c_long) callconv(.c) c_long {
return firstBitSet(c_long, i);
}
fn ffsll(i: c_longlong) callconv(.c) c_longlong {
return firstBitSet(c_longlong, i);
}
fn strcasecmp(a: [*:0]const c_char, b: [*:0]const c_char) callconv(.c) c_int {
return strncasecmp(a, b, std.math.maxInt(usize));
}
fn __strcasecmp_l(a: [*:0]const c_char, b: [*:0]const c_char, locale: *anyopaque) callconv(.c) c_int {
_ = locale;
return strcasecmp(a, b);
}
fn strncasecmp(a: [*:0]const c_char, b: [*:0]const c_char, max: usize) callconv(.c) c_int {
return switch (std.ascii.boundedOrderIgnoreCaseZ(@ptrCast(a), @ptrCast(b), max)) {
.eq => 0,
.gt => 1,
.lt => -1,
};
}
fn __strncasecmp_l(a: [*:0]const c_char, b: [*:0]const c_char, n: usize, locale: *anyopaque) callconv(.c) c_int {
_ = locale;
return strncasecmp(a, b, n);
}