authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-07-12 17:39:25+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-12 21:55:49+00:00
log5b570bceb531656c320684fbe42a87feff710a3b
tree97748b857cab92c502886099b60bc01e8ea34607
parent7adbc1140373548f2985c8054b540cc5648f6857

document a few functions in std.mem


1 files changed, 12 insertions(+), 3 deletions(-)

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