mirror of
https://github.com/ziglang/zig.git
synced 2025-12-12 09:13:11 +00:00
std.mem: add byteSwapAllElements
This commit is contained in:
parent
3a3fd47a8a
commit
d12504eefa
@ -2179,22 +2179,8 @@ pub fn byteSwapAllFields(comptime S: type, ptr: *S) void {
|
|||||||
const BackingInt = std.meta.Int(.unsigned, @bitSizeOf(S));
|
const BackingInt = std.meta.Int(.unsigned, @bitSizeOf(S));
|
||||||
ptr.* = @bitCast(@byteSwap(@as(BackingInt, @bitCast(ptr.*))));
|
ptr.* = @bitCast(@byteSwap(@as(BackingInt, @bitCast(ptr.*))));
|
||||||
},
|
},
|
||||||
.array => {
|
.array => |info| {
|
||||||
for (ptr) |*item| {
|
byteSwapAllElements(info.child, ptr);
|
||||||
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.*);
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
else => {
|
else => {
|
||||||
ptr.* = @byteSwap(ptr.*);
|
ptr.* = @byteSwap(ptr.*);
|
||||||
@ -2258,6 +2244,24 @@ test byteSwapAllFields {
|
|||||||
}, k);
|
}, 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
|
/// Returns an iterator that iterates over the slices of `buffer` that are not
|
||||||
/// any of the items in `delimiters`.
|
/// any of the items in `delimiters`.
|
||||||
///
|
///
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user