aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2025-07-18 10:42:24 -0700
committerAndrew Kelley <andrew@ziglang.org>2025-07-19 19:57:37 -0700
commitbd64bf0e47e75481569dcc3ba519e3d2e7f7b276 (patch)
treea5c3fc1aadc5c0860be69ec3d0423d701d2db8da /lib/std
parent83d1f88ac5f053f11b91d9c3d51f5c63355b8ca3 (diff)
downloadzig-bd64bf0e47e75481569dcc3ba519e3d2e7f7b276.tar.gz
zig-bd64bf0e47e75481569dcc3ba519e3d2e7f7b276.zip
std.mem: add byteSwapAllElements
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/mem.zig36
1 files changed, 20 insertions, 16 deletions
diff --git a/lib/std/mem.zig b/lib/std/mem.zig
index 33e68eedad..1a61076f32 100644
--- a/lib/std/mem.zig
+++ b/lib/std/mem.zig
@@ -2179,22 +2179,8 @@ pub fn byteSwapAllFields(comptime S: type, ptr: *S) void {
const BackingInt = std.meta.Int(.unsigned, @bitSizeOf(S));
ptr.* = @bitCast(@byteSwap(@as(BackingInt, @bitCast(ptr.*))));
},
- .array => {
- for (ptr) |*item| {
- switch (@typeInfo(@TypeOf(item.*))) {
- .@"struct", .@"union", .array => byteSwapAllFields(@TypeOf(item.*), item),
- .@"enum" => {
- item.* = @enumFromInt(@byteSwap(@intFromEnum(item.*)));
- },
- .bool => {},
- .float => |float_info| {
- item.* = @bitCast(@byteSwap(@as(std.meta.Int(.unsigned, float_info.bits), @bitCast(item.*))));
- },
- else => {
- item.* = @byteSwap(item.*);
- },
- }
- }
+ .array => |info| {
+ byteSwapAllElements(info.child, ptr);
},
else => {
ptr.* = @byteSwap(ptr.*);
@@ -2258,6 +2244,24 @@ test byteSwapAllFields {
}, k);
}
+pub fn byteSwapAllElements(comptime Elem: type, slice: []Elem) void {
+ for (slice) |*elem| {
+ switch (@typeInfo(@TypeOf(elem.*))) {
+ .@"struct", .@"union", .array => byteSwapAllFields(@TypeOf(elem.*), elem),
+ .@"enum" => {
+ elem.* = @enumFromInt(@byteSwap(@intFromEnum(elem.*)));
+ },
+ .bool => {},
+ .float => |float_info| {
+ elem.* = @bitCast(@byteSwap(@as(std.meta.Int(.unsigned, float_info.bits), @bitCast(elem.*))));
+ },
+ else => {
+ elem.* = @byteSwap(elem.*);
+ },
+ }
+ }
+}
+
/// Returns an iterator that iterates over the slices of `buffer` that are not
/// any of the items in `delimiters`.
///