authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-18 10:42:24-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-19 19:57:37-07:00
logbd64bf0e47e75481569dcc3ba519e3d2e7f7b276
treea5c3fc1aadc5c0860be69ec3d0423d701d2db8da
parent83d1f88ac5f053f11b91d9c3d51f5c63355b8ca3

std.mem: add byteSwapAllElements


1 files changed, 20 insertions(+), 16 deletions(-)

lib/std/mem.zig+20-16
...@@ -2179,22 +2179,8 @@ pub fn byteSwapAllFields(comptime S: type, ptr: *S) void {...@@ -2179,22 +2179,8 @@ pub fn byteSwapAllFields(comptime S: type, ptr: *S) void {
2179 const BackingInt = std.meta.Int(.unsigned, @bitSizeOf(S));2179 const BackingInt = std.meta.Int(.unsigned, @bitSizeOf(S));
2180 ptr.* = @bitCast(@byteSwap(@as(BackingInt, @bitCast(ptr.*))));2180 ptr.* = @bitCast(@byteSwap(@as(BackingInt, @bitCast(ptr.*))));
2181 },2181 },
2182 .array => {2182 .array => |info| {
2183 for (ptr) |*item| {2183 byteSwapAllElements(info.child, ptr);
2184 switch (@typeInfo(@TypeOf(item.*))) {
2185 .@"struct", .@"union", .array => byteSwapAllFields(@TypeOf(item.*), item),
2186 .@"enum" => {
2187 item.* = @enumFromInt(@byteSwap(@intFromEnum(item.*)));
2188 },
2189 .bool => {},
2190 .float => |float_info| {
2191 item.* = @bitCast(@byteSwap(@as(std.meta.Int(.unsigned, float_info.bits), @bitCast(item.*))));
2192 },
2193 else => {
2194 item.* = @byteSwap(item.*);
2195 },
2196 }
2197 }
2198 },2184 },
2199 else => {2185 else => {
2200 ptr.* = @byteSwap(ptr.*);2186 ptr.* = @byteSwap(ptr.*);
...@@ -2258,6 +2244,24 @@ test byteSwapAllFields {...@@ -2258,6 +2244,24 @@ test byteSwapAllFields {
2258 }, k);2244 }, k);
2259}2245}
22602246
2247pub fn byteSwapAllElements(comptime Elem: type, slice: []Elem) void {
2248 for (slice) |*elem| {
2249 switch (@typeInfo(@TypeOf(elem.*))) {
2250 .@"struct", .@"union", .array => byteSwapAllFields(@TypeOf(elem.*), elem),
2251 .@"enum" => {
2252 elem.* = @enumFromInt(@byteSwap(@intFromEnum(elem.*)));
2253 },
2254 .bool => {},
2255 .float => |float_info| {
2256 elem.* = @bitCast(@byteSwap(@as(std.meta.Int(.unsigned, float_info.bits), @bitCast(elem.*))));
2257 },
2258 else => {
2259 elem.* = @byteSwap(elem.*);
2260 },
2261 }
2262 }
2263}
2264
2261/// Returns an iterator that iterates over the slices of `buffer` that are not2265/// Returns an iterator that iterates over the slices of `buffer` that are not
2262/// any of the items in `delimiters`.2266/// any of the items in `delimiters`.
2263///2267///