libc: reimplement swab in Zig (#31130)

This PR replaces the bundled musl swab() implementation with zig's one.

Contributes towards #30978.

It looks like there are not test cases for swab() in test-libc.

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31130
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: Ivel <ivel.santos@proton.me>
Co-committed-by: Ivel <ivel.santos@proton.me>
This commit is contained in:
Ivel 2026-02-05 20:21:41 +01:00 committed by Andrew Kelley
parent c77e7146f5
commit fa3228ae42
4 changed files with 42 additions and 15 deletions

View file

@ -43,6 +43,9 @@ comptime {
@export(&execveLinux, .{ .name = "execve", .linkage = common.linkage, .visibility = common.visibility });
}
if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
@export(&swab, .{ .name = "swab", .linkage = common.linkage, .visibility = common.visibility });
}
}
fn _exit(exit_code: c_int) callconv(.c) noreturn {
@ -181,3 +184,42 @@ fn unlinkatLinux(fd: c_int, path: [*:0]const c_char, flags: c_int) callconv(.c)
fn execveLinux(path: [*:0]const c_char, argv: [*:null]const ?[*:0]c_char, envp: [*:null]const ?[*:0]c_char) callconv(.c) c_int {
return common.errno(linux.execve(@ptrCast(path), @ptrCast(argv), @ptrCast(envp)));
}
fn swab(noalias src_ptr: *const anyopaque, noalias dest_ptr: *anyopaque, n: isize) callconv(.c) void {
var src: [*]const u8 = @ptrCast(src_ptr);
var dest: [*]u8 = @ptrCast(dest_ptr);
var i = n;
while (i > 1) : (i -= 2) {
dest[0] = src[1];
dest[1] = src[0];
dest += 2;
src += 2;
}
}
test swab {
var a: [4]u8 = undefined;
@memset(a[0..], '\x00');
swab("abcd", &a, 4);
try std.testing.expectEqualSlices(u8, "badc", &a);
// Partial copy
@memset(a[0..], '\x00');
swab("abcd", &a, 2);
try std.testing.expectEqualSlices(u8, "ba\x00\x00", &a);
// n < 1
@memset(a[0..], '\x00');
swab("abcd", &a, 0);
try std.testing.expectEqualSlices(u8, "\x00" ** 4, &a);
swab("abcd", &a, -1);
try std.testing.expectEqualSlices(u8, "\x00" ** 4, &a);
// Odd n
@memset(a[0..], '\x00');
swab("abcd", &a, 1);
try std.testing.expectEqualSlices(u8, "\x00" ** 4, &a);
swab("abcd", &a, 3);
try std.testing.expectEqualSlices(u8, "ba\x00\x00", &a);
}

View file

@ -1,13 +0,0 @@
#include <unistd.h>
void swab(const void *restrict _src, void *restrict _dest, ssize_t n)
{
const char *src = _src;
char *dest = _dest;
for (; n>1; n-=2) {
dest[0] = src[1];
dest[1] = src[0];
dest += 2;
src += 2;
}
}

View file

@ -1579,7 +1579,6 @@ const src_files = [_][]const u8{
"musl/src/string/strndup.c",
"musl/src/string/strsignal.c",
"musl/src/string/strverscmp.c",
"musl/src/string/swab.c",
"musl/src/string/wcscasecmp.c",
"musl/src/string/wcscasecmp_l.c",
"musl/src/string/wcsdup.c",

View file

@ -957,7 +957,6 @@ const libc_top_half_src_files = [_][]const u8{
"musl/src/string/strerror_r.c",
"musl/src/string/strndup.c",
"musl/src/string/strverscmp.c",
"musl/src/string/swab.c",
"musl/src/string/wcscasecmp.c",
"musl/src/string/wcscasecmp_l.c",
"musl/src/string/wcsdup.c",