| ... | ... | @@ -8,6 +8,7 @@ const meta = std.meta; |
| 8 | 8 | const trait = meta.trait; |
| 9 | 9 | const testing = std.testing; |
| 10 | 10 | |
| 11 | // https://github.com/ziglang/zig/issues/2564 |
| 11 | 12 | pub const page_size = switch (builtin.arch) { |
| 12 | 13 | .wasm32, .wasm64 => 64 * 1024, |
| 13 | 14 | else => 4 * 1024, |
| ... | ... | @@ -518,6 +519,7 @@ pub fn copyBackwards(comptime T: type, dest: []T, source: []const T) void { |
| 518 | 519 | } |
| 519 | 520 | } |
| 520 | 521 | |
| 522 | /// Sets all elements of `dest` to `value`. |
| 521 | 523 | pub fn set(comptime T: type, dest: []T, value: T) void { |
| 522 | 524 | for (dest) |*d| |
| 523 | 525 | d.* = value; |
| ... | ... | @@ -675,6 +677,8 @@ test "mem.zeroes" { |
| 675 | 677 | } |
| 676 | 678 | } |
| 677 | 679 | |
| 680 | /// Sets a slice to zeroes. |
| 681 | /// Prevents the store from being optimized out. |
| 678 | 682 | pub fn secureZero(comptime T: type, s: []T) void { |
| 679 | 683 | // NOTE: We do not use a volatile slice cast here since LLVM cannot |
| 680 | 684 | // see that it can be replaced by a memset. |
| ... | ... | @@ -767,6 +771,7 @@ test "zeroInit" { |
| 767 | 771 | }); |
| 768 | 772 | } |
| 769 | 773 | |
| 774 | /// Compares two slices of numbers lexicographically. O(n). |
| 770 | 775 | pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order { |
| 771 | 776 | const n = math.min(lhs.len, rhs.len); |
| 772 | 777 | var i: usize = 0; |
| ... | ... | @@ -1904,6 +1909,8 @@ fn testWriteIntImpl() void { |
| 1904 | 1909 | })); |
| 1905 | 1910 | } |
| 1906 | 1911 | |
| 1912 | /// Returns the smallest number in a slice. O(n). |
| 1913 | /// `slice` must not be empty. |
| 1907 | 1914 | pub fn min(comptime T: type, slice: []const T) T { |
| 1908 | 1915 | var best = slice[0]; |
| 1909 | 1916 | for (slice[1..]) |item| { |
| ... | ... | @@ -1916,6 +1923,8 @@ test "mem.min" { |
| 1916 | 1923 | testing.expect(min(u8, "abcdefg") == 'a'); |
| 1917 | 1924 | } |
| 1918 | 1925 | |
| 1926 | /// Returns the largest number in a slice. O(n). |
| 1927 | /// `slice` must not be empty. |
| 1919 | 1928 | pub fn max(comptime T: type, slice: []const T) T { |
| 1920 | 1929 | var best = slice[0]; |
| 1921 | 1930 | for (slice[1..]) |item| { |
| ... | ... | @@ -2071,7 +2080,7 @@ test "asBytes" { |
| 2071 | 2080 | testing.expect(eql(u8, asBytes(&zero), "")); |
| 2072 | 2081 | } |
| 2073 | 2082 | |
| 2074 | | ///Given any value, returns a copy of its bytes in an array. |
| 2083 | /// Given any value, returns a copy of its bytes in an array. |
| 2075 | 2084 | pub fn toBytes(value: anytype) [@sizeOf(@TypeOf(value))]u8 { |
| 2076 | 2085 | return asBytes(&value).*; |
| 2077 | 2086 | } |
| ... | ... | @@ -2105,7 +2114,7 @@ fn BytesAsValueReturnType(comptime T: type, comptime B: type) type { |
| 2105 | 2114 | return if (comptime trait.isConstPtr(B)) *align(alignment) const T else *align(alignment) T; |
| 2106 | 2115 | } |
| 2107 | 2116 | |
| 2108 | | ///Given a pointer to an array of bytes, returns a pointer to a value of the specified type |
| 2117 | /// Given a pointer to an array of bytes, returns a pointer to a value of the specified type |
| 2109 | 2118 | /// backed by those bytes, preserving constness. |
| 2110 | 2119 | pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T, @TypeOf(bytes)) { |
| 2111 | 2120 | return @ptrCast(BytesAsValueReturnType(T, @TypeOf(bytes)), bytes); |
| ... | ... | @@ -2148,7 +2157,7 @@ test "bytesAsValue" { |
| 2148 | 2157 | testing.expect(meta.eql(inst, inst2.*)); |
| 2149 | 2158 | } |
| 2150 | 2159 | |
| 2151 | | ///Given a pointer to an array of bytes, returns a value of the specified type backed by a |
| 2160 | /// Given a pointer to an array of bytes, returns a value of the specified type backed by a |
| 2152 | 2161 | /// copy of those bytes. |
| 2153 | 2162 | pub fn bytesToValue(comptime T: type, bytes: anytype) T { |
| 2154 | 2163 | return bytesAsValue(T, bytes).*; |