diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-04-29 00:19:55 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-29 00:19:55 -0700 |
| commit | d65b42e07caa00dfe2f2fbf221c593ce57882784 (patch) | |
| tree | 7926cbea1499e0affe930bf6d7455dc24adf014e /lib/std | |
| parent | fd6200eda6d4fe19c34a59430a88a9ce38d6d7a4 (diff) | |
| parent | fa200ca0cad2705bad40eb723dedf4e3bf11f2ff (diff) | |
| download | zig-d65b42e07caa00dfe2f2fbf221c593ce57882784.tar.gz zig-d65b42e07caa00dfe2f2fbf221c593ce57882784.zip | |
Merge pull request #15481 from ziglang/use-mem-intrinsics
actually use the new memory intrinsics
Diffstat (limited to 'lib/std')
115 files changed, 627 insertions, 588 deletions
diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 34806adf48..f23b3ba5aa 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -1693,10 +1693,10 @@ pub fn constructCMacro(allocator: Allocator, name: []const u8, value: ?[]const u u8, name.len + if (value) |value_slice| value_slice.len + 1 else 0, ) catch |err| if (err == error.OutOfMemory) @panic("Out of memory") else unreachable; - mem.copy(u8, macro, name); + @memcpy(macro[0..name.len], name); if (value) |value_slice| { macro[name.len] = '='; - mem.copy(u8, macro[name.len + 1 ..], value_slice); + @memcpy(macro[name.len + 1 ..][0..value_slice.len], value_slice); } return macro; } diff --git a/lib/std/Build/Cache.zig b/lib/std/Build/Cache.zig index cae779a306..9139311785 100644 --- a/lib/std/Build/Cache.zig +++ b/lib/std/Build/Cache.zig @@ -388,7 +388,7 @@ pub const Manifest = struct { self.hash.hasher = hasher_init; self.hash.hasher.update(&bin_digest); - mem.copy(u8, &manifest_file_path, &self.hex_digest); + @memcpy(manifest_file_path[0..self.hex_digest.len], &self.hex_digest); manifest_file_path[hex_digest_len..][0..ext.len].* = ext.*; if (self.files.items.len == 0) { diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index b71298ce6a..d5a135e24b 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -1139,7 +1139,7 @@ fn appendModuleArgs( // We'll use this buffer to store the name we decide on var buf = try b.allocator.alloc(u8, dep.name.len + 32); // First, try just the exposed dependency name - std.mem.copy(u8, buf, dep.name); + @memcpy(buf[0..dep.name.len], dep.name); var name = buf[0..dep.name.len]; var n: usize = 0; while (names.contains(name)) { diff --git a/lib/std/Build/RunStep.zig b/lib/std/Build/RunStep.zig index 435c7369d0..ce2dd0234a 100644 --- a/lib/std/Build/RunStep.zig +++ b/lib/std/Build/RunStep.zig @@ -822,9 +822,19 @@ fn runCommand( }, }, .zig_test => { + const prefix: []const u8 = p: { + if (result.stdio.test_metadata) |tm| { + if (tm.next_index <= tm.names.len) { + const name = tm.testName(tm.next_index - 1); + break :p b.fmt("while executing test '{s}', ", .{name}); + } + } + break :p ""; + }; const expected_term: std.process.Child.Term = .{ .Exited = 0 }; if (!termMatches(expected_term, result.term)) { - return step.fail("the following command {} (expected {}):\n{s}", .{ + return step.fail("{s}the following command {} (expected {}):\n{s}", .{ + prefix, fmtTerm(result.term), fmtTerm(expected_term), try Step.allocPrintCmd(arena, self.cwd, final_argv), @@ -832,8 +842,8 @@ fn runCommand( } if (!result.stdio.test_results.isSuccess()) { return step.fail( - "the following test command failed:\n{s}", - .{try Step.allocPrintCmd(arena, self.cwd, final_argv)}, + "{s}the following test command failed:\n{s}", + .{ prefix, try Step.allocPrintCmd(arena, self.cwd, final_argv) }, ); } }, @@ -922,6 +932,7 @@ const StdIoResult = struct { stdout_null: bool, stderr_null: bool, test_results: Step.TestResults, + test_metadata: ?TestMetadata, }; fn evalZigTest( @@ -1057,6 +1068,7 @@ fn evalZigTest( .skip_count = skip_count, .leak_count = leak_count, }, + .test_metadata = metadata, }; } @@ -1172,6 +1184,7 @@ fn evalGeneric(self: *RunStep, child: *std.process.Child) !StdIoResult { .stdout_null = stdout_null, .stderr_null = stderr_null, .test_results = .{}, + .test_metadata = null, }; } diff --git a/lib/std/Progress.zig b/lib/std/Progress.zig index dba7166398..e3c5fc20dd 100644 --- a/lib/std/Progress.zig +++ b/lib/std/Progress.zig @@ -374,7 +374,7 @@ fn bufWrite(self: *Progress, end: *usize, comptime format: []const u8, args: any self.columns_written += self.output_buffer.len - end.*; end.* = self.output_buffer.len; const suffix = "... "; - std.mem.copy(u8, self.output_buffer[self.output_buffer.len - suffix.len ..], suffix); + @memcpy(self.output_buffer[self.output_buffer.len - suffix.len ..], suffix); }, } } diff --git a/lib/std/Thread.zig b/lib/std/Thread.zig index f2a78100ad..119328b2a8 100644 --- a/lib/std/Thread.zig +++ b/lib/std/Thread.zig @@ -56,7 +56,7 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void { const name_with_terminator = blk: { var name_buf: [max_name_len:0]u8 = undefined; - std.mem.copy(u8, &name_buf, name); + @memcpy(name_buf[0..name.len], name); name_buf[name.len] = 0; break :blk name_buf[0..name.len :0]; }; diff --git a/lib/std/array_hash_map.zig b/lib/std/array_hash_map.zig index bf43f24bc0..55b9aac6e4 100644 --- a/lib/std/array_hash_map.zig +++ b/lib/std/array_hash_map.zig @@ -578,9 +578,9 @@ pub fn ArrayHashMapUnmanaged( self.entries.len = 0; if (self.index_header) |header| { switch (header.capacityIndexType()) { - .u8 => mem.set(Index(u8), header.indexes(u8), Index(u8).empty), - .u16 => mem.set(Index(u16), header.indexes(u16), Index(u16).empty), - .u32 => mem.set(Index(u32), header.indexes(u32), Index(u32).empty), + .u8 => @memset(header.indexes(u8), Index(u8).empty), + .u16 => @memset(header.indexes(u16), Index(u16).empty), + .u32 => @memset(header.indexes(u32), Index(u32).empty), } } } diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index 1791482bc4..cbca601b82 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -120,7 +120,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { } const new_memory = try allocator.alignedAlloc(T, alignment, self.items.len); - mem.copy(T, new_memory, self.items); + @memcpy(new_memory, self.items); @memset(self.items, undefined); self.clearAndFree(); return new_memory; @@ -170,7 +170,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { self.items.len += items.len; mem.copyBackwards(T, self.items[i + items.len .. self.items.len], self.items[i .. self.items.len - items.len]); - mem.copy(T, self.items[i .. i + items.len], items); + @memcpy(self.items[i..][0..items.len], items); } /// Replace range of elements `list[start..start+len]` with `new_items`. @@ -182,15 +182,15 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { const range = self.items[start..after_range]; if (range.len == new_items.len) - mem.copy(T, range, new_items) + @memcpy(range[0..new_items.len], new_items) else if (range.len < new_items.len) { const first = new_items[0..range.len]; const rest = new_items[range.len..]; - mem.copy(T, range, first); + @memcpy(range[0..first.len], first); try self.insertSlice(after_range, rest); } else { - mem.copy(T, range, new_items); + @memcpy(range[0..new_items.len], new_items); const after_subrange = start + new_items.len; for (self.items[after_range..], 0..) |item, i| { @@ -260,7 +260,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { const new_len = old_len + items.len; assert(new_len <= self.capacity); self.items.len = new_len; - mem.copy(T, self.items[old_len..], items); + @memcpy(self.items[old_len..][0..items.len], items); } /// Append an unaligned slice of items to the list. Allocates more @@ -306,18 +306,22 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { /// Append a value to the list `n` times. /// Allocates more memory as necessary. /// Invalidates pointers if additional memory is needed. - pub fn appendNTimes(self: *Self, value: T, n: usize) Allocator.Error!void { + /// The function is inline so that a comptime-known `value` parameter will + /// have a more optimal memset codegen in case it has a repeated byte pattern. + pub inline fn appendNTimes(self: *Self, value: T, n: usize) Allocator.Error!void { const old_len = self.items.len; try self.resize(self.items.len + n); - mem.set(T, self.items[old_len..self.items.len], value); + @memset(self.items[old_len..self.items.len], value); } /// Append a value to the list `n` times. /// Asserts the capacity is enough. **Does not** invalidate pointers. - pub fn appendNTimesAssumeCapacity(self: *Self, value: T, n: usize) void { + /// The function is inline so that a comptime-known `value` parameter will + /// have a more optimal memset codegen in case it has a repeated byte pattern. + pub inline fn appendNTimesAssumeCapacity(self: *Self, value: T, n: usize) void { const new_len = self.items.len + n; assert(new_len <= self.capacity); - mem.set(T, self.items.ptr[self.items.len..new_len], value); + @memset(self.items.ptr[self.items.len..new_len], value); self.items.len = new_len; } @@ -397,7 +401,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { self.capacity = new_capacity; } else { const new_memory = try self.allocator.alignedAlloc(T, alignment, new_capacity); - mem.copy(T, new_memory, self.items); + @memcpy(new_memory[0..self.items.len], self.items); self.allocator.free(old_memory); self.items.ptr = new_memory.ptr; self.capacity = new_memory.len; @@ -596,7 +600,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ } const new_memory = try allocator.alignedAlloc(T, alignment, self.items.len); - mem.copy(T, new_memory, self.items); + @memcpy(new_memory, self.items); @memset(self.items, undefined); self.clearAndFree(allocator); return new_memory; @@ -647,7 +651,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ self.items.len += items.len; mem.copyBackwards(T, self.items[i + items.len .. self.items.len], self.items[i .. self.items.len - items.len]); - mem.copy(T, self.items[i .. i + items.len], items); + @memcpy(self.items[i..][0..items.len], items); } /// Replace range of elements `list[start..start+len]` with `new_items` @@ -716,7 +720,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ const new_len = old_len + items.len; assert(new_len <= self.capacity); self.items.len = new_len; - mem.copy(T, self.items[old_len..], items); + @memcpy(self.items[old_len..][0..items.len], items); } /// Append the slice of items to the list. Allocates more @@ -766,19 +770,23 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ /// Append a value to the list `n` times. /// Allocates more memory as necessary. /// Invalidates pointers if additional memory is needed. - pub fn appendNTimes(self: *Self, allocator: Allocator, value: T, n: usize) Allocator.Error!void { + /// The function is inline so that a comptime-known `value` parameter will + /// have a more optimal memset codegen in case it has a repeated byte pattern. + pub inline fn appendNTimes(self: *Self, allocator: Allocator, value: T, n: usize) Allocator.Error!void { const old_len = self.items.len; try self.resize(allocator, self.items.len + n); - mem.set(T, self.items[old_len..self.items.len], value); + @memset(self.items[old_len..self.items.len], value); } /// Append a value to the list `n` times. /// **Does not** invalidate pointers. /// Asserts the capacity is enough. - pub fn appendNTimesAssumeCapacity(self: *Self, value: T, n: usize) void { + /// The function is inline so that a comptime-known `value` parameter will + /// have a more optimal memset codegen in case it has a repeated byte pattern. + pub inline fn appendNTimesAssumeCapacity(self: *Self, value: T, n: usize) void { const new_len = self.items.len + n; assert(new_len <= self.capacity); - mem.set(T, self.items.ptr[self.items.len..new_len], value); + @memset(self.items.ptr[self.items.len..new_len], value); self.items.len = new_len; } @@ -815,7 +823,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ }, }; - mem.copy(T, new_memory, self.items[0..new_len]); + @memcpy(new_memory, self.items[0..new_len]); allocator.free(old_memory); self.items = new_memory; self.capacity = new_memory.len; @@ -877,7 +885,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ self.capacity = new_capacity; } else { const new_memory = try allocator.alignedAlloc(T, alignment, new_capacity); - mem.copy(T, new_memory, self.items); + @memcpy(new_memory[0..self.items.len], self.items); allocator.free(old_memory); self.items.ptr = new_memory.ptr; self.capacity = new_memory.len; diff --git a/lib/std/base64.zig b/lib/std/base64.zig index d9bf7d4a07..758c7c8b58 100644 --- a/lib/std/base64.zig +++ b/lib/std/base64.zig @@ -309,11 +309,11 @@ test "base64 padding dest overflow" { const input = "foo"; var expect: [128]u8 = undefined; - std.mem.set(u8, &expect, 0); + @memset(&expect, 0); _ = url_safe.Encoder.encode(expect[0..url_safe.Encoder.calcSize(input.len)], input); var got: [128]u8 = undefined; - std.mem.set(u8, &got, 0); + @memset(&got, 0); _ = url_safe.Encoder.encode(&got, input); try std.testing.expectEqualSlices(u8, &expect, &got); diff --git a/lib/std/bit_set.zig b/lib/std/bit_set.zig index 292d099303..b7dfc8d529 100644 --- a/lib/std/bit_set.zig +++ b/lib/std/bit_set.zig @@ -738,7 +738,7 @@ pub const DynamicBitSetUnmanaged = struct { // fill in any new masks if (new_masks > old_masks) { const fill_value = std.math.boolMask(MaskInt, fill); - std.mem.set(MaskInt, self.masks[old_masks..new_masks], fill_value); + @memset(self.masks[old_masks..new_masks], fill_value); } } @@ -765,7 +765,7 @@ pub const DynamicBitSetUnmanaged = struct { const num_masks = numMasks(self.bit_length); var copy = Self{}; try copy.resize(new_allocator, self.bit_length, false); - std.mem.copy(MaskInt, copy.masks[0..num_masks], self.masks[0..num_masks]); + @memcpy(copy.masks[0..num_masks], self.masks[0..num_masks]); return copy; } diff --git a/lib/std/bounded_array.zig b/lib/std/bounded_array.zig index 8a4c2db64b..be32093fe7 100644 --- a/lib/std/bounded_array.zig +++ b/lib/std/bounded_array.zig @@ -73,7 +73,7 @@ pub fn BoundedArrayAligned( /// Copy the content of an existing slice. pub fn fromSlice(m: []const T) error{Overflow}!Self { var list = try init(m.len); - std.mem.copy(T, list.slice(), m); + @memcpy(list.slice(), m); return list; } @@ -165,7 +165,7 @@ pub fn BoundedArrayAligned( try self.ensureUnusedCapacity(items.len); self.len += items.len; mem.copyBackwards(T, self.slice()[i + items.len .. self.len], self.constSlice()[i .. self.len - items.len]); - mem.copy(T, self.slice()[i .. i + items.len], items); + @memcpy(self.slice()[i..][0..items.len], items); } /// Replace range of elements `slice[start..start+len]` with `new_items`. @@ -181,14 +181,14 @@ pub fn BoundedArrayAligned( var range = self.slice()[start..after_range]; if (range.len == new_items.len) { - mem.copy(T, range, new_items); + @memcpy(range[0..new_items.len], new_items); } else if (range.len < new_items.len) { const first = new_items[0..range.len]; const rest = new_items[range.len..]; - mem.copy(T, range, first); + @memcpy(range[0..first.len], first); try self.insertSlice(after_range, rest); } else { - mem.copy(T, range, new_items); + @memcpy(range[0..new_items.len], new_items); const after_subrange = start + new_items.len; for (self.constSlice()[after_range..], 0..) |item, i| { self.slice()[after_subrange..][i] = item; @@ -243,9 +243,9 @@ pub fn BoundedArrayAligned( /// Append the slice of items to the slice, asserting the capacity is already /// enough to store the new items. pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void { - const oldlen = self.len; + const old_len = self.len; self.len += items.len; - mem.copy(T, self.slice()[oldlen..], items); + @memcpy(self.slice()[old_len..][0..items.len], items); } /// Append a value to the slice `n` times. @@ -253,7 +253,7 @@ pub fn BoundedArrayAligned( pub fn appendNTimes(self: *Self, value: T, n: usize) error{Overflow}!void { const old_len = self.len; try self.resize(old_len + n); - mem.set(T, self.slice()[old_len..self.len], value); + @memset(self.slice()[old_len..self.len], value); } /// Append a value to the slice `n` times. @@ -262,7 +262,7 @@ pub fn BoundedArrayAligned( const old_len = self.len; self.len += n; assert(self.len <= buffer_capacity); - mem.set(T, self.slice()[old_len..self.len], value); + @memset(self.slice()[old_len..self.len], value); } pub const Writer = if (T != u8) @@ -329,7 +329,7 @@ test "BoundedArray" { try testing.expectEqual(a.popOrNull(), 0); try testing.expectEqual(a.popOrNull(), null); var unused = a.unusedCapacitySlice(); - mem.set(u8, unused[0..8], 2); + @memset(unused[0..8], 2); unused[8] = 3; unused[9] = 4; try testing.expectEqual(unused.len, a.capacity()); diff --git a/lib/std/buf_set.zig b/lib/std/buf_set.zig index 9d6d7ed1e6..90ee86e12d 100644 --- a/lib/std/buf_set.zig +++ b/lib/std/buf_set.zig @@ -97,7 +97,7 @@ pub const BufSet = struct { fn copy(self: *const BufSet, value: []const u8) ![]const u8 { const result = try self.hash_map.allocator.alloc(u8, value.len); - mem.copy(u8, result, value); + @memcpy(result, value); return result; } }; diff --git a/lib/std/child_process.zig b/lib/std/child_process.zig index bd67d344c7..f5ca72ed39 100644 --- a/lib/std/child_process.zig +++ b/lib/std/child_process.zig @@ -259,7 +259,7 @@ pub const ChildProcess = struct { fn fifoToOwnedArrayList(fifo: *std.io.PollFifo) std.ArrayList(u8) { if (fifo.head > 0) { - std.mem.copy(u8, fifo.buf[0..fifo.count], fifo.buf[fifo.head .. fifo.head + fifo.count]); + @memcpy(fifo.buf[0..fifo.count], fifo.buf[fifo.head..][0..fifo.count]); } const result = std.ArrayList(u8){ .items = fifo.buf[0..fifo.count], @@ -1436,9 +1436,9 @@ pub fn createNullDelimitedEnvMap(arena: mem.Allocator, env_map: *const EnvMap) ! var i: usize = 0; while (it.next()) |pair| : (i += 1) { const env_buf = try arena.allocSentinel(u8, pair.key_ptr.len + pair.value_ptr.len + 1, 0); - mem.copy(u8, env_buf, pair.key_ptr.*); + @memcpy(env_buf[0..pair.key_ptr.len], pair.key_ptr.*); env_buf[pair.key_ptr.len] = '='; - mem.copy(u8, env_buf[pair.key_ptr.len + 1 ..], pair.value_ptr.*); + @memcpy(env_buf[pair.key_ptr.len + 1 ..][0..pair.value_ptr.len], pair.value_ptr.*); envp_buf[i] = env_buf.ptr; } assert(i == envp_count); diff --git a/lib/std/compress/deflate.zig b/lib/std/compress/deflate.zig index 7b079a6039..2fe5969067 100644 --- a/lib/std/compress/deflate.zig +++ b/lib/std/compress/deflate.zig @@ -12,6 +12,20 @@ pub const Decompressor = inflate.Decompressor; pub const compressor = deflate.compressor; pub const decompressor = inflate.decompressor; +/// Copies elements from a source `src` slice into a destination `dst` slice. +/// The copy never returns an error but might not be complete if the destination is too small. +/// Returns the number of elements copied, which will be the minimum of `src.len` and `dst.len`. +/// TODO: remove this smelly function +pub fn copy(dst: []u8, src: []const u8) usize { + if (dst.len <= src.len) { + @memcpy(dst, src[0..dst.len]); + return dst.len; + } else { + @memcpy(dst[0..src.len], src); + return src.len; + } +} + test { _ = @import("deflate/token.zig"); _ = @import("deflate/bits_utils.zig"); diff --git a/lib/std/compress/deflate/compressor.zig b/lib/std/compress/deflate/compressor.zig index 6c21875941..e2cbafe520 100644 --- a/lib/std/compress/deflate/compressor.zig +++ b/lib/std/compress/deflate/compressor.zig @@ -10,7 +10,6 @@ const Allocator = std.mem.Allocator; const deflate_const = @import("deflate_const.zig"); const fast = @import("deflate_fast.zig"); const hm_bw = @import("huffman_bit_writer.zig"); -const mu = @import("mem_utils.zig"); const token = @import("token.zig"); pub const Compression = enum(i5) { @@ -296,7 +295,7 @@ pub fn Compressor(comptime WriterType: anytype) type { fn fillDeflate(self: *Self, b: []const u8) u32 { if (self.index >= 2 * window_size - (min_match_length + max_match_length)) { // shift the window by window_size - mem.copy(u8, self.window, self.window[window_size .. 2 * window_size]); + mem.copyForwards(u8, self.window, self.window[window_size .. 2 * window_size]); self.index -= window_size; self.window_end -= window_size; if (self.block_start >= window_size) { @@ -328,7 +327,7 @@ pub fn Compressor(comptime WriterType: anytype) type { } } } - var n = mu.copy(self.window[self.window_end..], b); + const n = std.compress.deflate.copy(self.window[self.window_end..], b); self.window_end += n; return @intCast(u32, n); } @@ -369,7 +368,7 @@ pub fn Compressor(comptime WriterType: anytype) type { b = b[b.len - window_size ..]; } // Add all to window. - mem.copy(u8, self.window, b); + @memcpy(self.window[0..b.len], b); var n = b.len; // Calculate 256 hashes at the time (more L1 cache hits) @@ -543,7 +542,7 @@ pub fn Compressor(comptime WriterType: anytype) type { self.hash_offset = 1; self.tokens = try self.allocator.alloc(token.Token, max_flate_block_tokens); self.tokens_count = 0; - mem.set(token.Token, self.tokens, 0); + @memset(self.tokens, 0); self.length = min_match_length - 1; self.offset = 0; self.byte_available = false; @@ -706,7 +705,7 @@ pub fn Compressor(comptime WriterType: anytype) type { } fn fillStore(self: *Self, b: []const u8) u32 { - var n = mu.copy(self.window[self.window_end..], b); + const n = std.compress.deflate.copy(self.window[self.window_end..], b); self.window_end += n; return @intCast(u32, n); } @@ -841,9 +840,9 @@ pub fn Compressor(comptime WriterType: anytype) type { s.hash_head = try allocator.alloc(u32, hash_size); s.hash_prev = try allocator.alloc(u32, window_size); s.hash_match = try allocator.alloc(u32, max_match_length - 1); - mem.set(u32, s.hash_head, 0); - mem.set(u32, s.hash_prev, 0); - mem.set(u32, s.hash_match, 0); + @memset(s.hash_head, 0); + @memset(s.hash_prev, 0); + @memset(s.hash_match, 0); switch (options.level) { .no_compression => { @@ -936,8 +935,8 @@ pub fn Compressor(comptime WriterType: anytype) type { .best_compression, => { self.chain_head = 0; - mem.set(u32, self.hash_head, 0); - mem.set(u32, self.hash_prev, 0); + @memset(self.hash_head, 0); + @memset(self.hash_prev, 0); self.hash_offset = 1; self.index = 0; self.window_end = 0; @@ -1091,8 +1090,8 @@ test "bulkHash4" { // double the test data var out = try testing.allocator.alloc(u8, x.out.len * 2); defer testing.allocator.free(out); - mem.copy(u8, out[0..x.out.len], x.out); - mem.copy(u8, out[x.out.len..], x.out); + @memcpy(out[0..x.out.len], x.out); + @memcpy(out[x.out.len..], x.out); var j: usize = 4; while (j < out.len) : (j += 1) { diff --git a/lib/std/compress/deflate/decompressor.zig b/lib/std/compress/deflate/decompressor.zig index e5cfbb0f6b..6c232c598e 100644 --- a/lib/std/compress/deflate/decompressor.zig +++ b/lib/std/compress/deflate/decompressor.zig @@ -9,7 +9,6 @@ const ArrayList = std.ArrayList; const bu = @import("bits_utils.zig"); const ddec = @import("dict_decoder.zig"); const deflate_const = @import("deflate_const.zig"); -const mu = @import("mem_utils.zig"); const max_match_offset = deflate_const.max_match_offset; const end_block_marker = deflate_const.end_block_marker; @@ -159,7 +158,7 @@ const HuffmanDecoder = struct { if (sanity) { // initialize to a known invalid chunk code (0) to see if we overwrite // this value later on - mem.set(u16, self.links[off], 0); + @memset(self.links[off], 0); } try self.sub_chunks.append(off); } @@ -451,7 +450,7 @@ pub fn Decompressor(comptime ReaderType: type) type { pub fn read(self: *Self, output: []u8) Error!usize { while (true) { if (self.to_read.len > 0) { - var n = mu.copy(output, self.to_read); + const n = std.compress.deflate.copy(output, self.to_read); self.to_read = self.to_read[n..]; if (self.to_read.len == 0 and self.err != null) diff --git a/lib/std/compress/deflate/deflate_fast.zig b/lib/std/compress/deflate/deflate_fast.zig index 2009af2611..c86d181cb5 100644 --- a/lib/std/compress/deflate/deflate_fast.zig +++ b/lib/std/compress/deflate/deflate_fast.zig @@ -237,7 +237,7 @@ pub const DeflateFast = struct { } self.cur += @intCast(i32, src.len); self.prev_len = @intCast(u32, src.len); - mem.copy(u8, self.prev[0..self.prev_len], src); + @memcpy(self.prev[0..self.prev_len], src); return; } @@ -566,11 +566,11 @@ test "best speed match 2/2" { for (cases) |c| { var previous = try testing.allocator.alloc(u8, c.previous); defer testing.allocator.free(previous); - mem.set(u8, previous, 0); + @memset(previous, 0); var current = try testing.allocator.alloc(u8, c.current); defer testing.allocator.free(current); - mem.set(u8, current, 0); + @memset(current, 0); var e = DeflateFast{ .prev = previous, diff --git a/lib/std/compress/deflate/deflate_fast_test.zig b/lib/std/compress/deflate/deflate_fast_test.zig index f8efa80630..1c771d925a 100644 --- a/lib/std/compress/deflate/deflate_fast_test.zig +++ b/lib/std/compress/deflate/deflate_fast_test.zig @@ -123,13 +123,13 @@ test "best speed max match offset" { var src = try testing.allocator.alloc(u8, src_len); defer testing.allocator.free(src); - mem.copy(u8, src, abc); + @memcpy(src[0..abc.len], abc); if (!do_match_before) { - var src_offset: usize = @intCast(usize, offset - @as(i32, xyz.len)); - mem.copy(u8, src[src_offset..], xyz); + const src_offset: usize = @intCast(usize, offset - @as(i32, xyz.len)); + @memcpy(src[src_offset..][0..xyz.len], xyz); } - var src_offset: usize = @intCast(usize, offset); - mem.copy(u8, src[src_offset..], abc); + const src_offset: usize = @intCast(usize, offset); + @memcpy(src[src_offset..][0..abc.len], abc); var compressed = ArrayList(u8).init(testing.allocator); defer compressed.deinit(); diff --git a/lib/std/compress/deflate/dict_decoder.zig b/lib/std/compress/deflate/dict_decoder.zig index bf21572827..d9f240e7b4 100644 --- a/lib/std/compress/deflate/dict_decoder.zig +++ b/lib/std/compress/deflate/dict_decoder.zig @@ -47,7 +47,8 @@ pub const DictDecoder = struct { self.wr_pos = 0; if (dict != null) { - mem.copy(u8, self.hist, dict.?[dict.?.len -| self.hist.len..]); + const src = dict.?[dict.?.len -| self.hist.len..]; + @memcpy(self.hist[0..src.len], src); self.wr_pos = @intCast(u32, dict.?.len); } @@ -103,12 +104,15 @@ pub const DictDecoder = struct { self.wr_pos += 1; } + /// TODO: eliminate this function because the callsites should care about whether + /// or not their arguments alias and then they should directly call `@memcpy` or + /// `mem.copyForwards`. fn copy(dst: []u8, src: []const u8) u32 { if (src.len > dst.len) { - mem.copy(u8, dst, src[0..dst.len]); + mem.copyForwards(u8, dst, src[0..dst.len]); return @intCast(u32, dst.len); } - mem.copy(u8, dst, src); + mem.copyForwards(u8, dst[0..src.len], src); return @intCast(u32, src.len); } diff --git a/lib/std/compress/deflate/huffman_code.zig b/lib/std/compress/deflate/huffman_code.zig index e911e5219b..cf1dd71c75 100644 --- a/lib/std/compress/deflate/huffman_code.zig +++ b/lib/std/compress/deflate/huffman_code.zig @@ -202,7 +202,7 @@ pub const HuffmanEncoder = struct { // more values in the level below l.last_freq = l.next_pair_freq; // Take leaf counts from the lower level, except counts[level] remains the same. - mem.copy(u32, leaf_counts[level][0..level], leaf_counts[level - 1][0..level]); + @memcpy(leaf_counts[level][0..level], leaf_counts[level - 1][0..level]); levels[l.level - 1].needed = 2; } diff --git a/lib/std/compress/deflate/mem_utils.zig b/lib/std/compress/deflate/mem_utils.zig deleted file mode 100644 index 32b55a1bb6..0000000000 --- a/lib/std/compress/deflate/mem_utils.zig +++ /dev/null @@ -1,15 +0,0 @@ -const std = @import("std"); -const math = std.math; -const mem = std.mem; - -// Copies elements from a source `src` slice into a destination `dst` slice. -// The copy never returns an error but might not be complete if the destination is too small. -// Returns the number of elements copied, which will be the minimum of `src.len` and `dst.len`. -pub fn copy(dst: []u8, src: []const u8) usize { - if (dst.len <= src.len) { - mem.copy(u8, dst[0..], src[0..dst.len]); - } else { - mem.copy(u8, dst[0..src.len], src[0..]); - } - return math.min(dst.len, src.len); -} diff --git a/lib/std/compress/lzma.zig b/lib/std/compress/lzma.zig index 8bb8c19da1..ff05bc1c8b 100644 --- a/lib/std/compress/lzma.zig +++ b/lib/std/compress/lzma.zig @@ -75,9 +75,9 @@ pub fn Decompress(comptime ReaderType: type) type { } } const input = self.to_read.items; - const n = math.min(input.len, output.len); - mem.copy(u8, output[0..n], input[0..n]); - mem.copy(u8, input, input[n..]); + const n = @min(input.len, output.len); + @memcpy(output[0..n], input[0..n]); + @memcpy(input[0 .. input.len - n], input[n..]); self.to_read.shrinkRetainingCapacity(input.len - n); return n; } diff --git a/lib/std/compress/lzma/decode/rangecoder.zig b/lib/std/compress/lzma/decode/rangecoder.zig index 5df10be060..5a2f309fe4 100644 --- a/lib/std/compress/lzma/decode/rangecoder.zig +++ b/lib/std/compress/lzma/decode/rangecoder.zig @@ -143,7 +143,7 @@ pub fn BitTree(comptime num_bits: usize) type { } pub fn reset(self: *Self) void { - mem.set(u16, &self.probs, 0x400); + @memset(&self.probs, 0x400); } }; } diff --git a/lib/std/compress/lzma/vec2d.zig b/lib/std/compress/lzma/vec2d.zig index 1372d3592c..1ea3d1e8d5 100644 --- a/lib/std/compress/lzma/vec2d.zig +++ b/lib/std/compress/lzma/vec2d.zig @@ -13,7 +13,7 @@ pub fn Vec2D(comptime T: type) type { pub fn init(allocator: Allocator, value: T, size: struct { usize, usize }) !Self { const len = try math.mul(usize, size[0], size[1]); const data = try allocator.alloc(T, len); - mem.set(T, data, value); + @memset(data, value); return Self{ .data = data, .cols = size[1], @@ -26,7 +26,7 @@ pub fn Vec2D(comptime T: type) type { } pub fn fill(self: *Self, value: T) void { - mem.set(T, self.data, value); + @memset(self.data, value); } inline fn _get(self: Self, row: usize) ![]T { diff --git a/lib/std/compress/xz/block.zig b/lib/std/compress/xz/block.zig index 8d3d8f0353..520c335794 100644 --- a/lib/std/compress/xz/block.zig +++ b/lib/std/compress/xz/block.zig @@ -59,9 +59,9 @@ pub fn Decoder(comptime ReaderType: type) type { while (true) { if (self.to_read.items.len > 0) { const input = self.to_read.items; - const n = std.math.min(input.len, output.len); - std.mem.copy(u8, output[0..n], input[0..n]); - std.mem.copy(u8, input, input[n..]); + const n = @min(input.len, output.len); + @memcpy(output[0..n], input[0..n]); + std.mem.copyForwards(u8, input, input[n..]); self.to_read.shrinkRetainingCapacity(input.len - n); if (self.to_read.items.len == 0 and self.err != null) { if (self.err.? == DecodeError.EndOfStreamWithNoError) { diff --git a/lib/std/compress/zstandard/decode/block.zig b/lib/std/compress/zstandard/decode/block.zig index 4b7353f63c..e2042650c6 100644 --- a/lib/std/compress/zstandard/decode/block.zig +++ b/lib/std/compress/zstandard/decode/block.zig @@ -293,10 +293,10 @@ pub const DecodeState = struct { try self.decodeLiteralsSlice(dest[write_pos..], sequence.literal_length); const copy_start = write_pos + sequence.literal_length - sequence.offset; - const copy_end = copy_start + sequence.match_length; - // NOTE: we ignore the usage message for std.mem.copy and copy with dest.ptr >= src.ptr - // to allow repeats - std.mem.copy(u8, dest[write_pos + sequence.literal_length ..], dest[copy_start..copy_end]); + for ( + dest[write_pos + sequence.literal_length ..][0..sequence.match_length], + dest[copy_start..][0..sequence.match_length], + ) |*d, s| d.* = s; self.written_count += sequence.match_length; } @@ -311,7 +311,6 @@ pub const DecodeState = struct { try self.decodeLiteralsRingBuffer(dest, sequence.literal_length); const copy_start = dest.write_index + dest.data.len - sequence.offset; const copy_slice = dest.sliceAt(copy_start, sequence.match_length); - // TODO: would std.mem.copy and figuring out dest slice be better/faster? for (copy_slice.first) |b| dest.writeAssumeCapacity(b); for (copy_slice.second) |b| dest.writeAssumeCapacity(b); self.written_count += sequence.match_length; @@ -444,9 +443,8 @@ pub const DecodeState = struct { switch (self.literal_header.block_type) { .raw => { - const literals_end = self.literal_written_count + len; - const literal_data = self.literal_streams.one[self.literal_written_count..literals_end]; - std.mem.copy(u8, dest, literal_data); + const literal_data = self.literal_streams.one[self.literal_written_count..][0..len]; + @memcpy(dest[0..len], literal_data); self.literal_written_count += len; self.written_count += len; }, @@ -615,8 +613,7 @@ pub fn decodeBlock( .raw => { if (src.len < block_size) return error.MalformedBlockSize; if (dest[written_count..].len < block_size) return error.DestTooSmall; - const data = src[0..block_size]; - std.mem.copy(u8, dest[written_count..], data); + @memcpy(dest[written_count..][0..block_size], src[0..block_size]); consumed_count.* += block_size; decode_state.written_count += block_size; return block_size; diff --git a/lib/std/crypto/25519/ed25519.zig b/lib/std/crypto/25519/ed25519.zig index 66dcf3705b..6b9c1028b6 100644 --- a/lib/std/crypto/25519/ed25519.zig +++ b/lib/std/crypto/25519/ed25519.zig @@ -79,8 +79,8 @@ pub const Ed25519 = struct { const r_bytes = r.toBytes(); var t: [64]u8 = undefined; - mem.copy(u8, t[0..32], &r_bytes); - mem.copy(u8, t[32..], &public_key.bytes); + t[0..32].* = r_bytes; + t[32..].* = public_key.bytes; var h = Sha512.init(.{}); h.update(&t); @@ -200,8 +200,8 @@ pub const Ed25519 = struct { /// Return the raw signature (r, s) in little-endian format. pub fn toBytes(self: Signature) [encoded_length]u8 { var bytes: [encoded_length]u8 = undefined; - mem.copy(u8, bytes[0 .. encoded_length / 2], &self.r); - mem.copy(u8, bytes[encoded_length / 2 ..], &self.s); + bytes[0 .. encoded_length / 2].* = self.r; + bytes[encoded_length / 2 ..].* = self.s; return bytes; } @@ -260,8 +260,8 @@ pub const Ed25519 = struct { const pk_p = Curve.basePoint.clampedMul(az[0..32].*) catch return error.IdentityElement; const pk_bytes = pk_p.toBytes(); var sk_bytes: [SecretKey.encoded_length]u8 = undefined; - mem.copy(u8, &sk_bytes, &ss); - mem.copy(u8, sk_bytes[seed_length..], &pk_bytes); + sk_bytes[0..ss.len].* = ss; + sk_bytes[seed_length..].* = pk_bytes; return KeyPair{ .public_key = PublicKey.fromBytes(pk_bytes) catch unreachable, .secret_key = try SecretKey.fromBytes(sk_bytes), @@ -373,7 +373,7 @@ pub const Ed25519 = struct { var z_batch: [count]Curve.scalar.CompressedScalar = undefined; for (&z_batch) |*z| { crypto.random.bytes(z[0..16]); - mem.set(u8, z[16..], 0); + @memset(z[16..], 0); } var zs_sum = Curve.scalar.zero; @@ -444,8 +444,8 @@ pub const Ed25519 = struct { }; var prefix: [64]u8 = undefined; - mem.copy(u8, prefix[0..32], h[32..64]); - mem.copy(u8, prefix[32..64], blind_h[32..64]); + prefix[0..32].* = h[32..64].*; + prefix[32..64].* = blind_h[32..64].*; const blind_secret_key = BlindSecretKey{ .prefix = prefix, diff --git a/lib/std/crypto/25519/edwards25519.zig b/lib/std/crypto/25519/edwards25519.zig index df4f8467f9..a8ca8e2fb6 100644 --- a/lib/std/crypto/25519/edwards25519.zig +++ b/lib/std/crypto/25519/edwards25519.zig @@ -306,7 +306,7 @@ pub const Edwards25519 = struct { var pcs: [count][9]Edwards25519 = undefined; var bpc: [9]Edwards25519 = undefined; - mem.copy(Edwards25519, bpc[0..], basePointPc[0..bpc.len]); + @memcpy(&bpc, basePointPc[0..bpc.len]); for (ps, 0..) |p, i| { if (p.is_base) { @@ -439,7 +439,7 @@ pub const Edwards25519 = struct { var u: [n * H.digest_length]u8 = undefined; var i: usize = 0; while (i < n * H.digest_length) : (i += H.digest_length) { - mem.copy(u8, u[i..][0..H.digest_length], u_0[0..]); + u[i..][0..H.digest_length].* = u_0; var j: usize = 0; while (i > 0 and j < H.digest_length) : (j += 1) { u[i + j] ^= u[i + j - H.digest_length]; @@ -455,8 +455,8 @@ pub const Edwards25519 = struct { var px: [n]Edwards25519 = undefined; i = 0; while (i < n) : (i += 1) { - mem.set(u8, u_0[0 .. H.digest_length - h_l], 0); - mem.copy(u8, u_0[H.digest_length - h_l ..][0..h_l], u[i * h_l ..][0..h_l]); + @memset(u_0[0 .. H.digest_length - h_l], 0); + u_0[H.digest_length - h_l ..][0..h_l].* = u[i * h_l ..][0..h_l].*; px[i] = fromHash(u_0); } return px; diff --git a/lib/std/crypto/25519/scalar.zig b/lib/std/crypto/25519/scalar.zig index ff2e6aff80..fd6d42aebe 100644 --- a/lib/std/crypto/25519/scalar.zig +++ b/lib/std/crypto/25519/scalar.zig @@ -83,8 +83,8 @@ pub fn add(a: CompressedScalar, b: CompressedScalar) CompressedScalar { pub fn neg(s: CompressedScalar) CompressedScalar { const fs: [64]u8 = field_order_s ++ [_]u8{0} ** 32; var sx: [64]u8 = undefined; - mem.copy(u8, sx[0..32], s[0..]); - mem.set(u8, sx[32..], 0); + sx[0..32].* = s; + @memset(sx[32..], 0); var carry: u32 = 0; var i: usize = 0; while (i < 64) : (i += 1) { @@ -593,7 +593,7 @@ const ScalarDouble = struct { limbs[i] = mem.readIntLittle(u64, bytes[i * 7 ..][0..8]) & 0xffffffffffffff; } limbs[i] = @as(u64, mem.readIntLittle(u32, bytes[i * 7 ..][0..4])); - mem.set(u64, limbs[5..], 0); + @memset(limbs[5..], 0); return ScalarDouble{ .limbs = limbs }; } diff --git a/lib/std/crypto/25519/x25519.zig b/lib/std/crypto/25519/x25519.zig index 22bcf00136..b746a51968 100644 --- a/lib/std/crypto/25519/x25519.zig +++ b/lib/std/crypto/25519/x25519.zig @@ -37,7 +37,7 @@ pub const X25519 = struct { break :sk random_seed; }; var kp: KeyPair = undefined; - mem.copy(u8, &kp.secret_key, sk[0..]); + kp.secret_key = sk; kp.public_key = try X25519.recoverPublicKey(sk); return kp; } @@ -120,8 +120,8 @@ test "x25519 rfc7748 one iteration" { var i: usize = 0; while (i < 1) : (i += 1) { const output = try X25519.scalarmult(k, u); - mem.copy(u8, u[0..], k[0..]); - mem.copy(u8, k[0..], output[0..]); + u = k; + k = output; } try std.testing.expectEqual(k, expected_output); @@ -142,8 +142,8 @@ test "x25519 rfc7748 1,000 iterations" { var i: usize = 0; while (i < 1000) : (i += 1) { const output = try X25519.scalarmult(&k, &u); - mem.copy(u8, u[0..], k[0..]); - mem.copy(u8, k[0..], output[0..]); + u = k; + k = output; } try std.testing.expectEqual(k, expected_output); @@ -163,8 +163,8 @@ test "x25519 rfc7748 1,000,000 iterations" { var i: usize = 0; while (i < 1000000) : (i += 1) { const output = try X25519.scalarmult(&k, &u); - mem.copy(u8, u[0..], k[0..]); - mem.copy(u8, k[0..], output[0..]); + u = k; + k = output; } try std.testing.expectEqual(k[0..], expected_output); diff --git a/lib/std/crypto/Certificate.zig b/lib/std/crypto/Certificate.zig index 0caffba363..ec4766322c 100644 --- a/lib/std/crypto/Certificate.zig +++ b/lib/std/crypto/Certificate.zig @@ -928,7 +928,7 @@ pub const rsa = struct { pub const PSSSignature = struct { pub fn fromBytes(comptime modulus_len: usize, msg: []const u8) [modulus_len]u8 { var result = [1]u8{0} ** modulus_len; - std.mem.copy(u8, &result, msg); + std.mem.copyForwards(u8, &result, msg); return result; } @@ -1025,9 +1025,9 @@ pub const rsa = struct { // initial zero octets. var m_p = try allocator.alloc(u8, 8 + Hash.digest_length + sLen); defer allocator.free(m_p); - std.mem.copy(u8, m_p, &([_]u8{0} ** 8)); - std.mem.copy(u8, m_p[8..], &mHash); - std.mem.copy(u8, m_p[(8 + Hash.digest_length)..], salt); + std.mem.copyForwards(u8, m_p, &([_]u8{0} ** 8)); + std.mem.copyForwards(u8, m_p[8..], &mHash); + std.mem.copyForwards(u8, m_p[(8 + Hash.digest_length)..], salt); // 13. Let H' = Hash(M'), an octet string of length hLen. var h_p: [Hash.digest_length]u8 = undefined; @@ -1047,7 +1047,7 @@ pub const rsa = struct { var hash = try allocator.alloc(u8, seed.len + c.len); defer allocator.free(hash); - std.mem.copy(u8, hash, seed); + std.mem.copyForwards(u8, hash, seed); var hashed: [Hash.digest_length]u8 = undefined; while (idx < len) { @@ -1056,10 +1056,10 @@ pub const rsa = struct { c[2] = @intCast(u8, (counter >> 8) & 0xFF); c[3] = @intCast(u8, counter & 0xFF); - std.mem.copy(u8, hash[seed.len..], &c); + std.mem.copyForwards(u8, hash[seed.len..], &c); Hash.hash(hash, &hashed, .{}); - std.mem.copy(u8, out[idx..], &hashed); + std.mem.copyForwards(u8, out[idx..], &hashed); idx += hashed.len; counter += 1; diff --git a/lib/std/crypto/aegis.zig b/lib/std/crypto/aegis.zig index d7305b444a..2d37bacc3a 100644 --- a/lib/std/crypto/aegis.zig +++ b/lib/std/crypto/aegis.zig @@ -152,8 +152,8 @@ fn Aegis128LGeneric(comptime tag_bits: u9) type { state.absorb(ad[i..][0..32]); } if (ad.len % 32 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. ad.len % 32], ad[i .. i + ad.len % 32]); + @memset(src[0..], 0); + @memcpy(src[0 .. ad.len % 32], ad[i..][0 .. ad.len % 32]); state.absorb(&src); } i = 0; @@ -161,10 +161,10 @@ fn Aegis128LGeneric(comptime tag_bits: u9) type { state.enc(c[i..][0..32], m[i..][0..32]); } if (m.len % 32 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. m.len % 32], m[i .. i + m.len % 32]); + @memset(src[0..], 0); + @memcpy(src[0 .. m.len % 32], m[i..][0 .. m.len % 32]); state.enc(&dst, &src); - mem.copy(u8, c[i .. i + m.len % 32], dst[0 .. m.len % 32]); + @memcpy(c[i..][0 .. m.len % 32], dst[0 .. m.len % 32]); } tag.* = state.mac(tag_bits, ad.len, m.len); } @@ -185,8 +185,8 @@ fn Aegis128LGeneric(comptime tag_bits: u9) type { state.absorb(ad[i..][0..32]); } if (ad.len % 32 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. ad.len % 32], ad[i .. i + ad.len % 32]); + @memset(src[0..], 0); + @memcpy(src[0 .. ad.len % 32], ad[i..][0 .. ad.len % 32]); state.absorb(&src); } i = 0; @@ -194,11 +194,11 @@ fn Aegis128LGeneric(comptime tag_bits: u9) type { state.dec(m[i..][0..32], c[i..][0..32]); } if (m.len % 32 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. m.len % 32], c[i .. i + m.len % 32]); + @memset(src[0..], 0); + @memcpy(src[0 .. m.len % 32], c[i..][0 .. m.len % 32]); state.dec(&dst, &src); - mem.copy(u8, m[i .. i + m.len % 32], dst[0 .. m.len % 32]); - mem.set(u8, dst[0 .. m.len % 32], 0); + @memcpy(m[i..][0 .. m.len % 32], dst[0 .. m.len % 32]); + @memset(dst[0 .. m.len % 32], 0); const blocks = &state.blocks; blocks[0] = blocks[0].xorBlocks(AesBlock.fromBytes(dst[0..16])); blocks[4] = blocks[4].xorBlocks(AesBlock.fromBytes(dst[16..32])); @@ -334,8 +334,8 @@ fn Aegis256Generic(comptime tag_bits: u9) type { state.enc(&dst, ad[i..][0..16]); } if (ad.len % 16 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. ad.len % 16], ad[i .. i + ad.len % 16]); + @memset(src[0..], 0); + @memcpy(src[0 .. ad.len % 16], ad[i..][0 .. ad.len % 16]); state.enc(&dst, &src); } i = 0; @@ -343,10 +343,10 @@ fn Aegis256Generic(comptime tag_bits: u9) type { state.enc(c[i..][0..16], m[i..][0..16]); } if (m.len % 16 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. m.len % 16], m[i .. i + m.len % 16]); + @memset(src[0..], 0); + @memcpy(src[0 .. m.len % 16], m[i..][0 .. m.len % 16]); state.enc(&dst, &src); - mem.copy(u8, c[i .. i + m.len % 16], dst[0 .. m.len % 16]); + @memcpy(c[i..][0 .. m.len % 16], dst[0 .. m.len % 16]); } tag.* = state.mac(tag_bits, ad.len, m.len); } @@ -367,8 +367,8 @@ fn Aegis256Generic(comptime tag_bits: u9) type { state.enc(&dst, ad[i..][0..16]); } if (ad.len % 16 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. ad.len % 16], ad[i .. i + ad.len % 16]); + @memset(src[0..], 0); + @memcpy(src[0 .. ad.len % 16], ad[i..][0 .. ad.len % 16]); state.enc(&dst, &src); } i = 0; @@ -376,11 +376,11 @@ fn Aegis256Generic(comptime tag_bits: u9) type { state.dec(m[i..][0..16], c[i..][0..16]); } if (m.len % 16 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. m.len % 16], c[i .. i + m.len % 16]); + @memset(src[0..], 0); + @memcpy(src[0 .. m.len % 16], c[i..][0 .. m.len % 16]); state.dec(&dst, &src); - mem.copy(u8, m[i .. i + m.len % 16], dst[0 .. m.len % 16]); - mem.set(u8, dst[0 .. m.len % 16], 0); + @memcpy(m[i..][0 .. m.len % 16], dst[0 .. m.len % 16]); + @memset(dst[0 .. m.len % 16], 0); const blocks = &state.blocks; blocks[0] = blocks[0].xorBlocks(AesBlock.fromBytes(&dst)); } @@ -457,7 +457,7 @@ fn AegisMac(comptime T: type) type { self.msg_len += b.len; const len_partial = @min(b.len, block_length - self.off); - mem.copy(u8, self.buf[self.off..][0..len_partial], b[0..len_partial]); + @memcpy(self.buf[self.off..][0..len_partial], b[0..len_partial]); self.off += len_partial; if (self.off < block_length) { return; @@ -470,7 +470,7 @@ fn AegisMac(comptime T: type) type { self.state.absorb(b[i..][0..block_length]); } if (i != b.len) { - mem.copy(u8, self.buf[0..], b[i..]); + @memcpy(self.buf[0..], b[i..]); self.off = b.len - i; } } @@ -479,7 +479,7 @@ fn AegisMac(comptime T: type) type { pub fn final(self: *Self, out: *[mac_length]u8) void { if (self.off > 0) { var pad = [_]u8{0} ** block_length; - mem.copy(u8, pad[0..], self.buf[0..self.off]); + @memcpy(pad[0..self.off], self.buf[0..self.off]); self.state.absorb(&pad); } out.* = self.state.mac(T.tag_length * 8, self.msg_len, 0); diff --git a/lib/std/crypto/aes_gcm.zig b/lib/std/crypto/aes_gcm.zig index 4ec53371e6..660073d3ae 100644 --- a/lib/std/crypto/aes_gcm.zig +++ b/lib/std/crypto/aes_gcm.zig @@ -31,7 +31,7 @@ fn AesGcm(comptime Aes: anytype) type { var t: [16]u8 = undefined; var j: [16]u8 = undefined; - mem.copy(u8, j[0..nonce_length], npub[0..]); + j[0..nonce_length].* = npub; mem.writeIntBig(u32, j[nonce_length..][0..4], 1); aes.encrypt(&t, &j); @@ -64,7 +64,7 @@ fn AesGcm(comptime Aes: anytype) type { var t: [16]u8 = undefined; var j: [16]u8 = undefined; - mem.copy(u8, j[0..nonce_length], npub[0..]); + j[0..nonce_length].* = npub; mem.writeIntBig(u32, j[nonce_length..][0..4], 1); aes.encrypt(&t, &j); diff --git a/lib/std/crypto/aes_ocb.zig b/lib/std/crypto/aes_ocb.zig index 83e33e5fca..6d5ce3779a 100644 --- a/lib/std/crypto/aes_ocb.zig +++ b/lib/std/crypto/aes_ocb.zig @@ -75,7 +75,7 @@ fn AesOcb(comptime Aes: anytype) type { if (leftover > 0) { xorWith(&offset, lx.star); var padded = [_]u8{0} ** 16; - mem.copy(u8, padded[0..leftover], a[i * 16 ..][0..leftover]); + @memcpy(padded[0..leftover], a[i * 16 ..][0..leftover]); padded[leftover] = 1; var e = xorBlocks(offset, padded); aes_enc_ctx.encrypt(&e, &e); @@ -88,7 +88,7 @@ fn AesOcb(comptime Aes: anytype) type { var nx = [_]u8{0} ** 16; nx[0] = @intCast(u8, @truncate(u7, tag_length * 8) << 1); nx[16 - nonce_length - 1] = 1; - mem.copy(u8, nx[16 - nonce_length ..], &npub); + nx[nx.len - nonce_length ..].* = npub; const bottom = @truncate(u6, nx[15]); nx[15] &= 0xc0; @@ -132,14 +132,14 @@ fn AesOcb(comptime Aes: anytype) type { xorWith(&offset, lt[@ctz(i + 1 + j)]); offsets[j] = offset; const p = m[(i + j) * 16 ..][0..16].*; - mem.copy(u8, es[j * 16 ..][0..16], &xorBlocks(p, offsets[j])); + es[j * 16 ..][0..16].* = xorBlocks(p, offsets[j]); xorWith(&sum, p); } aes_enc_ctx.encryptWide(wb, &es, &es); j = 0; while (j < wb) : (j += 1) { const e = es[j * 16 ..][0..16].*; - mem.copy(u8, c[(i + j) * 16 ..][0..16], &xorBlocks(e, offsets[j])); + c[(i + j) * 16 ..][0..16].* = xorBlocks(e, offsets[j]); } } while (i < full_blocks) : (i += 1) { @@ -147,7 +147,7 @@ fn AesOcb(comptime Aes: anytype) type { const p = m[i * 16 ..][0..16].*; var e = xorBlocks(p, offset); aes_enc_ctx.encrypt(&e, &e); - mem.copy(u8, c[i * 16 ..][0..16], &xorBlocks(e, offset)); + c[i * 16 ..][0..16].* = xorBlocks(e, offset); xorWith(&sum, p); } const leftover = m.len % 16; @@ -159,7 +159,7 @@ fn AesOcb(comptime Aes: anytype) type { c[i * 16 + j] = pad[j] ^ x; } var e = [_]u8{0} ** 16; - mem.copy(u8, e[0..leftover], m[i * 16 ..][0..leftover]); + @memcpy(e[0..leftover], m[i * 16 ..][0..leftover]); e[leftover] = 0x80; xorWith(&sum, e); } @@ -196,13 +196,13 @@ fn AesOcb(comptime Aes: anytype) type { xorWith(&offset, lt[@ctz(i + 1 + j)]); offsets[j] = offset; const q = c[(i + j) * 16 ..][0..16].*; - mem.copy(u8, es[j * 16 ..][0..16], &xorBlocks(q, offsets[j])); + es[j * 16 ..][0..16].* = xorBlocks(q, offsets[j]); } aes_dec_ctx.decryptWide(wb, &es, &es); j = 0; while (j < wb) : (j += 1) { const p = xorBlocks(es[j * 16 ..][0..16].*, offsets[j]); - mem.copy(u8, m[(i + j) * 16 ..][0..16], &p); + m[(i + j) * 16 ..][0..16].* = p; xorWith(&sum, p); } } @@ -212,7 +212,7 @@ fn AesOcb(comptime Aes: anytype) type { var e = xorBlocks(q, offset); aes_dec_ctx.decrypt(&e, &e); const p = xorBlocks(e, offset); - mem.copy(u8, m[i * 16 ..][0..16], &p); + m[i * 16 ..][0..16].* = p; xorWith(&sum, p); } const leftover = m.len % 16; @@ -224,7 +224,7 @@ fn AesOcb(comptime Aes: anytype) type { m[i * 16 + j] = pad[j] ^ x; } var e = [_]u8{0} ** 16; - mem.copy(u8, e[0..leftover], m[i * 16 ..][0..leftover]); + @memcpy(e[0..leftover], m[i * 16 ..][0..leftover]); e[leftover] = 0x80; xorWith(&sum, e); } diff --git a/lib/std/crypto/argon2.zig b/lib/std/crypto/argon2.zig index 0112e81c6a..43dbe3e332 100644 --- a/lib/std/crypto/argon2.zig +++ b/lib/std/crypto/argon2.zig @@ -149,7 +149,7 @@ fn blake2bLong(out: []u8, in: []const u8) void { h.update(&outlen_bytes); h.update(in); h.final(&out_buf); - mem.copy(u8, out, out_buf[0..out.len]); + @memcpy(out, out_buf[0..out.len]); return; } @@ -158,19 +158,19 @@ fn blake2bLong(out: []u8, in: []const u8) void { h.update(in); h.final(&out_buf); var out_slice = out; - mem.copy(u8, out_slice, out_buf[0 .. H.digest_length / 2]); + out_slice[0 .. H.digest_length / 2].* = out_buf[0 .. H.digest_length / 2].*; out_slice = out_slice[H.digest_length / 2 ..]; var in_buf: [H.digest_length]u8 = undefined; while (out_slice.len > H.digest_length) { - mem.copy(u8, &in_buf, &out_buf); + in_buf = out_buf; H.hash(&in_buf, &out_buf, .{}); - mem.copy(u8, out_slice, out_buf[0 .. H.digest_length / 2]); + out_slice[0 .. H.digest_length / 2].* = out_buf[0 .. H.digest_length / 2].*; out_slice = out_slice[H.digest_length / 2 ..]; } - mem.copy(u8, &in_buf, &out_buf); + in_buf = out_buf; H.hash(&in_buf, &out_buf, .{ .expected_out_bits = out_slice.len * 8 }); - mem.copy(u8, out_slice, out_buf[0..out_slice.len]); + @memcpy(out_slice, out_buf[0..out_slice.len]); } fn initBlocks( @@ -494,7 +494,7 @@ pub fn kdf( if (params.t < 1 or params.p < 1) return KdfError.WeakParameters; var h0 = initHash(password, salt, params, derived_key.len, mode); - const memory = math.max( + const memory = @max( params.m / (sync_points * params.p) * (sync_points * params.p), 2 * sync_points * params.p, ); @@ -877,7 +877,7 @@ test "kdf" { .hash = "1640b932f4b60e272f5d2207b9a9c626ffa1bd88d2349016", }, }; - inline for (test_vectors) |v| { + for (test_vectors) |v| { var want: [24]u8 = undefined; _ = try std.fmt.hexToBytes(&want, v.hash); diff --git a/lib/std/crypto/ascon.zig b/lib/std/crypto/ascon.zig index f37d9acea5..ae4bb57d29 100644 --- a/lib/std/crypto/ascon.zig +++ b/lib/std/crypto/ascon.zig @@ -34,7 +34,7 @@ pub fn State(comptime endian: builtin.Endian) type { /// Initialize the state from a slice of bytes. pub fn init(initial_state: [block_bytes]u8) Self { var state = Self{ .st = undefined }; - mem.copy(u8, state.asBytes(), &initial_state); + @memcpy(state.asBytes(), &initial_state); state.endianSwap(); return state; } @@ -87,7 +87,7 @@ pub fn State(comptime endian: builtin.Endian) type { } if (i < bytes.len) { var padded = [_]u8{0} ** 8; - mem.copy(u8, padded[0 .. bytes.len - i], bytes[i..]); + @memcpy(padded[0 .. bytes.len - i], bytes[i..]); self.st[i / 8] = mem.readInt(u64, padded[0..], endian); } } @@ -109,7 +109,7 @@ pub fn State(comptime endian: builtin.Endian) type { } if (i < bytes.len) { var padded = [_]u8{0} ** 8; - mem.copy(u8, padded[0 .. bytes.len - i], bytes[i..]); + @memcpy(padded[0 .. bytes.len - i], bytes[i..]); self.st[i / 8] ^= mem.readInt(u64, padded[0..], endian); } } @@ -123,7 +123,7 @@ pub fn State(comptime endian: builtin.Endian) type { if (i < out.len) { var padded = [_]u8{0} ** 8; mem.writeInt(u64, padded[0..], self.st[i / 8], endian); - mem.copy(u8, out[i..], padded[0 .. out.len - i]); + @memcpy(out[i..], padded[0 .. out.len - i]); } } @@ -138,16 +138,16 @@ pub fn State(comptime endian: builtin.Endian) type { } if (i < in.len) { var padded = [_]u8{0} ** 8; - mem.copy(u8, padded[0 .. in.len - i], in[i..]); + @memcpy(padded[0 .. in.len - i], in[i..]); const x = mem.readIntNative(u64, &padded) ^ mem.nativeTo(u64, self.st[i / 8], endian); mem.writeIntNative(u64, &padded, x); - mem.copy(u8, out[i..], padded[0 .. in.len - i]); + @memcpy(out[i..], padded[0 .. in.len - i]); } } /// Set the words storing the bytes of a given range to zero. pub fn clear(self: *Self, from: usize, to: usize) void { - mem.set(u64, self.st[from / 8 .. (to + 7) / 8], 0); + @memset(self.st[from / 8 .. (to + 7) / 8], 0); } /// Clear the entire state, disabling compiler optimizations. diff --git a/lib/std/crypto/bcrypt.zig b/lib/std/crypto/bcrypt.zig index 2191ab0d9e..dda5f5e377 100644 --- a/lib/std/crypto/bcrypt.zig +++ b/lib/std/crypto/bcrypt.zig @@ -416,8 +416,8 @@ pub fn bcrypt( ) [dk_length]u8 { var state = State{}; var password_buf: [73]u8 = undefined; - const trimmed_len = math.min(password.len, password_buf.len - 1); - mem.copy(u8, password_buf[0..], password[0..trimmed_len]); + const trimmed_len = @min(password.len, password_buf.len - 1); + @memcpy(password_buf[0..trimmed_len], password[0..trimmed_len]); password_buf[trimmed_len] = 0; var passwordZ = password_buf[0 .. trimmed_len + 1]; state.expand(salt[0..], passwordZ); @@ -626,7 +626,7 @@ const CryptFormatHasher = struct { crypto.random.bytes(&salt); const hash = crypt_format.strHashInternal(password, salt, params); - mem.copy(u8, buf, &hash); + @memcpy(buf[0..hash.len], &hash); return buf[0..pwhash_str_length]; } diff --git a/lib/std/crypto/benchmark.zig b/lib/std/crypto/benchmark.zig index f512c513e7..696a9c3107 100644 --- a/lib/std/crypto/benchmark.zig +++ b/lib/std/crypto/benchmark.zig @@ -113,8 +113,8 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c var i: usize = 0; while (i < exchange_count) : (i += 1) { const out = try DhKeyExchange.scalarmult(secret, public); - mem.copy(u8, secret[0..16], out[0..16]); - mem.copy(u8, public[0..16], out[16..32]); + secret[0..16].* = out[0..16].*; + public[0..16].* = out[16..32].*; mem.doNotOptimizeAway(&out); } } diff --git a/lib/std/crypto/blake2.zig b/lib/std/crypto/blake2.zig index 85c26ce599..316ea5e6b7 100644 --- a/lib/std/crypto/blake2.zig +++ b/lib/std/crypto/blake2.zig @@ -76,7 +76,7 @@ pub fn Blake2s(comptime out_bits: usize) type { comptime debug.assert(8 <= out_bits and out_bits <= 256); var d: Self = undefined; - mem.copy(u32, d.h[0..], iv[0..]); + d.h = iv; const key_len = if (options.key) |key| key.len else 0; // default parameters @@ -93,7 +93,7 @@ pub fn Blake2s(comptime out_bits: usize) type { d.h[7] ^= mem.readIntLittle(u32, context[4..8]); } if (key_len > 0) { - mem.set(u8, d.buf[key_len..], 0); + @memset(d.buf[key_len..], 0); d.update(options.key.?); d.buf_len = 64; } @@ -112,7 +112,7 @@ pub fn Blake2s(comptime out_bits: usize) type { // Partial buffer exists from previous update. Copy into buffer then hash. if (d.buf_len != 0 and d.buf_len + b.len > 64) { off += 64 - d.buf_len; - mem.copy(u8, d.buf[d.buf_len..], b[0..off]); + @memcpy(d.buf[d.buf_len..][0..off], b[0..off]); d.t += 64; d.round(d.buf[0..], false); d.buf_len = 0; @@ -125,16 +125,17 @@ pub fn Blake2s(comptime out_bits: usize) type { } // Copy any remainder for next pass. - mem.copy(u8, d.buf[d.buf_len..], b[off..]); - d.buf_len += @intCast(u8, b[off..].len); + const b_slice = b[off..]; + @memcpy(d.buf[d.buf_len..][0..b_slice.len], b_slice); + d.buf_len += @intCast(u8, b_slice.len); } pub fn final(d: *Self, out: *[digest_length]u8) void { - mem.set(u8, d.buf[d.buf_len..], 0); + @memset(d.buf[d.buf_len..], 0); d.t += d.buf_len; d.round(d.buf[0..], true); for (&d.h) |*x| x.* = mem.nativeToLittle(u32, x.*); - mem.copy(u8, out[0..], @ptrCast(*[digest_length]u8, &d.h)); + out.* = @ptrCast(*[digest_length]u8, &d.h).*; } fn round(d: *Self, b: *const [64]u8, last: bool) void { @@ -511,7 +512,7 @@ pub fn Blake2b(comptime out_bits: usize) type { comptime debug.assert(8 <= out_bits and out_bits <= 512); var d: Self = undefined; - mem.copy(u64, d.h[0..], iv[0..]); + d.h = iv; const key_len = if (options.key) |key| key.len else 0; // default parameters @@ -528,7 +529,7 @@ pub fn Blake2b(comptime out_bits: usize) type { d.h[7] ^= mem.readIntLittle(u64, context[8..16]); } if (key_len > 0) { - mem.set(u8, d.buf[key_len..], 0); + @memset(d.buf[key_len..], 0); d.update(options.key.?); d.buf_len = 128; } @@ -547,7 +548,7 @@ pub fn Blake2b(comptime out_bits: usize) type { // Partial buffer exists from previous update. Copy into buffer then hash. if (d.buf_len != 0 and d.buf_len + b.len > 128) { off += 128 - d.buf_len; - mem.copy(u8, d.buf[d.buf_len..], b[0..off]); + @memcpy(d.buf[d.buf_len..][0..off], b[0..off]); d.t += 128; d.round(d.buf[0..], false); d.buf_len = 0; @@ -560,16 +561,17 @@ pub fn Blake2b(comptime out_bits: usize) type { } // Copy any remainder for next pass. - mem.copy(u8, d.buf[d.buf_len..], b[off..]); - d.buf_len += @intCast(u8, b[off..].len); + const b_slice = b[off..]; + @memcpy(d.buf[d.buf_len..][0..b_slice.len], b_slice); + d.buf_len += @intCast(u8, b_slice.len); } pub fn final(d: *Self, out: *[digest_length]u8) void { - mem.set(u8, d.buf[d.buf_len..], 0); + @memset(d.buf[d.buf_len..], 0); d.t += d.buf_len; d.round(d.buf[0..], true); for (&d.h) |*x| x.* = mem.nativeToLittle(u64, x.*); - mem.copy(u8, out[0..], @ptrCast(*[digest_length]u8, &d.h)); + out.* = @ptrCast(*[digest_length]u8, &d.h).*; } fn round(d: *Self, b: *const [128]u8, last: bool) void { diff --git a/lib/std/crypto/blake3.zig b/lib/std/crypto/blake3.zig index 36d717387f..fb580fda13 100644 --- a/lib/std/crypto/blake3.zig +++ b/lib/std/crypto/blake3.zig @@ -253,7 +253,7 @@ const Output = struct { while (out_word_it.next()) |out_word| { var word_bytes: [4]u8 = undefined; mem.writeIntLittle(u32, &word_bytes, words[word_counter]); - mem.copy(u8, out_word, word_bytes[0..out_word.len]); + @memcpy(out_word, word_bytes[0..out_word.len]); word_counter += 1; } output_block_counter += 1; @@ -284,7 +284,7 @@ const ChunkState = struct { fn fillBlockBuf(self: *ChunkState, input: []const u8) []const u8 { const want = BLOCK_LEN - self.block_len; const take = math.min(want, input.len); - mem.copy(u8, self.block[self.block_len..][0..take], input[0..take]); + @memcpy(self.block[self.block_len..][0..take], input[0..take]); self.block_len += @truncate(u8, take); return input[take..]; } @@ -336,8 +336,8 @@ fn parentOutput( flags: u8, ) Output { var block_words: [16]u32 align(16) = undefined; - mem.copy(u32, block_words[0..8], left_child_cv[0..]); - mem.copy(u32, block_words[8..], right_child_cv[0..]); + block_words[0..8].* = left_child_cv; + block_words[8..].* = right_child_cv; return Output{ .input_chaining_value = key, .block_words = block_words, diff --git a/lib/std/crypto/chacha20.zig b/lib/std/crypto/chacha20.zig index aa0f148be9..bffc70f500 100644 --- a/lib/std/crypto/chacha20.zig +++ b/lib/std/crypto/chacha20.zig @@ -211,7 +211,7 @@ fn ChaChaVecImpl(comptime rounds_nb: usize) type { var buf: [64]u8 = undefined; hashToBytes(buf[0..], x); - mem.copy(u8, out[i..], buf[0 .. out.len - i]); + @memcpy(out[i..], buf[0 .. out.len - i]); } } @@ -372,7 +372,7 @@ fn ChaChaNonVecImpl(comptime rounds_nb: usize) type { var buf: [64]u8 = undefined; hashToBytes(buf[0..], x); - mem.copy(u8, out[i..], buf[0 .. out.len - i]); + @memcpy(out[i..], buf[0 .. out.len - i]); } } @@ -413,8 +413,8 @@ fn keyToWords(key: [32]u8) [8]u32 { fn extend(key: [32]u8, nonce: [24]u8, comptime rounds_nb: usize) struct { key: [32]u8, nonce: [12]u8 } { var subnonce: [12]u8 = undefined; - mem.set(u8, subnonce[0..4], 0); - mem.copy(u8, subnonce[4..], nonce[16..24]); + @memset(subnonce[0..4], 0); + subnonce[4..].* = nonce[16..24].*; return .{ .key = ChaChaImpl(rounds_nb).hchacha20(nonce[0..16].*, key), .nonce = subnonce, diff --git a/lib/std/crypto/ecdsa.zig b/lib/std/crypto/ecdsa.zig index 37ae57a7e6..e552af2e26 100644 --- a/lib/std/crypto/ecdsa.zig +++ b/lib/std/crypto/ecdsa.zig @@ -102,8 +102,8 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { /// Return the raw signature (r, s) in big-endian format. pub fn toBytes(self: Signature) [encoded_length]u8 { var bytes: [encoded_length]u8 = undefined; - mem.copy(u8, bytes[0 .. encoded_length / 2], &self.r); - mem.copy(u8, bytes[encoded_length / 2 ..], &self.s); + @memcpy(bytes[0 .. encoded_length / 2], &self.r); + @memcpy(bytes[encoded_length / 2 ..], &self.s); return bytes; } @@ -325,11 +325,11 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { fn reduceToScalar(comptime unreduced_len: usize, s: [unreduced_len]u8) Curve.scalar.Scalar { if (unreduced_len >= 48) { var xs = [_]u8{0} ** 64; - mem.copy(u8, xs[xs.len - s.len ..], s[0..]); + @memcpy(xs[xs.len - s.len ..], s[0..]); return Curve.scalar.Scalar.fromBytes64(xs, .Big); } var xs = [_]u8{0} ** 48; - mem.copy(u8, xs[xs.len - s.len ..], s[0..]); + @memcpy(xs[xs.len - s.len ..], s[0..]); return Curve.scalar.Scalar.fromBytes48(xs, .Big); } @@ -345,14 +345,13 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { const m_x = m[m_v.len + 1 + noise_length ..][0..secret_key.len]; const m_h = m[m.len - h.len ..]; - mem.set(u8, m_v, 0x01); + @memset(m_v, 0x01); m_i.* = 0x00; - if (noise) |n| mem.copy(u8, m_z, &n); - mem.copy(u8, m_x, &secret_key); - mem.copy(u8, m_h, &h); + if (noise) |n| @memcpy(m_z, &n); + @memcpy(m_x, &secret_key); + @memcpy(m_h, &h); Hmac.create(&k, &m, &k); Hmac.create(m_v, m_v, &k); - mem.copy(u8, m_v, m_v); m_i.* = 0x01; Hmac.create(&k, &m, &k); Hmac.create(m_v, m_v, &k); @@ -361,10 +360,9 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { while (t_off < t.len) : (t_off += m_v.len) { const t_end = @min(t_off + m_v.len, t.len); Hmac.create(m_v, m_v, &k); - std.mem.copy(u8, t[t_off..t_end], m_v[0 .. t_end - t_off]); + @memcpy(t[t_off..t_end], m_v[0 .. t_end - t_off]); } if (Curve.scalar.Scalar.fromBytes(t, .Big)) |s| return s else |_| {} - mem.copy(u8, m_v, m_v); m_i.* = 0x00; Hmac.create(&k, m[0 .. m_v.len + 1], &k); Hmac.create(m_v, m_v, &k); diff --git a/lib/std/crypto/hkdf.zig b/lib/std/crypto/hkdf.zig index 7102ffe780..9163ba9d15 100644 --- a/lib/std/crypto/hkdf.zig +++ b/lib/std/crypto/hkdf.zig @@ -63,7 +63,7 @@ pub fn Hkdf(comptime Hmac: type) type { st.update(&counter); var tmp: [prk_length]u8 = undefined; st.final(tmp[0..prk_length]); - mem.copy(u8, out[i..][0..left], tmp[0..left]); + @memcpy(out[i..][0..left], tmp[0..left]); } } }; diff --git a/lib/std/crypto/hmac.zig b/lib/std/crypto/hmac.zig index f279132ee8..8d0daa3032 100644 --- a/lib/std/crypto/hmac.zig +++ b/lib/std/crypto/hmac.zig @@ -38,12 +38,12 @@ pub fn Hmac(comptime Hash: type) type { // Normalize key length to block size of hash if (key.len > Hash.block_length) { Hash.hash(key, scratch[0..mac_length], .{}); - mem.set(u8, scratch[mac_length..Hash.block_length], 0); + @memset(scratch[mac_length..Hash.block_length], 0); } else if (key.len < Hash.block_length) { - mem.copy(u8, scratch[0..key.len], key); - mem.set(u8, scratch[key.len..Hash.block_length], 0); + @memcpy(scratch[0..key.len], key); + @memset(scratch[key.len..Hash.block_length], 0); } else { - mem.copy(u8, scratch[0..], key); + @memcpy(&scratch, key); } for (&ctx.o_key_pad, 0..) |*b, i| { diff --git a/lib/std/crypto/isap.zig b/lib/std/crypto/isap.zig index 0888cfa4dd..5b0da739de 100644 --- a/lib/std/crypto/isap.zig +++ b/lib/std/crypto/isap.zig @@ -43,7 +43,7 @@ pub const IsapA128A = struct { } } else { var padded = [_]u8{0} ** 8; - mem.copy(u8, padded[0..left], m[i..]); + @memcpy(padded[0..left], m[i..]); padded[left] = 0x80; isap.st.addBytes(&padded); isap.st.permute(); diff --git a/lib/std/crypto/keccak_p.zig b/lib/std/crypto/keccak_p.zig index af7c12c8c2..9226f2f6d4 100644 --- a/lib/std/crypto/keccak_p.zig +++ b/lib/std/crypto/keccak_p.zig @@ -68,7 +68,7 @@ pub fn KeccakF(comptime f: u11) type { } if (i < bytes.len) { var padded = [_]u8{0} ** @sizeOf(T); - mem.copy(u8, padded[0 .. bytes.len - i], bytes[i..]); + @memcpy(padded[0 .. bytes.len - i], bytes[i..]); self.st[i / @sizeOf(T)] = mem.readIntLittle(T, padded[0..]); } } @@ -87,7 +87,7 @@ pub fn KeccakF(comptime f: u11) type { } if (i < bytes.len) { var padded = [_]u8{0} ** @sizeOf(T); - mem.copy(u8, padded[0 .. bytes.len - i], bytes[i..]); + @memcpy(padded[0 .. bytes.len - i], bytes[i..]); self.st[i / @sizeOf(T)] ^= mem.readIntLittle(T, padded[0..]); } } @@ -101,7 +101,7 @@ pub fn KeccakF(comptime f: u11) type { if (i < out.len) { var padded = [_]u8{0} ** @sizeOf(T); mem.writeIntLittle(T, padded[0..], self.st[i / @sizeOf(T)]); - mem.copy(u8, out[i..], padded[0 .. out.len - i]); + @memcpy(out[i..], padded[0 .. out.len - i]); } } @@ -116,16 +116,16 @@ pub fn KeccakF(comptime f: u11) type { } if (i < in.len) { var padded = [_]u8{0} ** @sizeOf(T); - mem.copy(u8, padded[0 .. in.len - i], in[i..]); + @memcpy(padded[0 .. in.len - i], in[i..]); const x = mem.readIntNative(T, &padded) ^ mem.nativeToLittle(T, self.st[i / @sizeOf(T)]); mem.writeIntNative(T, &padded, x); - mem.copy(u8, out[i..], padded[0 .. in.len - i]); + @memcpy(out[i..], padded[0 .. in.len - i]); } } /// Set the words storing the bytes of a given range to zero. pub fn clear(self: *Self, from: usize, to: usize) void { - mem.set(T, self.st[from / @sizeOf(T) .. (to + @sizeOf(T) - 1) / @sizeOf(T)], 0); + @memset(self.st[from / @sizeOf(T) .. (to + @sizeOf(T) - 1) / @sizeOf(T)], 0); } /// Clear the entire state, disabling compiler optimizations. @@ -215,7 +215,7 @@ pub fn State(comptime f: u11, comptime capacity: u11, comptime delim: u8, compti var bytes = bytes_; if (self.offset > 0) { const left = math.min(rate - self.offset, bytes.len); - mem.copy(u8, self.buf[self.offset..], bytes[0..left]); + @memcpy(self.buf[self.offset..][0..left], bytes[0..left]); self.offset += left; if (self.offset == rate) { self.offset = 0; @@ -231,7 +231,7 @@ pub fn State(comptime f: u11, comptime capacity: u11, comptime delim: u8, compti bytes = bytes[rate..]; } if (bytes.len > 0) { - mem.copy(u8, &self.buf, bytes); + @memcpy(self.buf[0..bytes.len], bytes); self.offset = bytes.len; } } diff --git a/lib/std/crypto/kyber_d00.zig b/lib/std/crypto/kyber_d00.zig index 21fdb6ff17..b52f9f475d 100644 --- a/lib/std/crypto/kyber_d00.zig +++ b/lib/std/crypto/kyber_d00.zig @@ -323,9 +323,9 @@ fn Kyber(comptime p: Params) type { s += InnerSk.bytes_length; ret.pk = InnerPk.fromBytes(buf[s .. s + InnerPk.bytes_length]); s += InnerPk.bytes_length; - mem.copy(u8, &ret.hpk, buf[s .. s + h_length]); + ret.hpk = buf[s..][0..h_length].*; s += h_length; - mem.copy(u8, &ret.z, buf[s .. s + shared_length]); + ret.z = buf[s..][0..shared_length].*; return ret; } }; @@ -345,7 +345,7 @@ fn Kyber(comptime p: Params) type { break :sk random_seed; }; var ret: KeyPair = undefined; - mem.copy(u8, &ret.secret_key.z, seed[inner_seed_length..seed_length]); + ret.secret_key.z = seed[inner_seed_length..seed_length].*; // Generate inner key innerKeyFromSeed( @@ -356,7 +356,7 @@ fn Kyber(comptime p: Params) type { ret.secret_key.pk = ret.public_key.pk; // Copy over z from seed. - mem.copy(u8, &ret.secret_key.z, seed[inner_seed_length..seed_length]); + ret.secret_key.z = seed[inner_seed_length..seed_length].*; // Compute H(pk) var h = sha3.Sha3_256.init(.{}); @@ -418,7 +418,7 @@ fn Kyber(comptime p: Params) type { fn fromBytes(buf: *const [bytes_length]u8) InnerPk { var ret: InnerPk = undefined; ret.th = V.fromBytes(buf[0..V.bytes_length]).normalize(); - mem.copy(u8, &ret.rho, buf[V.bytes_length..bytes_length]); + ret.rho = buf[V.bytes_length..bytes_length].*; ret.aT = M.uniform(ret.rho, true); return ret; } @@ -459,7 +459,7 @@ fn Kyber(comptime p: Params) type { var h = sha3.Sha3_512.init(.{}); h.update(&seed); h.final(&expanded_seed); - mem.copy(u8, &pk.rho, expanded_seed[0..32]); + pk.rho = expanded_seed[0..32].*; const sigma = expanded_seed[32..64]; pk.aT = M.uniform(pk.rho, false); // Expand ρ to A; we'll transpose later on @@ -1381,7 +1381,7 @@ fn Vec(comptime K: u8) type { const cs = comptime Poly.compressedSize(d); var ret: [compressedSize(d)]u8 = undefined; inline for (0..K) |i| { - mem.copy(u8, ret[i * cs .. (i + 1) * cs], &v.ps[i].compress(d)); + ret[i * cs .. (i + 1) * cs].* = v.ps[i].compress(d); } return ret; } @@ -1399,11 +1399,7 @@ fn Vec(comptime K: u8) type { fn toBytes(v: Self) [bytes_length]u8 { var ret: [bytes_length]u8 = undefined; inline for (0..K) |i| { - mem.copy( - u8, - ret[i * Poly.bytes_length .. (i + 1) * Poly.bytes_length], - &v.ps[i].toBytes(), - ); + ret[i * Poly.bytes_length .. (i + 1) * Poly.bytes_length].* = v.ps[i].toBytes(); } return ret; } @@ -1479,7 +1475,7 @@ test "MulHat" { const p2 = a.ntt().mulHat(b.ntt()).barrettReduce().invNTT().normalize(); var p: Poly = undefined; - mem.set(i16, &p.cs, 0); + @memset(&p.cs, 0); for (0..N) |i| { for (0..N) |j| { @@ -1742,15 +1738,15 @@ const NistDRBG = struct { g.incV(); var block: [16]u8 = undefined; ctx.encrypt(&block, &g.v); - mem.copy(u8, buf[i * 16 .. (i + 1) * 16], &block); + buf[i * 16 ..][0..16].* = block; } if (pd) |p| { for (&buf, p) |*b, x| { b.* ^= x; } } - mem.copy(u8, &g.key, buf[0..32]); - mem.copy(u8, &g.v, buf[32..48]); + g.key = buf[0..32].*; + g.v = buf[32..48].*; } // randombytes. @@ -1763,10 +1759,10 @@ const NistDRBG = struct { g.incV(); ctx.encrypt(&block, &g.v); if (dst.len < 16) { - mem.copy(u8, dst, block[0..dst.len]); + @memcpy(dst, block[0..dst.len]); break; } - mem.copy(u8, dst, &block); + dst[0..block.len].* = block; dst = dst[16..dst.len]; } g.update(null); diff --git a/lib/std/crypto/md5.zig b/lib/std/crypto/md5.zig index 6276fadb43..bd4a78c032 100644 --- a/lib/std/crypto/md5.zig +++ b/lib/std/crypto/md5.zig @@ -66,7 +66,7 @@ pub const Md5 = struct { // Partial buffer exists from previous update. Copy into buffer then hash. if (d.buf_len != 0 and d.buf_len + b.len >= 64) { off += 64 - d.buf_len; - mem.copy(u8, d.buf[d.buf_len..], b[0..off]); + @memcpy(d.buf[d.buf_len..][0..off], b[0..off]); d.round(&d.buf); d.buf_len = 0; @@ -78,8 +78,9 @@ pub const Md5 = struct { } // Copy any remainder for next pass. - mem.copy(u8, d.buf[d.buf_len..], b[off..]); - d.buf_len += @intCast(u8, b[off..].len); + const b_slice = b[off..]; + @memcpy(d.buf[d.buf_len..][0..b_slice.len], b_slice); + d.buf_len += @intCast(u8, b_slice.len); // Md5 uses the bottom 64-bits for length padding d.total_len +%= b.len; @@ -87,7 +88,7 @@ pub const Md5 = struct { pub fn final(d: *Self, out: *[digest_length]u8) void { // The buffer here will never be completely full. - mem.set(u8, d.buf[d.buf_len..], 0); + @memset(d.buf[d.buf_len..], 0); // Append padding bits. d.buf[d.buf_len] = 0x80; @@ -96,7 +97,7 @@ pub const Md5 = struct { // > 448 mod 512 so need to add an extra round to wrap around. if (64 - d.buf_len < 8) { d.round(d.buf[0..]); - mem.set(u8, d.buf[0..], 0); + @memset(d.buf[0..], 0); } // Append message length. diff --git a/lib/std/crypto/modes.zig b/lib/std/crypto/modes.zig index 325d8c0ceb..eed803a899 100644 --- a/lib/std/crypto/modes.zig +++ b/lib/std/crypto/modes.zig @@ -38,8 +38,10 @@ pub fn ctr(comptime BlockCipher: anytype, block_cipher: BlockCipher, dst: []u8, if (i < src.len) { mem.writeInt(u128, &counter, counterInt, endian); var pad = [_]u8{0} ** block_length; - mem.copy(u8, &pad, src[i..]); + const src_slice = src[i..]; + @memcpy(pad[0..src_slice.len], src_slice); block_cipher.xor(&pad, &pad, counter); - mem.copy(u8, dst[i..], pad[0 .. src.len - i]); + const pad_slice = pad[0 .. src.len - i]; + @memcpy(dst[i..][0..pad_slice.len], pad_slice); } } diff --git a/lib/std/crypto/pbkdf2.zig b/lib/std/crypto/pbkdf2.zig index 6f9783df72..7c6df5444d 100644 --- a/lib/std/crypto/pbkdf2.zig +++ b/lib/std/crypto/pbkdf2.zig @@ -129,13 +129,13 @@ pub fn pbkdf2(dk: []u8, password: []const u8, salt: []const u8, rounds: u32, com const offset = block * h_len; const block_len = if (block != blocks_count - 1) h_len else r; const dk_block: []u8 = dk[offset..][0..block_len]; - mem.copy(u8, dk_block, prev_block[0..dk_block.len]); + @memcpy(dk_block, prev_block[0..dk_block.len]); var i: u32 = 1; while (i < rounds) : (i += 1) { // U_c = PRF (P, U_{c-1}) Prf.create(&new_block, prev_block[0..], password); - mem.copy(u8, prev_block[0..], new_block[0..]); + prev_block = new_block; // F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c for (dk_block, 0..) |_, j| { diff --git a/lib/std/crypto/pcurves/common.zig b/lib/std/crypto/pcurves/common.zig index 40f4a728c7..5d41bc190a 100644 --- a/lib/std/crypto/pcurves/common.zig +++ b/lib/std/crypto/pcurves/common.zig @@ -228,8 +228,8 @@ pub fn Field(comptime params: FieldParams) type { } if (iterations % 2 != 0) { fiat.divstep(&out1, &out2, &out3, &out4, &out5, d, f, g, v, r); - mem.copy(Word, &v, &out4); - mem.copy(Word, &f, &out2); + v = out4; + f = out2; } var v_opp: Limbs = undefined; fiat.opp(&v_opp, v); diff --git a/lib/std/crypto/pcurves/p256.zig b/lib/std/crypto/pcurves/p256.zig index a160d08016..0460515511 100644 --- a/lib/std/crypto/pcurves/p256.zig +++ b/lib/std/crypto/pcurves/p256.zig @@ -105,7 +105,7 @@ pub const P256 = struct { var out: [33]u8 = undefined; const xy = p.affineCoordinates(); out[0] = if (xy.y.isOdd()) 3 else 2; - mem.copy(u8, out[1..], &xy.x.toBytes(.Big)); + out[1..].* = xy.x.toBytes(.Big); return out; } @@ -114,8 +114,8 @@ pub const P256 = struct { var out: [65]u8 = undefined; out[0] = 4; const xy = p.affineCoordinates(); - mem.copy(u8, out[1..33], &xy.x.toBytes(.Big)); - mem.copy(u8, out[33..65], &xy.y.toBytes(.Big)); + out[1..33].* = xy.x.toBytes(.Big); + out[33..65].* = xy.y.toBytes(.Big); return out; } diff --git a/lib/std/crypto/pcurves/p256/scalar.zig b/lib/std/crypto/pcurves/p256/scalar.zig index ce019082ef..4e88d1fee7 100644 --- a/lib/std/crypto/pcurves/p256/scalar.zig +++ b/lib/std/crypto/pcurves/p256/scalar.zig @@ -192,20 +192,20 @@ const ScalarDouble = struct { var t = ScalarDouble{ .x1 = undefined, .x2 = Fe.zero, .x3 = Fe.zero }; { var b = [_]u8{0} ** encoded_length; - const len = math.min(s.len, 24); - mem.copy(u8, b[0..len], s[0..len]); + const len = @min(s.len, 24); + b[0..len].* = s[0..len].*; t.x1 = Fe.fromBytes(b, .Little) catch unreachable; } if (s_.len >= 24) { var b = [_]u8{0} ** encoded_length; - const len = math.min(s.len - 24, 24); - mem.copy(u8, b[0..len], s[24..][0..len]); + const len = @min(s.len - 24, 24); + b[0..len].* = s[24..][0..len].*; t.x2 = Fe.fromBytes(b, .Little) catch unreachable; } if (s_.len >= 48) { var b = [_]u8{0} ** encoded_length; const len = s.len - 48; - mem.copy(u8, b[0..len], s[48..][0..len]); + b[0..len].* = s[48..][0..len].*; t.x3 = Fe.fromBytes(b, .Little) catch unreachable; } return t; diff --git a/lib/std/crypto/pcurves/p384.zig b/lib/std/crypto/pcurves/p384.zig index 3aaf9e341f..6662fc0011 100644 --- a/lib/std/crypto/pcurves/p384.zig +++ b/lib/std/crypto/pcurves/p384.zig @@ -105,7 +105,7 @@ pub const P384 = struct { var out: [49]u8 = undefined; const xy = p.affineCoordinates(); out[0] = if (xy.y.isOdd()) 3 else 2; - mem.copy(u8, out[1..], &xy.x.toBytes(.Big)); + out[1..].* = xy.x.toBytes(.Big); return out; } @@ -114,8 +114,8 @@ pub const P384 = struct { var out: [97]u8 = undefined; out[0] = 4; const xy = p.affineCoordinates(); - mem.copy(u8, out[1..49], &xy.x.toBytes(.Big)); - mem.copy(u8, out[49..97], &xy.y.toBytes(.Big)); + out[1..49].* = xy.x.toBytes(.Big); + out[49..97].* = xy.y.toBytes(.Big); return out; } diff --git a/lib/std/crypto/pcurves/p384/scalar.zig b/lib/std/crypto/pcurves/p384/scalar.zig index ec71a52efa..ef257ab7ce 100644 --- a/lib/std/crypto/pcurves/p384/scalar.zig +++ b/lib/std/crypto/pcurves/p384/scalar.zig @@ -180,14 +180,14 @@ const ScalarDouble = struct { var t = ScalarDouble{ .x1 = undefined, .x2 = Fe.zero }; { var b = [_]u8{0} ** encoded_length; - const len = math.min(s.len, 32); - mem.copy(u8, b[0..len], s[0..len]); + const len = @min(s.len, 32); + b[0..len].* = s[0..len].*; t.x1 = Fe.fromBytes(b, .Little) catch unreachable; } if (s_.len >= 32) { var b = [_]u8{0} ** encoded_length; - const len = math.min(s.len - 32, 32); - mem.copy(u8, b[0..len], s[32..][0..len]); + const len = @min(s.len - 32, 32); + b[0..len].* = s[32..][0..len].*; t.x2 = Fe.fromBytes(b, .Little) catch unreachable; } return t; diff --git a/lib/std/crypto/pcurves/secp256k1.zig b/lib/std/crypto/pcurves/secp256k1.zig index 6998b0db82..753f929f44 100644 --- a/lib/std/crypto/pcurves/secp256k1.zig +++ b/lib/std/crypto/pcurves/secp256k1.zig @@ -158,7 +158,7 @@ pub const Secp256k1 = struct { var out: [33]u8 = undefined; const xy = p.affineCoordinates(); out[0] = if (xy.y.isOdd()) 3 else 2; - mem.copy(u8, out[1..], &xy.x.toBytes(.Big)); + out[1..].* = xy.x.toBytes(.Big); return out; } @@ -167,8 +167,8 @@ pub const Secp256k1 = struct { var out: [65]u8 = undefined; out[0] = 4; const xy = p.affineCoordinates(); - mem.copy(u8, out[1..33], &xy.x.toBytes(.Big)); - mem.copy(u8, out[33..65], &xy.y.toBytes(.Big)); + out[1..33].* = xy.x.toBytes(.Big); + out[33..65].* = xy.y.toBytes(.Big); return out; } diff --git a/lib/std/crypto/pcurves/secp256k1/scalar.zig b/lib/std/crypto/pcurves/secp256k1/scalar.zig index 0b7d6e952d..e0b5e053e3 100644 --- a/lib/std/crypto/pcurves/secp256k1/scalar.zig +++ b/lib/std/crypto/pcurves/secp256k1/scalar.zig @@ -192,20 +192,20 @@ const ScalarDouble = struct { var t = ScalarDouble{ .x1 = undefined, .x2 = Fe.zero, .x3 = Fe.zero }; { var b = [_]u8{0} ** encoded_length; - const len = math.min(s.len, 24); - mem.copy(u8, b[0..len], s[0..len]); + const len = @min(s.len, 24); + b[0..len].* = s[0..len].*; t.x1 = Fe.fromBytes(b, .Little) catch unreachable; } if (s_.len >= 24) { var b = [_]u8{0} ** encoded_length; - const len = math.min(s.len - 24, 24); - mem.copy(u8, b[0..len], s[24..][0..len]); + const len = @min(s.len - 24, 24); + b[0..len].* = s[24..][0..len].*; t.x2 = Fe.fromBytes(b, .Little) catch unreachable; } if (s_.len >= 48) { var b = [_]u8{0} ** encoded_length; const len = s.len - 48; - mem.copy(u8, b[0..len], s[48..][0..len]); + b[0..len].* = s[48..][0..len].*; t.x3 = Fe.fromBytes(b, .Little) catch unreachable; } return t; diff --git a/lib/std/crypto/phc_encoding.zig b/lib/std/crypto/phc_encoding.zig index 4b6965d040..cc0f10e395 100644 --- a/lib/std/crypto/phc_encoding.zig +++ b/lib/std/crypto/phc_encoding.zig @@ -35,7 +35,7 @@ pub fn BinValue(comptime max_len: usize) type { pub fn fromSlice(slice: []const u8) Error!Self { if (slice.len > capacity) return Error.NoSpaceLeft; var bin_value: Self = undefined; - mem.copy(u8, &bin_value.buf, slice); + @memcpy(bin_value.buf[0..slice.len], slice); bin_value.len = slice.len; return bin_value; } diff --git a/lib/std/crypto/salsa20.zig b/lib/std/crypto/salsa20.zig index 492b8e9988..7f57e6cecb 100644 --- a/lib/std/crypto/salsa20.zig +++ b/lib/std/crypto/salsa20.zig @@ -383,10 +383,10 @@ pub const XSalsa20Poly1305 = struct { debug.assert(c.len == m.len); const extended = extend(rounds, k, npub); var block0 = [_]u8{0} ** 64; - const mlen0 = math.min(32, m.len); - mem.copy(u8, block0[32..][0..mlen0], m[0..mlen0]); + const mlen0 = @min(32, m.len); + @memcpy(block0[32..][0..mlen0], m[0..mlen0]); Salsa20.xor(block0[0..], block0[0..], 0, extended.key, extended.nonce); - mem.copy(u8, c[0..mlen0], block0[32..][0..mlen0]); + @memcpy(c[0..mlen0], block0[32..][0..mlen0]); Salsa20.xor(c[mlen0..], m[mlen0..], 1, extended.key, extended.nonce); var mac = Poly1305.init(block0[0..32]); mac.update(ad); @@ -405,7 +405,7 @@ pub const XSalsa20Poly1305 = struct { const extended = extend(rounds, k, npub); var block0 = [_]u8{0} ** 64; const mlen0 = math.min(32, c.len); - mem.copy(u8, block0[32..][0..mlen0], c[0..mlen0]); + @memcpy(block0[32..][0..mlen0], c[0..mlen0]); Salsa20.xor(block0[0..], block0[0..], 0, extended.key, extended.nonce); var mac = Poly1305.init(block0[0..32]); mac.update(ad); @@ -420,7 +420,7 @@ pub const XSalsa20Poly1305 = struct { utils.secureZero(u8, &computedTag); return error.AuthenticationFailed; } - mem.copy(u8, m[0..mlen0], block0[32..][0..mlen0]); + @memcpy(m[0..mlen0], block0[32..][0..mlen0]); Salsa20.xor(m[mlen0..], c[mlen0..], 1, extended.key, extended.nonce); } }; @@ -533,7 +533,7 @@ pub const SealedBox = struct { debug.assert(c.len == m.len + seal_length); var ekp = try KeyPair.create(null); const nonce = createNonce(ekp.public_key, public_key); - mem.copy(u8, c[0..public_length], ekp.public_key[0..]); + c[0..public_length].* = ekp.public_key; try Box.seal(c[Box.public_length..], m, nonce, public_key, ekp.secret_key); utils.secureZero(u8, ekp.secret_key[0..]); } diff --git a/lib/std/crypto/scrypt.zig b/lib/std/crypto/scrypt.zig index dc73d974c7..077de3b510 100644 --- a/lib/std/crypto/scrypt.zig +++ b/lib/std/crypto/scrypt.zig @@ -27,7 +27,7 @@ const max_salt_len = 64; const max_hash_len = 64; fn blockCopy(dst: []align(16) u32, src: []align(16) const u32, n: usize) void { - mem.copy(u32, dst, src[0 .. n * 16]); + @memcpy(dst[0 .. n * 16], src[0 .. n * 16]); } fn blockXor(dst: []align(16) u32, src: []align(16) const u32, n: usize) void { @@ -242,7 +242,7 @@ const crypt_format = struct { pub fn fromSlice(slice: []const u8) EncodingError!Self { if (slice.len > capacity) return EncodingError.NoSpaceLeft; var bin_value: Self = undefined; - mem.copy(u8, &bin_value.buf, slice); + @memcpy(bin_value.buf[0..slice.len], slice); bin_value.len = slice.len; return bin_value; } @@ -314,7 +314,7 @@ const crypt_format = struct { fn serializeTo(params: anytype, out: anytype) !void { var header: [14]u8 = undefined; - mem.copy(u8, header[0..3], prefix); + header[0..3].* = prefix.*; Codec.intEncode(header[3..4], params.ln); Codec.intEncode(header[4..9], params.r); Codec.intEncode(header[9..14], params.p); diff --git a/lib/std/crypto/sha1.zig b/lib/std/crypto/sha1.zig index 4d11b04eb5..1f5f3eaae2 100644 --- a/lib/std/crypto/sha1.zig +++ b/lib/std/crypto/sha1.zig @@ -62,7 +62,7 @@ pub const Sha1 = struct { // Partial buffer exists from previous update. Copy into buffer then hash. if (d.buf_len != 0 and d.buf_len + b.len >= 64) { off += 64 - d.buf_len; - mem.copy(u8, d.buf[d.buf_len..], b[0..off]); + @memcpy(d.buf[d.buf_len..][0..off], b[0..off]); d.round(d.buf[0..]); d.buf_len = 0; @@ -74,7 +74,7 @@ pub const Sha1 = struct { } // Copy any remainder for next pass. - mem.copy(u8, d.buf[d.buf_len..], b[off..]); + @memcpy(d.buf[d.buf_len..][0 .. b.len - off], b[off..]); d.buf_len += @intCast(u8, b[off..].len); d.total_len += b.len; @@ -82,7 +82,7 @@ pub const Sha1 = struct { pub fn final(d: *Self, out: *[digest_length]u8) void { // The buffer here will never be completely full. - mem.set(u8, d.buf[d.buf_len..], 0); + @memset(d.buf[d.buf_len..], 0); // Append padding bits. d.buf[d.buf_len] = 0x80; @@ -91,7 +91,7 @@ pub const Sha1 = struct { // > 448 mod 512 so need to add an extra round to wrap around. if (64 - d.buf_len < 8) { d.round(d.buf[0..]); - mem.set(u8, d.buf[0..], 0); + @memset(d.buf[0..], 0); } // Append message length. diff --git a/lib/std/crypto/sha2.zig b/lib/std/crypto/sha2.zig index ad99079852..bd5a7cc5d4 100644 --- a/lib/std/crypto/sha2.zig +++ b/lib/std/crypto/sha2.zig @@ -118,7 +118,7 @@ fn Sha2x32(comptime params: Sha2Params32) type { // Partial buffer exists from previous update. Copy into buffer then hash. if (d.buf_len != 0 and d.buf_len + b.len >= 64) { off += 64 - d.buf_len; - mem.copy(u8, d.buf[d.buf_len..], b[0..off]); + @memcpy(d.buf[d.buf_len..][0..off], b[0..off]); d.round(&d.buf); d.buf_len = 0; @@ -130,7 +130,8 @@ fn Sha2x32(comptime params: Sha2Params32) type { } // Copy any remainder for next pass. - mem.copy(u8, d.buf[d.buf_len..], b[off..]); + const b_slice = b[off..]; + @memcpy(d.buf[d.buf_len..][0..b_slice.len], b_slice); d.buf_len += @intCast(u8, b[off..].len); d.total_len += b.len; @@ -143,7 +144,7 @@ fn Sha2x32(comptime params: Sha2Params32) type { pub fn final(d: *Self, out: *[digest_length]u8) void { // The buffer here will never be completely full. - mem.set(u8, d.buf[d.buf_len..], 0); + @memset(d.buf[d.buf_len..], 0); // Append padding bits. d.buf[d.buf_len] = 0x80; @@ -152,7 +153,7 @@ fn Sha2x32(comptime params: Sha2Params32) type { // > 448 mod 512 so need to add an extra round to wrap around. if (64 - d.buf_len < 8) { d.round(&d.buf); - mem.set(u8, d.buf[0..], 0); + @memset(d.buf[0..], 0); } // Append message length. @@ -609,7 +610,7 @@ fn Sha2x64(comptime params: Sha2Params64) type { // Partial buffer exists from previous update. Copy into buffer then hash. if (d.buf_len != 0 and d.buf_len + b.len >= 128) { off += 128 - d.buf_len; - mem.copy(u8, d.buf[d.buf_len..], b[0..off]); + @memcpy(d.buf[d.buf_len..][0..off], b[0..off]); d.round(&d.buf); d.buf_len = 0; @@ -621,7 +622,8 @@ fn Sha2x64(comptime params: Sha2Params64) type { } // Copy any remainder for next pass. - mem.copy(u8, d.buf[d.buf_len..], b[off..]); + const b_slice = b[off..]; + @memcpy(d.buf[d.buf_len..][0..b_slice.len], b_slice); d.buf_len += @intCast(u8, b[off..].len); d.total_len += b.len; @@ -634,7 +636,7 @@ fn Sha2x64(comptime params: Sha2Params64) type { pub fn final(d: *Self, out: *[digest_length]u8) void { // The buffer here will never be completely full. - mem.set(u8, d.buf[d.buf_len..], 0); + @memset(d.buf[d.buf_len..], 0); // Append padding bits. d.buf[d.buf_len] = 0x80; @@ -643,7 +645,7 @@ fn Sha2x64(comptime params: Sha2Params64) type { // > 896 mod 1024 so need to add an extra round to wrap around. if (128 - d.buf_len < 16) { d.round(d.buf[0..]); - mem.set(u8, d.buf[0..], 0); + @memset(d.buf[0..], 0); } // Append message length. diff --git a/lib/std/crypto/sha3.zig b/lib/std/crypto/sha3.zig index 1f48f87c53..23f9e65534 100644 --- a/lib/std/crypto/sha3.zig +++ b/lib/std/crypto/sha3.zig @@ -149,7 +149,7 @@ fn ShakeLike(comptime security_level: u11, comptime delim: u8, comptime rounds: const left = self.buf.len - self.offset; if (left > 0) { const n = math.min(left, out.len); - mem.copy(u8, out[0..n], self.buf[self.offset..][0..n]); + @memcpy(out[0..n], self.buf[self.offset..][0..n]); out = out[n..]; self.offset += n; if (out.len == 0) { @@ -164,7 +164,7 @@ fn ShakeLike(comptime security_level: u11, comptime delim: u8, comptime rounds: } if (out.len > 0) { self.st.squeeze(self.buf[0..]); - mem.copy(u8, out[0..], self.buf[0..out.len]); + @memcpy(out[0..], self.buf[0..out.len]); self.offset = out.len; } } diff --git a/lib/std/crypto/siphash.zig b/lib/std/crypto/siphash.zig index 16388439d1..d91485cdfa 100644 --- a/lib/std/crypto/siphash.zig +++ b/lib/std/crypto/siphash.zig @@ -98,7 +98,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round self.msg_len +%= @truncate(u8, b.len); var buf = [_]u8{0} ** 8; - mem.copy(u8, buf[0..], b[0..]); + @memcpy(buf[0..b.len], b); buf[7] = self.msg_len; self.round(buf); @@ -203,7 +203,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) if (self.buf_len != 0 and self.buf_len + b.len >= 8) { off += 8 - self.buf_len; - mem.copy(u8, self.buf[self.buf_len..], b[0..off]); + @memcpy(self.buf[self.buf_len..][0..off], b[0..off]); self.state.update(self.buf[0..]); self.buf_len = 0; } @@ -212,8 +212,9 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) const aligned_len = remain_len - (remain_len % 8); self.state.update(b[off .. off + aligned_len]); - mem.copy(u8, self.buf[self.buf_len..], b[off + aligned_len ..]); - self.buf_len += @intCast(u8, b[off + aligned_len ..].len); + const b_slice = b[off + aligned_len ..]; + @memcpy(self.buf[self.buf_len..][0..b_slice.len], b_slice); + self.buf_len += @intCast(u8, b_slice.len); } pub fn peek(self: Self) [mac_length]u8 { diff --git a/lib/std/crypto/tls.zig b/lib/std/crypto/tls.zig index 5b5d3dea67..8ae494edce 100644 --- a/lib/std/crypto/tls.zig +++ b/lib/std/crypto/tls.zig @@ -312,11 +312,11 @@ pub fn hkdfExpandLabel( buf[2] = @intCast(u8, tls13.len + label.len); buf[3..][0..tls13.len].* = tls13.*; var i: usize = 3 + tls13.len; - mem.copy(u8, buf[i..], label); + @memcpy(buf[i..][0..label.len], label); i += label.len; buf[i] = @intCast(u8, context.len); i += 1; - mem.copy(u8, buf[i..], context); + @memcpy(buf[i..][0..context.len], context); i += context.len; var result: [len]u8 = undefined; diff --git a/lib/std/crypto/tls/Client.zig b/lib/std/crypto/tls/Client.zig index 1d94dfce31..c886bc7276 100644 --- a/lib/std/crypto/tls/Client.zig +++ b/lib/std/crypto/tls/Client.zig @@ -685,7 +685,7 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In .application_cipher = app_cipher, .partially_read_buffer = undefined, }; - mem.copy(u8, &client.partially_read_buffer, leftover); + @memcpy(client.partially_read_buffer[0..leftover.len], leftover); return client; }, else => { @@ -809,7 +809,7 @@ fn prepareCiphertextRecord( .overhead_len = overhead_len, }; - mem.copy(u8, &cleartext_buf, bytes[bytes_i..][0..encrypted_content_len]); + @memcpy(cleartext_buf[0..encrypted_content_len], bytes[bytes_i..][0..encrypted_content_len]); cleartext_buf[encrypted_content_len] = @enumToInt(inner_content_type); bytes_i += encrypted_content_len; const ciphertext_len = encrypted_content_len + 1; @@ -1029,8 +1029,8 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) if (frag1.len < second_len) return finishRead2(c, first, frag1, vp.total); - mem.copy(u8, frag[0..in], first); - mem.copy(u8, frag[first.len..], frag1[0..second_len]); + @memcpy(frag[0..in], first); + @memcpy(frag[first.len..][0..second_len], frag1[0..second_len]); frag = frag[0..full_record_len]; frag1 = frag1[second_len..]; in = 0; @@ -1059,8 +1059,8 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) if (frag1.len < second_len) return finishRead2(c, first, frag1, vp.total); - mem.copy(u8, frag[0..in], first); - mem.copy(u8, frag[first.len..], frag1[0..second_len]); + @memcpy(frag[0..in], first); + @memcpy(frag[first.len..][0..second_len], frag1[0..second_len]); frag = frag[0..full_record_len]; frag1 = frag1[second_len..]; in = 0; @@ -1177,7 +1177,7 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) // We have already run out of room in iovecs. Continue // appending to `partially_read_buffer`. const dest = c.partially_read_buffer[c.partial_ciphertext_idx..]; - mem.copy(u8, dest, msg); + @memcpy(dest[0..msg.len], msg); c.partial_ciphertext_idx = @intCast(@TypeOf(c.partial_ciphertext_idx), c.partial_ciphertext_idx + msg.len); } else { const amt = vp.put(msg); @@ -1185,7 +1185,7 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) const rest = msg[amt..]; c.partial_cleartext_idx = 0; c.partial_ciphertext_idx = @intCast(@TypeOf(c.partial_ciphertext_idx), rest.len); - mem.copy(u8, &c.partially_read_buffer, rest); + @memcpy(c.partially_read_buffer[0..rest.len], rest); } } } else { @@ -1213,12 +1213,12 @@ fn finishRead(c: *Client, frag: []const u8, in: usize, out: usize) usize { if (c.partial_ciphertext_idx > c.partial_cleartext_idx) { // There is cleartext at the beginning already which we need to preserve. c.partial_ciphertext_end = @intCast(@TypeOf(c.partial_ciphertext_end), c.partial_ciphertext_idx + saved_buf.len); - mem.copy(u8, c.partially_read_buffer[c.partial_ciphertext_idx..], saved_buf); + @memcpy(c.partially_read_buffer[c.partial_ciphertext_idx..][0..saved_buf.len], saved_buf); } else { c.partial_cleartext_idx = 0; c.partial_ciphertext_idx = 0; c.partial_ciphertext_end = @intCast(@TypeOf(c.partial_ciphertext_end), saved_buf.len); - mem.copy(u8, &c.partially_read_buffer, saved_buf); + @memcpy(c.partially_read_buffer[0..saved_buf.len], saved_buf); } return out; } @@ -1227,14 +1227,14 @@ fn finishRead2(c: *Client, first: []const u8, frag1: []const u8, out: usize) usi if (c.partial_ciphertext_idx > c.partial_cleartext_idx) { // There is cleartext at the beginning already which we need to preserve. c.partial_ciphertext_end = @intCast(@TypeOf(c.partial_ciphertext_end), c.partial_ciphertext_idx + first.len + frag1.len); - mem.copy(u8, c.partially_read_buffer[c.partial_ciphertext_idx..], first); - mem.copy(u8, c.partially_read_buffer[c.partial_ciphertext_idx + first.len ..], frag1); + @memcpy(c.partially_read_buffer[c.partial_ciphertext_idx..][0..first.len], first); + @memcpy(c.partially_read_buffer[c.partial_ciphertext_idx + first.len ..][0..frag1.len], frag1); } else { c.partial_cleartext_idx = 0; c.partial_ciphertext_idx = 0; c.partial_ciphertext_end = @intCast(@TypeOf(c.partial_ciphertext_end), first.len + frag1.len); - mem.copy(u8, &c.partially_read_buffer, first); - mem.copy(u8, c.partially_read_buffer[first.len..], frag1); + @memcpy(c.partially_read_buffer[0..first.len], first); + @memcpy(c.partially_read_buffer[first.len..][0..frag1.len], frag1); } return out; } @@ -1282,7 +1282,7 @@ const VecPut = struct { const v = vp.iovecs[vp.idx]; const dest = v.iov_base[vp.off..v.iov_len]; const src = bytes[bytes_i..][0..@min(dest.len, bytes.len - bytes_i)]; - mem.copy(u8, dest, src); + @memcpy(dest[0..src.len], src); bytes_i += src.len; vp.off += src.len; if (vp.off >= v.iov_len) { diff --git a/lib/std/crypto/utils.zig b/lib/std/crypto/utils.zig index 38dc236455..14a235e418 100644 --- a/lib/std/crypto/utils.zig +++ b/lib/std/crypto/utils.zig @@ -134,12 +134,8 @@ pub fn timingSafeSub(comptime T: type, a: []const T, b: []const T, result: []T, /// Sets a slice to zeroes. /// Prevents the store from being optimized out. -pub fn secureZero(comptime T: type, s: []T) void { - // TODO: implement `@memset` for non-byte-sized element type in the llvm backend - //@memset(@as([]volatile T, s), 0); - const ptr = @ptrCast([*]volatile u8, s.ptr); - const length = s.len * @sizeOf(T); - @memset(ptr[0..length], 0); +pub inline fn secureZero(comptime T: type, s: []T) void { + @memset(@as([]volatile T, s), 0); } test "crypto.utils.timingSafeEql" { @@ -148,7 +144,7 @@ test "crypto.utils.timingSafeEql" { random.bytes(a[0..]); random.bytes(b[0..]); try testing.expect(!timingSafeEql([100]u8, a, b)); - mem.copy(u8, a[0..], b[0..]); + a = b; try testing.expect(timingSafeEql([100]u8, a, b)); } @@ -201,7 +197,7 @@ test "crypto.utils.secureZero" { var a = [_]u8{0xfe} ** 8; var b = [_]u8{0xfe} ** 8; - mem.set(u8, a[0..], 0); + @memset(a[0..], 0); secureZero(u8, b[0..]); try testing.expectEqualSlices(u8, a[0..], b[0..]); diff --git a/lib/std/cstr.zig b/lib/std/cstr.zig index 52524c5084..8a203e5c8e 100644 --- a/lib/std/cstr.zig +++ b/lib/std/cstr.zig @@ -34,7 +34,7 @@ fn testCStrFnsImpl() !void { /// Caller owns the returned memory. pub fn addNullByte(allocator: mem.Allocator, slice: []const u8) ![:0]u8 { const result = try allocator.alloc(u8, slice.len + 1); - mem.copy(u8, result, slice); + @memcpy(result[0..slice.len], slice); result[slice.len] = 0; return result[0..slice.len :0]; } @@ -78,7 +78,7 @@ pub const NullTerminated2DArray = struct { for (slice) |inner| { index_buf[i] = buf.ptr + write_index; i += 1; - mem.copy(u8, buf[write_index..], inner); + @memcpy(buf[write_index..][0..inner.len], inner); write_index += inner.len; buf[write_index] = 0; write_index += 1; diff --git a/lib/std/debug.zig b/lib/std/debug.zig index fc8b55a888..ecc1a9f0cf 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -309,8 +309,8 @@ pub fn panicExtra( // error being part of the @panic stack trace (but that error should // only happen rarely) const msg = std.fmt.bufPrint(buf[0..size], format, args) catch |err| switch (err) { - std.fmt.BufPrintError.NoSpaceLeft => blk: { - std.mem.copy(u8, buf[size..], trunc_msg); + error.NoSpaceLeft => blk: { + @memcpy(buf[size..], trunc_msg); break :blk &buf; }, }; diff --git a/lib/std/dynamic_library.zig b/lib/std/dynamic_library.zig index 099ba63204..59ad7429cf 100644 --- a/lib/std/dynamic_library.zig +++ b/lib/std/dynamic_library.zig @@ -210,7 +210,7 @@ pub const ElfDynLib = struct { -1, 0, ); - mem.copy(u8, sect_mem, file_bytes[0..ph.p_filesz]); + @memcpy(sect_mem[0..ph.p_filesz], file_bytes[0..ph.p_filesz]); } }, else => {}, diff --git a/lib/std/enums.zig b/lib/std/enums.zig index a915d40f49..8e67c358b7 100644 --- a/lib/std/enums.zig +++ b/lib/std/enums.zig @@ -275,7 +275,7 @@ pub fn EnumMap(comptime E: type, comptime V: type) type { .bits = Self.BitSet.initFull(), .values = undefined, }; - std.mem.set(V, &result.values, value); + @memset(&result.values, value); return result; } /// Initializes a full mapping with supplied values. @@ -1175,7 +1175,7 @@ pub fn IndexedArray(comptime I: type, comptime V: type, comptime Ext: fn (type) pub fn initFill(v: Value) Self { var self: Self = undefined; - std.mem.set(Value, &self.values, v); + @memset(&self.values, v); return self; } diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index a6736e41a7..0861e19d14 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -86,19 +86,17 @@ pub fn LinearFifo( pub fn realign(self: *Self) void { if (self.buf.len - self.head >= self.count) { - // this copy overlaps - mem.copy(T, self.buf[0..self.count], self.buf[self.head..][0..self.count]); + mem.copyForwards(T, self.buf[0..self.count], self.buf[self.head..][0..self.count]); self.head = 0; } else { var tmp: [mem.page_size / 2 / @sizeOf(T)]T = undefined; while (self.head != 0) { - const n = math.min(self.head, tmp.len); + const n = @min(self.head, tmp.len); const m = self.buf.len - n; - mem.copy(T, tmp[0..n], self.buf[0..n]); - // this middle copy overlaps; the others here don't - mem.copy(T, self.buf[0..m], self.buf[n..][0..m]); - mem.copy(T, self.buf[m..], tmp[0..n]); + @memcpy(tmp[0..n], self.buf[0..n]); + mem.copyForwards(T, self.buf[0..m], self.buf[n..][0..m]); + @memcpy(self.buf[m..][0..n], tmp[0..n]); self.head -= n; } } @@ -223,8 +221,8 @@ pub fn LinearFifo( while (dst_left.len > 0) { const slice = self.readableSlice(0); if (slice.len == 0) break; - const n = math.min(slice.len, dst_left.len); - mem.copy(T, dst_left, slice[0..n]); + const n = @min(slice.len, dst_left.len); + @memcpy(dst_left[0..n], slice[0..n]); self.discard(n); dst_left = dst_left[n..]; } @@ -289,8 +287,8 @@ pub fn LinearFifo( while (src_left.len > 0) { const writable_slice = self.writableSlice(0); assert(writable_slice.len != 0); - const n = math.min(writable_slice.len, src_left.len); - mem.copy(T, writable_slice, src_left[0..n]); + const n = @min(writable_slice.len, src_left.len); + @memcpy(writable_slice[0..n], src_left[0..n]); self.update(n); src_left = src_left[n..]; } @@ -354,11 +352,11 @@ pub fn LinearFifo( const slice = self.readableSliceMut(0); if (src.len < slice.len) { - mem.copy(T, slice, src); + @memcpy(slice[0..src.len], src); } else { - mem.copy(T, slice, src[0..slice.len]); + @memcpy(slice, src[0..slice.len]); const slice2 = self.readableSliceMut(slice.len); - mem.copy(T, slice2, src[slice.len..]); + @memcpy(slice2[0 .. src.len - slice.len], src[slice.len..]); } } diff --git a/lib/std/fmt/errol.zig b/lib/std/fmt/errol.zig index 1c67175ede..9ad5d218a9 100644 --- a/lib/std/fmt/errol.zig +++ b/lib/std/fmt/errol.zig @@ -84,8 +84,8 @@ pub fn errol3(value: f64, buffer: []u8) FloatDecimal { const i = tableLowerBound(bits); if (i < enum3.len and enum3[i] == bits) { const data = enum3_data[i]; - const digits = buffer[1 .. data.str.len + 1]; - mem.copy(u8, digits, data.str); + const digits = buffer[1..][0..data.str.len]; + @memcpy(digits, data.str); return FloatDecimal{ .digits = digits, .exp = data.exp, diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 290eb151f7..9f64387bd8 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -106,7 +106,7 @@ pub fn atomicSymLink(allocator: Allocator, existing_path: []const u8, new_path: var rand_buf: [AtomicFile.RANDOM_BYTES]u8 = undefined; const tmp_path = try allocator.alloc(u8, dirname.len + 1 + base64_encoder.calcSize(rand_buf.len)); defer allocator.free(tmp_path); - mem.copy(u8, tmp_path[0..], dirname); + @memcpy(tmp_path[0..dirname.len], dirname); tmp_path[dirname.len] = path.sep; while (true) { crypto.random.bytes(rand_buf[0..]); @@ -1541,9 +1541,9 @@ pub const Dir = struct { return error.NameTooLong; } - mem.copy(u8, out_buffer, out_path); - - return out_buffer[0..out_path.len]; + const result = out_buffer[0..out_path.len]; + @memcpy(result, out_path); + return result; } /// Windows-only. Same as `Dir.realpath` except `pathname` is WTF16 encoded. @@ -1593,9 +1593,9 @@ pub const Dir = struct { return error.NameTooLong; } - mem.copy(u8, out_buffer, out_path); - - return out_buffer[0..out_path.len]; + const result = out_buffer[0..out_path.len]; + @memcpy(result, out_path); + return result; } /// Same as `Dir.realpath` except caller must free the returned memory. @@ -2346,8 +2346,9 @@ pub const Dir = struct { if (cleanup_dir_parent) |*d| d.close(); cleanup_dir_parent = iterable_dir; iterable_dir = new_dir; - mem.copy(u8, &dir_name_buf, entry.name); - dir_name = dir_name_buf[0..entry.name.len]; + const result = dir_name_buf[0..entry.name.len]; + @memcpy(result, entry.name); + dir_name = result; continue :scan_dir; } else { if (iterable_dir.dir.deleteFile(entry.name)) { @@ -2974,8 +2975,9 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 { var real_path_buf: [MAX_PATH_BYTES]u8 = undefined; const real_path = try std.os.realpathZ(&symlink_path_buf, &real_path_buf); if (real_path.len > out_buffer.len) return error.NameTooLong; - std.mem.copy(u8, out_buffer, real_path); - return out_buffer[0..real_path.len]; + const result = out_buffer[0..real_path.len]; + @memcpy(result, real_path); + return result; } switch (builtin.os.tag) { .linux => return os.readlinkZ("/proc/self/exe", out_buffer), @@ -3014,8 +3016,9 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 { const real_path = try os.realpathZ(os.argv[0], &real_path_buf); if (real_path.len > out_buffer.len) return error.NameTooLong; - mem.copy(u8, out_buffer, real_path); - return out_buffer[0..real_path.len]; + const result = out_buffer[0..real_path.len]; + @memcpy(result, real_path); + return result; } else if (argv0.len != 0) { // argv[0] is not empty (and not a path): search it inside PATH const PATH = std.os.getenvZ("PATH") orelse return error.FileNotFound; @@ -3032,8 +3035,9 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 { // found a file, and hope it is the right file if (real_path.len > out_buffer.len) return error.NameTooLong; - mem.copy(u8, out_buffer, real_path); - return out_buffer[0..real_path.len]; + const result = out_buffer[0..real_path.len]; + @memcpy(result, real_path); + return result; } else |_| continue; } } diff --git a/lib/std/fs/path.zig b/lib/std/fs/path.zig index beb386f92c..4f3e05cd59 100644 --- a/lib/std/fs/path.zig +++ b/lib/std/fs/path.zig @@ -79,7 +79,7 @@ fn joinSepMaybeZ(allocator: Allocator, separator: u8, comptime sepPredicate: fn const buf = try allocator.alloc(u8, total_len); errdefer allocator.free(buf); - mem.copy(u8, buf, paths[first_path_index]); + @memcpy(buf[0..paths[first_path_index].len], paths[first_path_index]); var buf_index: usize = paths[first_path_index].len; var prev_path = paths[first_path_index]; assert(prev_path.len > 0); @@ -94,7 +94,7 @@ fn joinSepMaybeZ(allocator: Allocator, separator: u8, comptime sepPredicate: fn buf_index += 1; } const adjusted_path = if (prev_sep and this_sep) this_path[1..] else this_path; - mem.copy(u8, buf[buf_index..], adjusted_path); + @memcpy(buf[buf_index..][0..adjusted_path.len], adjusted_path); buf_index += adjusted_path.len; prev_path = this_path; } @@ -631,7 +631,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 { real_result[i..][0..3].* = "..\\".*; i += 3; } - mem.copy(u8, real_result[i..], result.items); + @memcpy(real_result[i..][0..result.items.len], result.items); return real_result; } } @@ -710,7 +710,7 @@ pub fn resolvePosix(allocator: Allocator, paths: []const []const u8) Allocator.E real_result[i..][0..3].* = "../".*; i += 3; } - mem.copy(u8, real_result[i..], result.items); + @memcpy(real_result[i..][0..result.items.len], result.items); return real_result; } } @@ -1106,7 +1106,7 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ! while (rest_it.next()) |to_component| { result[result_index] = '\\'; result_index += 1; - mem.copy(u8, result[result_index..], to_component); + @memcpy(result[result_index..][0..to_component.len], to_component); result_index += to_component.len; } @@ -1151,7 +1151,7 @@ pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![] return allocator.realloc(result, result_index - 1); } - mem.copy(u8, result[result_index..], to_rest); + @memcpy(result[result_index..][0..to_rest.len], to_rest); return result; } diff --git a/lib/std/hash/cityhash.zig b/lib/std/hash/cityhash.zig index 23b3ce02da..d0884b135f 100644 --- a/lib/std/hash/cityhash.zig +++ b/lib/std/hash/cityhash.zig @@ -348,8 +348,8 @@ fn SMHasherTest(comptime hash_fn: anytype) u32 { var key: [256]u8 = undefined; var hashes_bytes: [256 * @sizeOf(HashResult)]u8 = undefined; - std.mem.set(u8, &key, 0); - std.mem.set(u8, &hashes_bytes, 0); + @memset(&key, 0); + @memset(&hashes_bytes, 0); var i: u32 = 0; while (i < 256) : (i += 1) { diff --git a/lib/std/hash/wyhash.zig b/lib/std/hash/wyhash.zig index 2f30c26b75..772395eab9 100644 --- a/lib/std/hash/wyhash.zig +++ b/lib/std/hash/wyhash.zig @@ -147,7 +147,7 @@ pub const Wyhash = struct { if (self.buf_len != 0 and self.buf_len + b.len >= 32) { off += 32 - self.buf_len; - mem.copy(u8, self.buf[self.buf_len..], b[0..off]); + @memcpy(self.buf[self.buf_len..][0..off], b[0..off]); self.state.update(self.buf[0..]); self.buf_len = 0; } @@ -156,7 +156,8 @@ pub const Wyhash = struct { const aligned_len = remain_len - (remain_len % 32); self.state.update(b[off .. off + aligned_len]); - mem.copy(u8, self.buf[self.buf_len..], b[off + aligned_len ..]); + const src = b[off + aligned_len ..]; + @memcpy(self.buf[self.buf_len..][0..src.len], src); self.buf_len += @intCast(u8, b[off + aligned_len ..].len); } diff --git a/lib/std/hash/xxhash.zig b/lib/std/hash/xxhash.zig index bf4877e029..88db1dc1bd 100644 --- a/lib/std/hash/xxhash.zig +++ b/lib/std/hash/xxhash.zig @@ -36,7 +36,7 @@ pub const XxHash64 = struct { pub fn update(self: *XxHash64, input: []const u8) void { if (input.len < 32 - self.buf_len) { - mem.copy(u8, self.buf[self.buf_len..], input); + @memcpy(self.buf[self.buf_len..][0..input.len], input); self.buf_len += input.len; return; } @@ -45,7 +45,7 @@ pub const XxHash64 = struct { if (self.buf_len > 0) { i = 32 - self.buf_len; - mem.copy(u8, self.buf[self.buf_len..], input[0..i]); + @memcpy(self.buf[self.buf_len..][0..i], input[0..i]); self.processStripe(&self.buf); self.buf_len = 0; } @@ -55,7 +55,7 @@ pub const XxHash64 = struct { } const remaining_bytes = input[i..]; - mem.copy(u8, &self.buf, remaining_bytes); + @memcpy(self.buf[0..remaining_bytes.len], remaining_bytes); self.buf_len = remaining_bytes.len; } @@ -165,7 +165,7 @@ pub const XxHash32 = struct { pub fn update(self: *XxHash32, input: []const u8) void { if (input.len < 16 - self.buf_len) { - mem.copy(u8, self.buf[self.buf_len..], input); + @memcpy(self.buf[self.buf_len..][0..input.len], input); self.buf_len += input.len; return; } @@ -174,7 +174,7 @@ pub const XxHash32 = struct { if (self.buf_len > 0) { i = 16 - self.buf_len; - mem.copy(u8, self.buf[self.buf_len..], input[0..i]); + @memcpy(self.buf[self.buf_len..][0..i], input[0..i]); self.processStripe(&self.buf); self.buf_len = 0; } @@ -184,7 +184,7 @@ pub const XxHash32 = struct { } const remaining_bytes = input[i..]; - mem.copy(u8, &self.buf, remaining_bytes); + @memcpy(self.buf[0..remaining_bytes.len], remaining_bytes); self.buf_len = remaining_bytes.len; } diff --git a/lib/std/heap/WasmAllocator.zig b/lib/std/heap/WasmAllocator.zig index 3b980ac9d3..efec116db4 100644 --- a/lib/std/heap/WasmAllocator.zig +++ b/lib/std/heap/WasmAllocator.zig @@ -230,7 +230,7 @@ test "shrink" { var slice = try test_ally.alloc(u8, 20); defer test_ally.free(slice); - mem.set(u8, slice, 0x11); + @memset(slice, 0x11); try std.testing.expect(test_ally.resize(slice, 17)); slice = slice[0..17]; diff --git a/lib/std/heap/WasmPageAllocator.zig b/lib/std/heap/WasmPageAllocator.zig index 4084eaa88e..1370af022c 100644 --- a/lib/std/heap/WasmPageAllocator.zig +++ b/lib/std/heap/WasmPageAllocator.zig @@ -153,7 +153,7 @@ fn freePages(start: usize, end: usize) void { extended.data = @intToPtr([*]u128, new_end * mem.page_size)[0 .. mem.page_size / @sizeOf(u128)]; // Since this is the first page being freed and we consume it, assume *nothing* is free. - mem.set(u128, extended.data, PageStatus.none_free); + @memset(extended.data, PageStatus.none_free); } const clamped_start = @max(extendedOffset(), start); extended.recycle(clamped_start - extendedOffset(), new_end - clamped_start); diff --git a/lib/std/heap/general_purpose_allocator.zig b/lib/std/heap/general_purpose_allocator.zig index e7ebe75812..de7bc7c423 100644 --- a/lib/std/heap/general_purpose_allocator.zig +++ b/lib/std/heap/general_purpose_allocator.zig @@ -448,7 +448,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type { fn collectStackTrace(first_trace_addr: usize, addresses: *[stack_n]usize) void { if (stack_n == 0) return; - mem.set(usize, addresses, 0); + @memset(addresses, 0); var stack_trace = StackTrace{ .instruction_addresses = addresses, .index = 0, @@ -1113,7 +1113,7 @@ test "shrink" { var slice = try allocator.alloc(u8, 20); defer allocator.free(slice); - mem.set(u8, slice, 0x11); + @memset(slice, 0x11); try std.testing.expect(allocator.resize(slice, 17)); slice = slice[0..17]; diff --git a/lib/std/http/Client.zig b/lib/std/http/Client.zig index e1f28e7921..42ef766bd3 100644 --- a/lib/std/http/Client.zig +++ b/lib/std/http/Client.zig @@ -284,7 +284,7 @@ pub const BufferedConnection = struct { if (available > 0) { const can_read = @truncate(u16, @min(available, left)); - std.mem.copy(u8, buffer[out_index..], bconn.buf[bconn.start..][0..can_read]); + @memcpy(buffer[out_index..][0..can_read], bconn.buf[bconn.start..][0..can_read]); out_index += can_read; bconn.start += can_read; diff --git a/lib/std/http/Headers.zig b/lib/std/http/Headers.zig index 0322c32481..376fd60b61 100644 --- a/lib/std/http/Headers.zig +++ b/lib/std/http/Headers.zig @@ -38,7 +38,8 @@ pub const Field = struct { pub fn modify(entry: *Field, allocator: Allocator, new_value: []const u8) !void { if (entry.value.len <= new_value.len) { - std.mem.copy(u8, @constCast(entry.value), new_value); + // TODO: eliminate this use of `@constCast`. + @memcpy(@constCast(entry.value)[0..new_value.len], new_value); } else { allocator.free(entry.value); diff --git a/lib/std/http/Server.zig b/lib/std/http/Server.zig index 66adb1bb96..c7f2a86c27 100644 --- a/lib/std/http/Server.zig +++ b/lib/std/http/Server.zig @@ -128,7 +128,7 @@ pub const BufferedConnection = struct { if (available > 0) { const can_read = @truncate(u16, @min(available, left)); - std.mem.copy(u8, buffer[out_index..], bconn.buf[bconn.start..][0..can_read]); + @memcpy(buffer[out_index..][0..can_read], bconn.buf[bconn.start..][0..can_read]); out_index += can_read; bconn.start += can_read; diff --git a/lib/std/http/protocol.zig b/lib/std/http/protocol.zig index 962376ce82..c6bdd76272 100644 --- a/lib/std/http/protocol.zig +++ b/lib/std/http/protocol.zig @@ -654,7 +654,7 @@ const MockBufferedConnection = struct { if (available > 0) { const can_read = @truncate(u16, @min(available, left)); - std.mem.copy(u8, buffer[out_index..], bconn.buf[bconn.start..][0..can_read]); + @memcpy(buffer[out_index..][0..can_read], bconn.buf[bconn.start..][0..can_read]); out_index += can_read; bconn.start += can_read; diff --git a/lib/std/io/buffered_reader.zig b/lib/std/io/buffered_reader.zig index 2f6677c6a5..efc1d78cc4 100644 --- a/lib/std/io/buffered_reader.zig +++ b/lib/std/io/buffered_reader.zig @@ -20,8 +20,8 @@ pub fn BufferedReader(comptime buffer_size: usize, comptime ReaderType: type) ty var dest_index: usize = 0; while (dest_index < dest.len) { - const written = std.math.min(dest.len - dest_index, self.end - self.start); - std.mem.copy(u8, dest[dest_index..], self.buf[self.start .. self.start + written]); + const written = @min(dest.len - dest_index, self.end - self.start); + @memcpy(dest[dest_index..][0..written], self.buf[self.start..][0..written]); if (written == 0) { // buf empty, fill it const n = try self.unbuffered_reader.read(self.buf[0..]); @@ -115,11 +115,8 @@ test "io.BufferedReader Block" { } fn read(self: *Self, dest: []u8) Error!usize { - if (self.curr_read >= self.reads_allowed) { - return 0; - } - std.debug.assert(dest.len >= self.block.len); - std.mem.copy(u8, dest, self.block); + if (self.curr_read >= self.reads_allowed) return 0; + @memcpy(dest[0..self.block.len], self.block); self.curr_read += 1; return self.block.len; diff --git a/lib/std/io/buffered_writer.zig b/lib/std/io/buffered_writer.zig index 497a1810a7..906d6cce49 100644 --- a/lib/std/io/buffered_writer.zig +++ b/lib/std/io/buffered_writer.zig @@ -30,8 +30,9 @@ pub fn BufferedWriter(comptime buffer_size: usize, comptime WriterType: type) ty return self.unbuffered_writer.write(bytes); } - mem.copy(u8, self.buf[self.end..], bytes); - self.end += bytes.len; + const new_end = self.end + bytes.len; + @memcpy(self.buf[self.end..new_end], bytes); + self.end = new_end; return bytes.len; } }; diff --git a/lib/std/io/fixed_buffer_stream.zig b/lib/std/io/fixed_buffer_stream.zig index 2fcb260c72..c170dd1f74 100644 --- a/lib/std/io/fixed_buffer_stream.zig +++ b/lib/std/io/fixed_buffer_stream.zig @@ -45,10 +45,10 @@ pub fn FixedBufferStream(comptime Buffer: type) type { } pub fn read(self: *Self, dest: []u8) ReadError!usize { - const size = std.math.min(dest.len, self.buffer.len - self.pos); + const size = @min(dest.len, self.buffer.len - self.pos); const end = self.pos + size; - mem.copy(u8, dest[0..size], self.buffer[self.pos..end]); + @memcpy(dest[0..size], self.buffer[self.pos..end]); self.pos = end; return size; @@ -67,7 +67,7 @@ pub fn FixedBufferStream(comptime Buffer: type) type { else self.buffer.len - self.pos; - mem.copy(u8, self.buffer[self.pos .. self.pos + n], bytes[0..n]); + @memcpy(self.buffer[self.pos..][0..n], bytes[0..n]); self.pos += n; if (n == 0) return error.NoSpaceLeft; diff --git a/lib/std/io/writer.zig b/lib/std/io/writer.zig index 3160c37ff2..f4b2643e77 100644 --- a/lib/std/io/writer.zig +++ b/lib/std/io/writer.zig @@ -35,7 +35,7 @@ pub fn Writer( pub fn writeByteNTimes(self: Self, byte: u8, n: usize) Error!void { var bytes: [256]u8 = undefined; - mem.set(u8, bytes[0..], byte); + @memset(bytes[0..], byte); var remaining: usize = n; while (remaining > 0) { diff --git a/lib/std/json.zig b/lib/std/json.zig index ca7fb5ad20..432f4e6911 100644 --- a/lib/std/json.zig +++ b/lib/std/json.zig @@ -1667,7 +1667,7 @@ fn parseInternal( const source_slice = stringToken.slice(tokens.slice, tokens.i - 1); if (r.len != stringToken.decodedLength()) return error.LengthMismatch; switch (stringToken.escapes) { - .None => mem.copy(u8, &r, source_slice), + .None => @memcpy(r[0..source_slice.len], source_slice), .Some => try unescapeValidString(&r, source_slice), } return r; @@ -1733,7 +1733,7 @@ fn parseInternal( try allocator.alloc(u8, len); errdefer allocator.free(output); switch (stringToken.escapes) { - .None => mem.copy(u8, output, source_slice), + .None => @memcpy(output[0..source_slice.len], source_slice), .Some => try unescapeValidString(output, source_slice), } diff --git a/lib/std/json/test.zig b/lib/std/json/test.zig index 452341535b..7b5e7e814d 100644 --- a/lib/std/json/test.zig +++ b/lib/std/json/test.zig @@ -2811,7 +2811,7 @@ test "json.serialize issue #5959" { // StreamingParser has multiple internal fields set to undefined. This causes issues when using // expectEqual so these are zeroed. We are testing for equality here only because this is a // known small test reproduction which hits the relevant LLVM issue. - std.mem.set(u8, @ptrCast([*]u8, &parser)[0..@sizeOf(StreamingParser)], 0); + @memset(@ptrCast([*]u8, &parser)[0..@sizeOf(StreamingParser)], 0); try std.testing.expectEqual(parser, parser); } diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig index f97a763d95..2406d669ec 100644 --- a/lib/std/math/big/int.zig +++ b/lib/std/math/big/int.zig @@ -176,7 +176,7 @@ pub const Mutable = struct { /// Asserts the value fits in the limbs buffer. pub fn copy(self: *Mutable, other: Const) void { if (self.limbs.ptr != other.limbs.ptr) { - mem.copy(Limb, self.limbs[0..], other.limbs[0..other.limbs.len]); + @memcpy(self.limbs[0..other.limbs.len], other.limbs[0..other.limbs.len]); } self.positive = other.positive; self.len = other.limbs.len; @@ -199,7 +199,7 @@ pub const Mutable = struct { /// can be modified separately from the original. /// Asserts that limbs is big enough to store the value. pub fn clone(other: Mutable, limbs: []Limb) Mutable { - mem.copy(Limb, limbs, other.limbs[0..other.len]); + @memcpy(limbs[0..other.len], other.limbs[0..other.len]); return .{ .limbs = limbs, .len = other.len, @@ -344,7 +344,7 @@ pub const Mutable = struct { .min => { // Negative bound, signed = -0x80. r.len = req_limbs; - mem.set(Limb, r.limbs[0 .. r.len - 1], 0); + @memset(r.limbs[0 .. r.len - 1], 0); r.limbs[r.len - 1] = signmask; r.positive = false; }, @@ -363,7 +363,7 @@ pub const Mutable = struct { const new_mask = (new_signmask << 1) -% 1; // 0b0..001..1 where the rightmost 0 is the sign bit. r.len = new_req_limbs; - std.mem.set(Limb, r.limbs[0 .. r.len - 1], maxInt(Limb)); + @memset(r.limbs[0 .. r.len - 1], maxInt(Limb)); r.limbs[r.len - 1] = new_mask; } }, @@ -376,7 +376,7 @@ pub const Mutable = struct { .max => { // Max bound, unsigned = 0xFF r.len = req_limbs; - std.mem.set(Limb, r.limbs[0 .. r.len - 1], maxInt(Limb)); + @memset(r.limbs[0 .. r.len - 1], maxInt(Limb)); r.limbs[r.len - 1] = mask; }, }, @@ -489,7 +489,7 @@ pub const Mutable = struct { if (msl < req_limbs) { r.limbs[msl] = 1; r.len = req_limbs; - mem.set(Limb, r.limbs[msl + 1 .. req_limbs], 0); + @memset(r.limbs[msl + 1 .. req_limbs], 0); } else { carry_truncated = true; } @@ -637,14 +637,14 @@ pub const Mutable = struct { const a_copy = if (rma.limbs.ptr == a.limbs.ptr) blk: { const start = buf_index; - mem.copy(Limb, limbs_buffer[buf_index..], a.limbs); + @memcpy(limbs_buffer[buf_index..][0..a.limbs.len], a.limbs); buf_index += a.limbs.len; break :blk a.toMutable(limbs_buffer[start..buf_index]).toConst(); } else a; const b_copy = if (rma.limbs.ptr == b.limbs.ptr) blk: { const start = buf_index; - mem.copy(Limb, limbs_buffer[buf_index..], b.limbs); + @memcpy(limbs_buffer[buf_index..][0..b.limbs.len], b.limbs); buf_index += b.limbs.len; break :blk b.toMutable(limbs_buffer[start..buf_index]).toConst(); } else b; @@ -676,7 +676,7 @@ pub const Mutable = struct { } } - mem.set(Limb, rma.limbs[0 .. a.limbs.len + b.limbs.len], 0); + @memset(rma.limbs[0 .. a.limbs.len + b.limbs.len], 0); llmulacc(.add, allocator, rma.limbs, a.limbs, b.limbs); @@ -708,7 +708,7 @@ pub const Mutable = struct { const a_copy = if (rma.limbs.ptr == a.limbs.ptr) blk: { const start = buf_index; const a_len = math.min(req_limbs, a.limbs.len); - mem.copy(Limb, limbs_buffer[buf_index..], a.limbs[0..a_len]); + @memcpy(limbs_buffer[buf_index..][0..a_len], a.limbs[0..a_len]); buf_index += a_len; break :blk a.toMutable(limbs_buffer[start..buf_index]).toConst(); } else a; @@ -716,7 +716,7 @@ pub const Mutable = struct { const b_copy = if (rma.limbs.ptr == b.limbs.ptr) blk: { const start = buf_index; const b_len = math.min(req_limbs, b.limbs.len); - mem.copy(Limb, limbs_buffer[buf_index..], b.limbs[0..b_len]); + @memcpy(limbs_buffer[buf_index..][0..b_len], b.limbs[0..b_len]); buf_index += b_len; break :blk a.toMutable(limbs_buffer[start..buf_index]).toConst(); } else b; @@ -751,7 +751,7 @@ pub const Mutable = struct { const a_limbs = a.limbs[0..math.min(req_limbs, a.limbs.len)]; const b_limbs = b.limbs[0..math.min(req_limbs, b.limbs.len)]; - mem.set(Limb, rma.limbs[0..req_limbs], 0); + @memset(rma.limbs[0..req_limbs], 0); llmulacc(.add, allocator, rma.limbs, a_limbs, b_limbs); rma.normalize(math.min(req_limbs, a.limbs.len + b.limbs.len)); @@ -919,7 +919,7 @@ pub const Mutable = struct { _ = opt_allocator; assert(rma.limbs.ptr != a.limbs.ptr); // illegal aliasing - mem.set(Limb, rma.limbs, 0); + @memset(rma.limbs, 0); llsquareBasecase(rma.limbs, a.limbs); @@ -1522,7 +1522,7 @@ pub const Mutable = struct { if (xy_trailing != 0) { // Manually shift here since we know its limb aligned. mem.copyBackwards(Limb, r.limbs[xy_trailing..], r.limbs[0..r.len]); - mem.set(Limb, r.limbs[0..xy_trailing], 0); + @memset(r.limbs[0..xy_trailing], 0); r.len += xy_trailing; } } @@ -1556,7 +1556,7 @@ pub const Mutable = struct { // for 0 <= j <= n - t, set q[j] to 0 q.len = shift + 1; q.positive = true; - mem.set(Limb, q.limbs[0..q.len], 0); + @memset(q.limbs[0..q.len], 0); // 2. // while x >= y * b^(n - t): @@ -1691,7 +1691,7 @@ pub const Mutable = struct { r.addScalar(a.abs(), -1); if (req_limbs > r.len) { - mem.set(Limb, r.limbs[r.len..req_limbs], 0); + @memset(r.limbs[r.len..req_limbs], 0); } assert(r.limbs.len >= req_limbs); @@ -1730,7 +1730,7 @@ pub const Mutable = struct { // Zero-extend the result if (req_limbs > r.len) { - mem.set(Limb, r.limbs[r.len..req_limbs], 0); + @memset(r.limbs[r.len..req_limbs], 0); } // Truncate to required number of limbs. @@ -1921,8 +1921,8 @@ pub const Const = struct { /// The result is an independent resource which is managed by the caller. pub fn toManaged(self: Const, allocator: Allocator) Allocator.Error!Managed { - const limbs = try allocator.alloc(Limb, math.max(Managed.default_capacity, self.limbs.len)); - mem.copy(Limb, limbs, self.limbs); + const limbs = try allocator.alloc(Limb, @max(Managed.default_capacity, self.limbs.len)); + @memcpy(limbs[0..self.limbs.len], self.limbs); return Managed{ .allocator = allocator, .limbs = limbs, @@ -1935,7 +1935,7 @@ pub const Const = struct { /// Asserts `limbs` is big enough to store the value. pub fn toMutable(self: Const, limbs: []Limb) Mutable { - mem.copy(Limb, limbs, self.limbs[0..self.limbs.len]); + @memcpy(limbs[0..self.limbs.len], self.limbs[0..self.limbs.len]); return .{ .limbs = limbs, .positive = self.positive, @@ -2253,7 +2253,7 @@ pub const Const = struct { .positive = true, // Make absolute by ignoring self.positive. .len = self.limbs.len, }; - mem.copy(Limb, q.limbs, self.limbs); + @memcpy(q.limbs[0..self.limbs.len], self.limbs); var r: Mutable = .{ .limbs = limbs_buffer[q.limbs.len..][0..self.limbs.len], @@ -2587,8 +2587,8 @@ pub const Managed = struct { .allocator = allocator, .metadata = other.metadata, .limbs = block: { - var limbs = try allocator.alloc(Limb, other.len()); - mem.copy(Limb, limbs[0..], other.limbs[0..other.len()]); + const limbs = try allocator.alloc(Limb, other.len()); + @memcpy(limbs, other.limbs[0..other.len()]); break :block limbs; }, }; @@ -2600,7 +2600,7 @@ pub const Managed = struct { if (self.limbs.ptr == other.limbs.ptr) return; try self.ensureCapacity(other.limbs.len); - mem.copy(Limb, self.limbs[0..], other.limbs[0..other.limbs.len]); + @memcpy(self.limbs[0..other.limbs.len], other.limbs[0..other.limbs.len]); self.setMetadata(other.positive, other.limbs.len); } @@ -3302,7 +3302,7 @@ fn llmulaccKaratsuba( // Note, we don't need to compute all of p2, just enough limbs to satisfy r. const p2_limbs = math.min(limbs_after_split, a1.len + b1.len); - mem.set(Limb, tmp[0..p2_limbs], 0); + @memset(tmp[0..p2_limbs], 0); llmulacc(.add, allocator, tmp[0..p2_limbs], a1[0..math.min(a1.len, p2_limbs)], b1[0..math.min(b1.len, p2_limbs)]); const p2 = tmp[0..llnormalize(tmp[0..p2_limbs])]; @@ -3317,7 +3317,7 @@ fn llmulaccKaratsuba( // Compute p0. // Since a0.len, b0.len <= split and r.len >= split * 2, the full width of p0 needs to be computed. const p0_limbs = a0.len + b0.len; - mem.set(Limb, tmp[0..p0_limbs], 0); + @memset(tmp[0..p0_limbs], 0); llmulacc(.add, allocator, tmp[0..p0_limbs], a0, b0); const p0 = tmp[0..llnormalize(tmp[0..p0_limbs])]; @@ -3341,7 +3341,7 @@ fn llmulaccKaratsuba( return; } - mem.set(Limb, tmp, 0); + @memset(tmp, 0); // p1 is nonzero, so compute the intermediary terms j0 = a0 - a1 and j1 = b1 - b0. // Note that in this case, we again need some storage for intermediary results @@ -3666,7 +3666,7 @@ fn llshl(r: []Limb, a: []const Limb, shift: usize) void { } r[limb_shift - 1] = carry; - mem.set(Limb, r[0 .. limb_shift - 1], 0); + @memset(r[0 .. limb_shift - 1], 0); } fn llshr(r: []Limb, a: []const Limb, shift: usize) void { @@ -4061,8 +4061,8 @@ fn llpow(r: []Limb, a: []const Limb, b: u32, tmp_limbs: []Limb) void { tmp2 = tmp_limbs; } - mem.copy(Limb, tmp1, a); - mem.set(Limb, tmp1[a.len..], 0); + @memcpy(tmp1[0..a.len], a); + @memset(tmp1[a.len..], 0); // Scan the exponent as a binary number, from left to right, dropping the // most significant bit set. @@ -4074,14 +4074,14 @@ fn llpow(r: []Limb, a: []const Limb, b: u32, tmp_limbs: []Limb) void { var i: usize = 0; while (i < exp_bits) : (i += 1) { // Square - mem.set(Limb, tmp2, 0); + @memset(tmp2, 0); llsquareBasecase(tmp2, tmp1[0..llnormalize(tmp1)]); mem.swap([]Limb, &tmp1, &tmp2); // Multiply by a const ov = @shlWithOverflow(exp, 1); exp = ov[0]; if (ov[1] != 0) { - mem.set(Limb, tmp2, 0); + @memset(tmp2, 0); llmulacc(.add, null, tmp2, tmp1[0..llnormalize(tmp1)], a); mem.swap([]Limb, &tmp1, &tmp2); } diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 4b796c851a..557e39cdfc 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -192,12 +192,15 @@ test "Allocator.resize" { } } +/// Deprecated: use `@memcpy` if the arguments do not overlap, or +/// `copyForwards` if they do. +pub const copy = copyForwards; + /// Copy all of source into dest at position 0. /// dest.len must be >= source.len. /// If the slices overlap, dest.ptr must be <= src.ptr. -pub fn copy(comptime T: type, dest: []T, source: []const T) void { - for (dest[0..source.len], source) |*d, s| - d.* = s; +pub fn copyForwards(comptime T: type, dest: []T, source: []const T) void { + for (dest[0..source.len], source) |*d, s| d.* = s; } /// Copy all of source into dest at position 0. @@ -216,11 +219,7 @@ pub fn copyBackwards(comptime T: type, dest: []T, source: []const T) void { } } -/// Sets all elements of `dest` to `value`. -pub fn set(comptime T: type, dest: []T, value: T) void { - for (dest) |*d| - d.* = value; -} +pub const set = @compileError("deprecated; use @memset instead"); /// Generally, Zig users are encouraged to explicitly initialize all fields of a struct explicitly rather than using this function. /// However, it is recognized that there are sometimes use cases for initializing all fields to a "zero" value. For example, when @@ -249,7 +248,7 @@ pub fn zeroes(comptime T: type) T { if (@sizeOf(T) == 0) return undefined; if (struct_info.layout == .Extern) { var item: T = undefined; - set(u8, asBytes(&item), 0); + @memset(asBytes(&item), 0); return item; } else { var structure: T = undefined; @@ -1667,9 +1666,9 @@ pub fn writeIntSliceLittle(comptime T: type, buffer: []u8, value: T) void { assert(buffer.len >= @divExact(@typeInfo(T).Int.bits, 8)); if (@typeInfo(T).Int.bits == 0) { - return set(u8, buffer, 0); + return @memset(buffer, 0); } else if (@typeInfo(T).Int.bits == 8) { - set(u8, buffer, 0); + @memset(buffer, 0); buffer[0] = @bitCast(u8, value); return; } @@ -1691,9 +1690,9 @@ pub fn writeIntSliceBig(comptime T: type, buffer: []u8, value: T) void { assert(buffer.len >= @divExact(@typeInfo(T).Int.bits, 8)); if (@typeInfo(T).Int.bits == 0) { - return set(u8, buffer, 0); + return @memset(buffer, 0); } else if (@typeInfo(T).Int.bits == 8) { - set(u8, buffer, 0); + @memset(buffer, 0); buffer[buffer.len - 1] = @bitCast(u8, value); return; } @@ -2706,7 +2705,7 @@ fn testReadIntImpl() !void { } } -test "writeIntSlice" { +test writeIntSlice { try testWriteIntImpl(); comptime try testWriteIntImpl(); } @@ -3124,7 +3123,7 @@ pub fn replace(comptime T: type, input: []const T, needle: []const T, replacemen var replacements: usize = 0; while (slide < input.len) { if (mem.startsWith(T, input[slide..], needle)) { - mem.copy(T, output[i .. i + replacement.len], replacement); + @memcpy(output[i..][0..replacement.len], replacement); i += replacement.len; slide += needle.len; replacements += 1; diff --git a/lib/std/mem/Allocator.zig b/lib/std/mem/Allocator.zig index b402fab3fa..5110534ed4 100644 --- a/lib/std/mem/Allocator.zig +++ b/lib/std/mem/Allocator.zig @@ -307,14 +307,14 @@ pub fn free(self: Allocator, memory: anytype) void { /// Copies `m` to newly allocated memory. Caller owns the memory. pub fn dupe(allocator: Allocator, comptime T: type, m: []const T) ![]T { const new_buf = try allocator.alloc(T, m.len); - mem.copy(T, new_buf, m); + @memcpy(new_buf, m); return new_buf; } /// Copies `m` to newly allocated memory, with a null-terminated element. Caller owns the memory. pub fn dupeZ(allocator: Allocator, comptime T: type, m: []const T) ![:0]T { const new_buf = try allocator.alloc(T, m.len + 1); - mem.copy(T, new_buf, m); + @memcpy(new_buf[0..m.len], m); new_buf[m.len] = 0; return new_buf[0..m.len :0]; } diff --git a/lib/std/multi_array_list.zig b/lib/std/multi_array_list.zig index 063e2e5620..322471bedf 100644 --- a/lib/std/multi_array_list.zig +++ b/lib/std/multi_array_list.zig @@ -380,7 +380,7 @@ pub fn MultiArrayList(comptime T: type) type { inline for (fields, 0..) |field_info, i| { if (@sizeOf(field_info.type) != 0) { const field = @intToEnum(Field, i); - mem.copy(field_info.type, other_slice.items(field), self_slice.items(field)); + @memcpy(other_slice.items(field), self_slice.items(field)); } } gpa.free(self.allocatedBytes()); @@ -441,7 +441,7 @@ pub fn MultiArrayList(comptime T: type) type { inline for (fields, 0..) |field_info, i| { if (@sizeOf(field_info.type) != 0) { const field = @intToEnum(Field, i); - mem.copy(field_info.type, other_slice.items(field), self_slice.items(field)); + @memcpy(other_slice.items(field), self_slice.items(field)); } } gpa.free(self.allocatedBytes()); @@ -460,7 +460,7 @@ pub fn MultiArrayList(comptime T: type) type { inline for (fields, 0..) |field_info, i| { if (@sizeOf(field_info.type) != 0) { const field = @intToEnum(Field, i); - mem.copy(field_info.type, result_slice.items(field), self_slice.items(field)); + @memcpy(result_slice.items(field), self_slice.items(field)); } } return result; diff --git a/lib/std/net.zig b/lib/std/net.zig index d59676ec17..15a9e01da8 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -106,8 +106,8 @@ pub const Address = extern union { // Add 1 to ensure a terminating 0 is present in the path array for maximum portability. if (path.len + 1 > sock_addr.path.len) return error.NameTooLong; - mem.set(u8, &sock_addr.path, 0); - mem.copy(u8, &sock_addr.path, path); + @memset(&sock_addr.path, 0); + @memcpy(sock_addr.path[0..path.len], path); return Address{ .un = sock_addr }; } @@ -346,7 +346,7 @@ pub const Ip6Address = extern struct { if (!saw_any_digits) { if (abbrv) return error.InvalidCharacter; // ':::' if (i != 0) abbrv = true; - mem.set(u8, ip_slice[index..], 0); + @memset(ip_slice[index..], 0); ip_slice = tail[0..]; index = 0; continue; @@ -416,7 +416,7 @@ pub const Ip6Address = extern struct { index += 1; ip_slice[index] = @truncate(u8, x); index += 1; - mem.copy(u8, result.sa.addr[16 - index ..], ip_slice[0..index]); + @memcpy(result.sa.addr[16 - index ..][0..index], ip_slice[0..index]); return result; } } @@ -465,7 +465,7 @@ pub const Ip6Address = extern struct { if (!saw_any_digits) { if (abbrv) return error.InvalidCharacter; // ':::' if (i != 0) abbrv = true; - mem.set(u8, ip_slice[index..], 0); + @memset(ip_slice[index..], 0); ip_slice = tail[0..]; index = 0; continue; @@ -550,7 +550,7 @@ pub const Ip6Address = extern struct { index += 1; ip_slice[index] = @truncate(u8, x); index += 1; - mem.copy(u8, result.sa.addr[16 - index ..], ip_slice[0..index]); + @memcpy(result.sa.addr[16 - index ..][0..index], ip_slice[0..index]); return result; } } @@ -662,7 +662,7 @@ fn if_nametoindex(name: []const u8) !u32 { var sockfd = try os.socket(os.AF.UNIX, os.SOCK.DGRAM | os.SOCK.CLOEXEC, 0); defer os.closeSocket(sockfd); - std.mem.copy(u8, &ifr.ifrn.name, name); + @memcpy(ifr.ifrn.name[0..name.len], name); ifr.ifrn.name[name.len] = 0; // TODO investigate if this needs to be integrated with evented I/O. @@ -676,7 +676,7 @@ fn if_nametoindex(name: []const u8) !u32 { return error.NameTooLong; var if_name: [os.IFNAMESIZE:0]u8 = undefined; - std.mem.copy(u8, &if_name, name); + @memcpy(if_name[0..name.len], name); if_name[name.len] = 0; const if_slice = if_name[0..name.len :0]; const index = os.system.if_nametoindex(if_slice); @@ -1041,14 +1041,14 @@ fn linuxLookupName( var salen: os.socklen_t = undefined; var dalen: os.socklen_t = undefined; if (addr.addr.any.family == os.AF.INET6) { - mem.copy(u8, &da6.addr, &addr.addr.in6.sa.addr); + da6.addr = addr.addr.in6.sa.addr; da = @ptrCast(*os.sockaddr, &da6); dalen = @sizeOf(os.sockaddr.in6); sa = @ptrCast(*os.sockaddr, &sa6); salen = @sizeOf(os.sockaddr.in6); } else { - mem.copy(u8, &sa6.addr, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff"); - mem.copy(u8, &da6.addr, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff"); + sa6.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; + da6.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; mem.writeIntNative(u32, da6.addr[12..], addr.addr.in.sa.addr); da4.addr = addr.addr.in.sa.addr; da = @ptrCast(*os.sockaddr, &da4); @@ -1343,7 +1343,7 @@ fn linuxLookupNameFromDnsSearch( // name is not a CNAME record) and serves as a buffer for passing // the full requested name to name_from_dns. try canon.resize(canon_name.len); - mem.copy(u8, canon.items, canon_name); + @memcpy(canon.items, canon_name); try canon.append('.'); var tok_it = mem.tokenize(u8, search, " \t"); @@ -1567,7 +1567,7 @@ fn resMSendRc( for (0..ns.len) |i| { if (ns[i].any.family != os.AF.INET) continue; mem.writeIntNative(u32, ns[i].in6.sa.addr[12..], ns[i].in.sa.addr); - mem.copy(u8, ns[i].in6.sa.addr[0..12], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff"); + ns[i].in6.sa.addr[0..12].* = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff".*; ns[i].any.family = os.AF.INET6; ns[i].in6.sa.flowinfo = 0; ns[i].in6.sa.scope_id = 0; @@ -1665,7 +1665,7 @@ fn resMSendRc( if (i == next) { while (next < queries.len and answers[next].len != 0) : (next += 1) {} } else { - mem.copy(u8, answer_bufs[i], answer_bufs[next][0..rlen]); + @memcpy(answer_bufs[i][0..rlen], answer_bufs[next][0..rlen]); } if (next == queries.len) break :outer; diff --git a/lib/std/os.zig b/lib/std/os.zig index 9a0c05f457..32c73916d4 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -1881,9 +1881,9 @@ pub fn execvpeZ_expandArg0( while (it.next()) |search_path| { const path_len = search_path.len + file_slice.len + 1; if (path_buf.len < path_len + 1) return error.NameTooLong; - mem.copy(u8, &path_buf, search_path); + @memcpy(path_buf[0..search_path.len], search_path); path_buf[search_path.len] = '/'; - mem.copy(u8, path_buf[search_path.len + 1 ..], file_slice); + @memcpy(path_buf[search_path.len + 1 ..][0..file_slice.len], file_slice); path_buf[path_len] = 0; const full_path = path_buf[0..path_len :0].ptr; switch (arg0_expand) { @@ -1917,7 +1917,7 @@ pub fn getenv(key: []const u8) ?[]const u8 { if (builtin.link_libc) { var small_key_buf: [64]u8 = undefined; if (key.len < small_key_buf.len) { - mem.copy(u8, &small_key_buf, key); + @memcpy(small_key_buf[0..key.len], key); small_key_buf[key.len] = 0; const key0 = small_key_buf[0..key.len :0]; return getenvZ(key0); @@ -2022,8 +2022,9 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 { } else if (builtin.os.tag == .wasi and !builtin.link_libc) { const path = "."; if (out_buffer.len < path.len) return error.NameTooLong; - std.mem.copy(u8, out_buffer, path); - return out_buffer[0..path.len]; + const result = out_buffer[0..path.len]; + @memcpy(result, path); + return result; } const err = if (builtin.link_libc) blk: { @@ -2673,7 +2674,7 @@ pub fn renameatW( .FileNameLength = @intCast(u32, new_path_w.len * 2), // already checked error.NameTooLong .FileName = undefined, }; - std.mem.copy(u16, @as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w); + @memcpy(@as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w); var io_status_block: windows.IO_STATUS_BLOCK = undefined; @@ -5264,8 +5265,9 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { } const len = mem.indexOfScalar(u8, &kfile.path, 0) orelse MAX_PATH_BYTES; if (len == 0) return error.NameTooLong; - mem.copy(u8, out_buffer, kfile.path[0..len]); - return out_buffer[0..len]; + const result = out_buffer[0..len]; + @memcpy(result, kfile.path[0..len]); + return result; } else { // This fallback implementation reimplements libutil's `kinfo_getfile()`. // The motivation is to avoid linking -lutil when building zig or general @@ -5296,8 +5298,9 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { if (kf.fd == fd) { len = mem.indexOfScalar(u8, &kf.path, 0) orelse MAX_PATH_BYTES; if (len == 0) return error.NameTooLong; - mem.copy(u8, out_buffer, kf.path[0..len]); - return out_buffer[0..len]; + const result = out_buffer[0..len]; + @memcpy(result, kf.path[0..len]); + return result; } i += @intCast(usize, kf.structsize); } @@ -5686,8 +5689,9 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 { if (builtin.os.tag == .linux) { const uts = uname(); const hostname = mem.sliceTo(&uts.nodename, 0); - mem.copy(u8, name_buffer, hostname); - return name_buffer[0..hostname.len]; + const result = name_buffer[0..hostname.len]; + @memcpy(result, hostname); + return result; } @compileError("TODO implement gethostname for this OS"); @@ -5725,7 +5729,7 @@ pub fn res_mkquery( @memset(q[0..n], 0); q[2] = @as(u8, op) * 8 + 1; q[5] = 1; - mem.copy(u8, q[13..], name); + @memcpy(q[13..][0..name.len], name); var i: usize = 13; var j: usize = undefined; while (q[i] != 0) : (i = j + 1) { @@ -5748,7 +5752,7 @@ pub fn res_mkquery( q[0] = @truncate(u8, id / 256); q[1] = @truncate(u8, id); - mem.copy(u8, buf, q[0..n]); + @memcpy(buf[0..n], q[0..n]); return n; } @@ -6755,7 +6759,7 @@ fn toMemFdPath(name: []const u8) ![MFD_MAX_NAME_LEN:0]u8 { var path_with_null: [MFD_MAX_NAME_LEN:0]u8 = undefined; // >= rather than > to make room for the null byte if (name.len >= MFD_MAX_NAME_LEN) return error.NameTooLong; - mem.copy(u8, &path_with_null, name); + @memcpy(path_with_null[0..name.len], name); path_with_null[name.len] = 0; return path_with_null; } diff --git a/lib/std/os/linux/bpf.zig b/lib/std/os/linux/bpf.zig index db6473d673..63b669acd5 100644 --- a/lib/std/os/linux/bpf.zig +++ b/lib/std/os/linux/bpf.zig @@ -1631,7 +1631,7 @@ test "map lookup, update, and delete" { const status = try map_get_next_key(map, &lookup_key, &next_key); try expectEqual(status, true); try expectEqual(next_key, key); - std.mem.copy(u8, &lookup_key, &next_key); + lookup_key = next_key; const status2 = try map_get_next_key(map, &lookup_key, &next_key); try expectEqual(status2, false); diff --git a/lib/std/os/linux/io_uring.zig b/lib/std/os/linux/io_uring.zig index 20248fd65f..b7467d765f 100644 --- a/lib/std/os/linux/io_uring.zig +++ b/lib/std/os/linux/io_uring.zig @@ -1855,8 +1855,8 @@ test "write_fixed/read_fixed" { var raw_buffers: [2][11]u8 = undefined; // First buffer will be written to the file. - std.mem.set(u8, &raw_buffers[0], 'z'); - std.mem.copy(u8, &raw_buffers[0], "foobar"); + @memset(&raw_buffers[0], 'z'); + raw_buffers[0][0.."foobar".len].* = "foobar".*; var buffers = [2]os.iovec{ .{ .iov_base = &raw_buffers[0], .iov_len = raw_buffers[0].len }, @@ -2966,7 +2966,7 @@ test "provide_buffers: read" { // Provide 1 buffer again // Deliberately put something we don't expect in the buffers - mem.set(u8, mem.sliceAsBytes(&buffers), 42); + @memset(mem.sliceAsBytes(&buffers), 42); const reprovided_buffer_id = 2; @@ -3155,7 +3155,7 @@ test "provide_buffers: accept/connect/send/recv" { // Do 4 recv which should consume all buffers // Deliberately put something we don't expect in the buffers - mem.set(u8, mem.sliceAsBytes(&buffers), 1); + @memset(mem.sliceAsBytes(&buffers), 1); var i: usize = 0; while (i < buffers.len) : (i += 1) { @@ -3235,7 +3235,7 @@ test "provide_buffers: accept/connect/send/recv" { // Final recv which should work // Deliberately put something we don't expect in the buffers - mem.set(u8, mem.sliceAsBytes(&buffers), 1); + @memset(mem.sliceAsBytes(&buffers), 1); { var sqe = try ring.recv(0xdfdfdfdf, socket_test_harness.client, .{ .buffer_selection = .{ .group_id = group_id, .len = buffer_len } }, 0); diff --git a/lib/std/os/linux/tls.zig b/lib/std/os/linux/tls.zig index cffdec0424..311e5609e8 100644 --- a/lib/std/os/linux/tls.zig +++ b/lib/std/os/linux/tls.zig @@ -275,7 +275,7 @@ inline fn alignPtrCast(comptime T: type, ptr: [*]u8) *T { /// architecture-specific value of the thread-pointer register pub fn prepareTLS(area: []u8) usize { // Clear the area we're going to use, just to be safe - mem.set(u8, area, 0); + @memset(area, 0); // Prepare the DTV const dtv = alignPtrCast(DTV, area.ptr + tls_image.dtv_offset); dtv.entries = 1; @@ -287,7 +287,7 @@ pub fn prepareTLS(area: []u8) usize { .VariantII => area.ptr + tls_image.tcb_offset, }; // Copy the data - mem.copy(u8, area[tls_image.data_offset..], tls_image.init_data); + @memcpy(area[tls_image.data_offset..][0..tls_image.init_data.len], tls_image.init_data); // Return the corrected value (if needed) for the tp register. // Overflow here is not a problem, the pointer arithmetic involving the tp diff --git a/lib/std/os/test.zig b/lib/std/os/test.zig index 682bd4f6f8..f694ea277a 100644 --- a/lib/std/os/test.zig +++ b/lib/std/os/test.zig @@ -587,7 +587,7 @@ test "mmap" { try testing.expect(mem.eql(u8, data, &[_]u8{0x00} ** 1234)); // Make sure the memory is writeable as requested - std.mem.set(u8, data, 0x55); + @memset(data, 0x55); try testing.expect(mem.eql(u8, data, &[_]u8{0x55} ** 1234)); } diff --git a/lib/std/os/uefi/protocols/device_path_protocol.zig b/lib/std/os/uefi/protocols/device_path_protocol.zig index fb497a79da..2365b0cb2e 100644 --- a/lib/std/os/uefi/protocols/device_path_protocol.zig +++ b/lib/std/os/uefi/protocols/device_path_protocol.zig @@ -48,7 +48,7 @@ pub const DevicePathProtocol = extern struct { // DevicePathProtocol for the extra node before the end var buf = try allocator.alloc(u8, path_size + 2 * (path.len + 1) + @sizeOf(DevicePathProtocol)); - mem.copy(u8, buf, @ptrCast([*]const u8, self)[0..path_size]); + @memcpy(buf[0..path_size.len], @ptrCast([*]const u8, self)[0..path_size]); // Pointer to the copy of the end node of the current chain, which is - 4 from the buffer // as the end node itself is 4 bytes (type: u8 + subtype: u8 + length: u16). diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index 23139c9f92..6d654f1c20 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -754,7 +754,7 @@ pub fn CreateSymbolicLink( .Flags = if (dir) |_| SYMLINK_FLAG_RELATIVE else 0, }; - std.mem.copy(u8, buffer[0..], std.mem.asBytes(&symlink_data)); + @memcpy(buffer[0..@sizeOf(SYMLINK_DATA)], std.mem.asBytes(&symlink_data)); @memcpy(buffer[@sizeOf(SYMLINK_DATA)..][0 .. target_path.len * 2], @ptrCast([*]const u8, target_path)); const paths_start = @sizeOf(SYMLINK_DATA) + target_path.len * 2; @memcpy(buffer[paths_start..][0 .. target_path.len * 2], @ptrCast([*]const u8, target_path)); @@ -1208,8 +1208,8 @@ pub fn GetFinalPathNameByHandle( if (out_buffer.len < drive_letter.len + file_name_u16.len) return error.NameTooLong; - mem.copy(u16, out_buffer, drive_letter); - mem.copy(u16, out_buffer[drive_letter.len..], file_name_u16); + @memcpy(out_buffer[0..drive_letter.len], drive_letter); + mem.copyForwards(u16, out_buffer[drive_letter.len..][0..file_name_u16.len], file_name_u16); const total_len = drive_letter.len + file_name_u16.len; // Validate that DOS does not contain any spurious nul bytes. @@ -2012,7 +2012,7 @@ pub fn sliceToPrefixedFileW(s: []const u8) !PathSpace { } const prefix_u16 = [_]u16{ '\\', '?', '?', '\\' }; const start_index = if (prefix_index > 0 or !std.fs.path.isAbsolute(s)) 0 else blk: { - mem.copy(u16, path_space.data[0..], prefix_u16[0..]); + path_space.data[0..prefix_u16.len].* = prefix_u16; break :blk prefix_u16.len; }; path_space.len = start_index + try std.unicode.utf8ToUtf16Le(path_space.data[start_index..], s); @@ -2025,7 +2025,7 @@ pub fn sliceToPrefixedFileW(s: []const u8) !PathSpace { std.debug.assert(temp_path.len == path_space.len); temp_path.data[path_space.len] = 0; path_space.len = prefix_u16.len + try getFullPathNameW(&temp_path.data, path_space.data[prefix_u16.len..]); - mem.copy(u16, &path_space.data, &prefix_u16); + path_space.data[0..prefix_u16.len].* = prefix_u16; std.debug.assert(path_space.data[path_space.len] == 0); return path_space; } @@ -2053,12 +2053,12 @@ pub fn wToPrefixedFileW(s: []const u16) !PathSpace { const start_index = if (mem.startsWith(u16, s, &[_]u16{ '\\', '?' })) 0 else blk: { const prefix = [_]u16{ '\\', '?', '?', '\\' }; - mem.copy(u16, path_space.data[0..], &prefix); + path_space.data[0..prefix.len].* = prefix; break :blk prefix.len; }; path_space.len = start_index + s.len; if (path_space.len > path_space.data.len) return error.NameTooLong; - mem.copy(u16, path_space.data[start_index..], s); + @memcpy(path_space.data[start_index..][0..s.len], s); // > File I/O functions in the Windows API convert "/" to "\" as part of // > converting the name to an NT-style name, except when using the "\\?\" // > prefix as detailed in the following sections. diff --git a/lib/std/process.zig b/lib/std/process.zig index 56e3708f3b..504f9075eb 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -855,7 +855,7 @@ pub fn argsAlloc(allocator: Allocator) ![][:0]u8 { const result_slice_list = mem.bytesAsSlice([:0]u8, buf[0..slice_list_bytes]); const result_contents = buf[slice_list_bytes..]; - mem.copy(u8, result_contents, contents_slice); + @memcpy(result_contents[0..contents_slice.len], contents_slice); var contents_index: usize = 0; for (slice_sizes, 0..) |len, i| { diff --git a/lib/std/rand/ChaCha.zig b/lib/std/rand/ChaCha.zig index 0992aeb97e..3878fb25c8 100644 --- a/lib/std/rand/ChaCha.zig +++ b/lib/std/rand/ChaCha.zig @@ -40,7 +40,8 @@ pub fn addEntropy(self: *Self, bytes: []const u8) void { } if (i < bytes.len) { var k = [_]u8{0} ** Cipher.key_length; - mem.copy(u8, k[0..], bytes[i..]); + const src = bytes[i..]; + @memcpy(k[0..src.len], src); Cipher.xor( self.state[0..Cipher.key_length], self.state[0..Cipher.key_length], @@ -72,8 +73,8 @@ pub fn fill(self: *Self, buf_: []u8) void { if (avail > 0) { // Bytes from the current block const n = @min(avail, buf.len); - mem.copy(u8, buf[0..n], bytes[self.offset..][0..n]); - mem.set(u8, bytes[self.offset..][0..n], 0); + @memcpy(buf[0..n], bytes[self.offset..][0..n]); + @memset(bytes[self.offset..][0..n], 0); buf = buf[n..]; self.offset += n; } @@ -83,15 +84,15 @@ pub fn fill(self: *Self, buf_: []u8) void { // Full blocks while (buf.len >= bytes.len) { - mem.copy(u8, buf[0..bytes.len], bytes); + @memcpy(buf[0..bytes.len], bytes); buf = buf[bytes.len..]; self.refill(); } // Remaining bytes if (buf.len > 0) { - mem.copy(u8, buf, bytes[0..buf.len]); - mem.set(u8, bytes[0..buf.len], 0); + @memcpy(buf, bytes[0..buf.len]); + @memset(bytes[0..buf.len], 0); self.offset = buf.len; } } diff --git a/lib/std/rand/Isaac64.zig b/lib/std/rand/Isaac64.zig index 42242008fc..8c6205e1cd 100644 --- a/lib/std/rand/Isaac64.zig +++ b/lib/std/rand/Isaac64.zig @@ -87,7 +87,7 @@ fn next(self: *Isaac64) u64 { fn seed(self: *Isaac64, init_s: u64, comptime rounds: usize) void { // We ignore the multi-pass requirement since we don't currently expose full access to // seeding the self.m array completely. - mem.set(u64, self.m[0..], 0); + @memset(self.m[0..], 0); self.m[0] = init_s; // prescrambled golden ratio constants @@ -143,7 +143,7 @@ fn seed(self: *Isaac64, init_s: u64, comptime rounds: usize) void { } } - mem.set(u64, self.r[0..], 0); + @memset(self.r[0..], 0); self.a = 0; self.b = 0; self.c = 0; diff --git a/lib/std/segmented_list.zig b/lib/std/segmented_list.zig index 7c0a27b5b7..172fe4e7c3 100644 --- a/lib/std/segmented_list.zig +++ b/lib/std/segmented_list.zig @@ -230,7 +230,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type allocator.free(new_dynamic_segments); } else { // Good thing we allocated that new memory slice. - mem.copy([*]T, new_dynamic_segments, self.dynamic_segments[0..new_cap_shelf_count]); + @memcpy(new_dynamic_segments, self.dynamic_segments[0..new_cap_shelf_count]); allocator.free(self.dynamic_segments); self.dynamic_segments = new_dynamic_segments; } @@ -248,24 +248,21 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type var i = start; if (end <= prealloc_item_count) { - mem.copy(T, dest[i - start ..], self.prealloc_segment[i..end]); + const src = self.prealloc_segment[i..end]; + @memcpy(dest[i - start ..][0..src.len], src); return; } else if (i < prealloc_item_count) { - mem.copy(T, dest[i - start ..], self.prealloc_segment[i..]); + const src = self.prealloc_segment[i..]; + @memcpy(dest[i - start ..][0..src.len], src); i = prealloc_item_count; } while (i < end) { const shelf_index = shelfIndex(i); const copy_start = boxIndex(i, shelf_index); - const copy_end = std.math.min(shelfSize(shelf_index), copy_start + end - i); - - mem.copy( - T, - dest[i - start ..], - self.dynamic_segments[shelf_index][copy_start..copy_end], - ); - + const copy_end = @min(shelfSize(shelf_index), copy_start + end - i); + const src = self.dynamic_segments[shelf_index][copy_start..copy_end]; + @memcpy(dest[i - start ..][0..src.len], src); i += (copy_end - copy_start); } } @@ -498,11 +495,11 @@ fn testSegmentedList(comptime prealloc: usize) !void { control[@intCast(usize, i)] = i + 1; } - mem.set(i32, dest[0..], 0); + @memset(dest[0..], 0); list.writeToSlice(dest[0..], 0); try testing.expect(mem.eql(i32, control[0..], dest[0..])); - mem.set(i32, dest[0..], 0); + @memset(dest[0..], 0); list.writeToSlice(dest[50..], 50); try testing.expect(mem.eql(i32, control[50..], dest[50..])); } diff --git a/lib/std/sort.zig b/lib/std/sort.zig index d2b79db91b..080ab5200e 100644 --- a/lib/std/sort.zig +++ b/lib/std/sort.zig @@ -361,8 +361,10 @@ pub fn sort( if (lessThan(context, items[B1.end - 1], items[A1.start])) { // the two ranges are in reverse order, so copy them in reverse order into the cache - mem.copy(T, cache[B1.length()..], items[A1.start..A1.end]); - mem.copy(T, cache[0..], items[B1.start..B1.end]); + const a1_items = items[A1.start..A1.end]; + @memcpy(cache[B1.length()..][0..a1_items.len], a1_items); + const b1_items = items[B1.start..B1.end]; + @memcpy(cache[0..b1_items.len], b1_items); } else if (lessThan(context, items[B1.start], items[A1.end - 1])) { // these two ranges weren't already in order, so merge them into the cache mergeInto(T, items, A1, B1, context, lessThan, cache[0..]); @@ -371,23 +373,29 @@ pub fn sort( if (!lessThan(context, items[B2.start], items[A2.end - 1]) and !lessThan(context, items[A2.start], items[B1.end - 1])) continue; // copy A1 and B1 into the cache in the same order - mem.copy(T, cache[0..], items[A1.start..A1.end]); - mem.copy(T, cache[A1.length()..], items[B1.start..B1.end]); + const a1_items = items[A1.start..A1.end]; + @memcpy(cache[0..a1_items.len], a1_items); + const b1_items = items[B1.start..B1.end]; + @memcpy(cache[A1.length()..][0..b1_items.len], b1_items); } A1 = Range.init(A1.start, B1.end); // merge A2 and B2 into the cache if (lessThan(context, items[B2.end - 1], items[A2.start])) { // the two ranges are in reverse order, so copy them in reverse order into the cache - mem.copy(T, cache[A1.length() + B2.length() ..], items[A2.start..A2.end]); - mem.copy(T, cache[A1.length()..], items[B2.start..B2.end]); + const a2_items = items[A2.start..A2.end]; + @memcpy(cache[A1.length() + B2.length() ..][0..a2_items.len], a2_items); + const b2_items = items[B2.start..B2.end]; + @memcpy(cache[A1.length()..][0..b2_items.len], b2_items); } else if (lessThan(context, items[B2.start], items[A2.end - 1])) { // these two ranges weren't already in order, so merge them into the cache mergeInto(T, items, A2, B2, context, lessThan, cache[A1.length()..]); } else { // copy A2 and B2 into the cache in the same order - mem.copy(T, cache[A1.length()..], items[A2.start..A2.end]); - mem.copy(T, cache[A1.length() + A2.length() ..], items[B2.start..B2.end]); + const a2_items = items[A2.start..A2.end]; + @memcpy(cache[A1.length()..][0..a2_items.len], a2_items); + const b2_items = items[B2.start..B2.end]; + @memcpy(cache[A1.length() + A2.length() ..][0..b2_items.len], b2_items); } A2 = Range.init(A2.start, B2.end); @@ -397,15 +405,19 @@ pub fn sort( if (lessThan(context, cache[B3.end - 1], cache[A3.start])) { // the two ranges are in reverse order, so copy them in reverse order into the items - mem.copy(T, items[A1.start + A2.length() ..], cache[A3.start..A3.end]); - mem.copy(T, items[A1.start..], cache[B3.start..B3.end]); + const a3_items = cache[A3.start..A3.end]; + @memcpy(items[A1.start + A2.length() ..][0..a3_items.len], a3_items); + const b3_items = cache[B3.start..B3.end]; + @memcpy(items[A1.start..][0..b3_items.len], b3_items); } else if (lessThan(context, cache[B3.start], cache[A3.end - 1])) { // these two ranges weren't already in order, so merge them back into the items mergeInto(T, cache[0..], A3, B3, context, lessThan, items[A1.start..]); } else { // copy A3 and B3 into the items in the same order - mem.copy(T, items[A1.start..], cache[A3.start..A3.end]); - mem.copy(T, items[A1.start + A1.length() ..], cache[B3.start..B3.end]); + const a3_items = cache[A3.start..A3.end]; + @memcpy(items[A1.start..][0..a3_items.len], a3_items); + const b3_items = cache[B3.start..B3.end]; + @memcpy(items[A1.start + A1.length() ..][0..b3_items.len], b3_items); } } @@ -423,7 +435,8 @@ pub fn sort( mem.rotate(T, items[A.start..B.end], A.length()); } else if (lessThan(context, items[B.start], items[A.end - 1])) { // these two ranges weren't already in order, so we'll need to merge them! - mem.copy(T, cache[0..], items[A.start..A.end]); + const a_items = items[A.start..A.end]; + @memcpy(cache[0..a_items.len], a_items); mergeExternal(T, items, A, B, context, lessThan, cache[0..]); } } @@ -718,7 +731,8 @@ pub fn sort( // if the first unevenly sized A block fits into the cache, copy it there for when we go to Merge it // otherwise, if the second buffer is available, block swap the contents into that if (lastA.length() <= cache.len) { - mem.copy(T, cache[0..], items[lastA.start..lastA.end]); + const last_a_items = items[lastA.start..lastA.end]; + @memcpy(cache[0..last_a_items.len], last_a_items); } else if (buffer2.length() > 0) { blockSwap(T, items, lastA.start, buffer2.start, lastA.length()); } @@ -762,7 +776,7 @@ pub fn sort( if (buffer2.length() > 0 or block_size <= cache.len) { // copy the previous A block into the cache or buffer2, since that's where we need it to be when we go to merge it anyway if (block_size <= cache.len) { - mem.copy(T, cache[0..], items[blockA.start .. blockA.start + block_size]); + @memcpy(cache[0..block_size], items[blockA.start..][0..block_size]); } else { blockSwap(T, items, blockA.start, buffer2.start, block_size); } @@ -1122,7 +1136,8 @@ fn mergeInto( insert_index += 1; if (A_index == A_last) { // copy the remainder of B into the final array - mem.copy(T, into[insert_index..], from[B_index..B_last]); + const from_b = from[B_index..B_last]; + @memcpy(into[insert_index..][0..from_b.len], from_b); break; } } else { @@ -1131,7 +1146,8 @@ fn mergeInto( insert_index += 1; if (B_index == B_last) { // copy the remainder of A into the final array - mem.copy(T, into[insert_index..], from[A_index..A_last]); + const from_a = from[A_index..A_last]; + @memcpy(into[insert_index..][0..from_a.len], from_a); break; } } @@ -1171,7 +1187,8 @@ fn mergeExternal( } // copy the remainder of A into the final array - mem.copy(T, items[insert_index..], cache[A_index..A_last]); + const cache_a = cache[A_index..A_last]; + @memcpy(items[insert_index..][0..cache_a.len], cache_a); } fn swap( @@ -1305,7 +1322,7 @@ test "sort" { for (u8cases) |case| { var buf: [8]u8 = undefined; const slice = buf[0..case[0].len]; - mem.copy(u8, slice, case[0]); + @memcpy(slice, case[0]); sort(u8, slice, {}, asc_u8); try testing.expect(mem.eql(u8, slice, case[1])); } @@ -1340,7 +1357,7 @@ test "sort" { for (i32cases) |case| { var buf: [8]i32 = undefined; const slice = buf[0..case[0].len]; - mem.copy(i32, slice, case[0]); + @memcpy(slice, case[0]); sort(i32, slice, {}, asc_i32); try testing.expect(mem.eql(i32, slice, case[1])); } @@ -1377,7 +1394,7 @@ test "sort descending" { for (rev_cases) |case| { var buf: [8]i32 = undefined; const slice = buf[0..case[0].len]; - mem.copy(i32, slice, case[0]); + @memcpy(slice, case[0]); sort(i32, slice, {}, desc_i32); try testing.expect(mem.eql(i32, slice, case[1])); } diff --git a/lib/std/tar.zig b/lib/std/tar.zig index ec668d5f93..c570c8e09c 100644 --- a/lib/std/tar.zig +++ b/lib/std/tar.zig @@ -55,9 +55,9 @@ pub const Header = struct { const p = prefix(header); if (p.len == 0) return n; - std.mem.copy(u8, buffer[0..p.len], p); + @memcpy(buffer[0..p.len], p); buffer[p.len] = '/'; - std.mem.copy(u8, buffer[p.len + 1 ..], n); + @memcpy(buffer[p.len + 1 ..][0..n.len], n); return buffer[0 .. p.len + 1 + n.len]; } @@ -101,8 +101,9 @@ pub fn pipeToFileSystem(dir: std.fs.Dir, reader: anytype, options: Options) !voi var end: usize = 0; header: while (true) { if (buffer.len - start < 1024) { - std.mem.copy(u8, &buffer, buffer[start..end]); - end -= start; + const dest_end = end - start; + @memcpy(buffer[0..dest_end], buffer[start..end]); + end = dest_end; start = 0; } const ask_header = @min(buffer.len - end, 1024 -| (end - start)); @@ -138,8 +139,9 @@ pub fn pipeToFileSystem(dir: std.fs.Dir, reader: anytype, options: Options) !voi var file_off: usize = 0; while (true) { if (buffer.len - start < 1024) { - std.mem.copy(u8, &buffer, buffer[start..end]); - end -= start; + const dest_end = end - start; + @memcpy(buffer[0..dest_end], buffer[start..end]); + end = dest_end; start = 0; } // Ask for the rounded up file size + 512 for the next header. diff --git a/lib/std/target.zig b/lib/std/target.zig index d9214f7be6..f23e32fbe7 100644 --- a/lib/std/target.zig +++ b/lib/std/target.zig @@ -1596,7 +1596,7 @@ pub const Target = struct { /// Asserts that the length is less than or equal to 255 bytes. pub fn set(self: *DynamicLinker, dl_or_null: ?[]const u8) void { if (dl_or_null) |dl| { - mem.copy(u8, &self.buffer, dl); + @memcpy(self.buffer[0..dl.len], dl); self.max_byte = @intCast(u8, dl.len - 1); } else { self.max_byte = null; @@ -1612,7 +1612,7 @@ pub const Target = struct { return r.*; } fn copy(r: *DynamicLinker, s: []const u8) DynamicLinker { - mem.copy(u8, &r.buffer, s); + @memcpy(r.buffer[0..s.len], s); r.max_byte = @intCast(u8, s.len - 1); return r.*; } diff --git a/lib/std/testing/failing_allocator.zig b/lib/std/testing/failing_allocator.zig index 914e405b11..2cdb78cd1d 100644 --- a/lib/std/testing/failing_allocator.zig +++ b/lib/std/testing/failing_allocator.zig @@ -66,7 +66,7 @@ pub const FailingAllocator = struct { const self = @ptrCast(*FailingAllocator, @alignCast(@alignOf(FailingAllocator), ctx)); if (self.index == self.fail_index) { if (!self.has_induced_failure) { - mem.set(usize, &self.stack_addresses, 0); + @memset(&self.stack_addresses, 0); var stack_trace = std.builtin.StackTrace{ .instruction_addresses = &self.stack_addresses, .index = 0, diff --git a/lib/std/tz.zig b/lib/std/tz.zig index 76eb3b06fc..0cb9cefa50 100644 --- a/lib/std/tz.zig +++ b/lib/std/tz.zig @@ -137,7 +137,7 @@ pub const Tz = struct { const name = std.mem.sliceTo(designators[tt.name_data[0]..], 0); // We are mandating the "SHOULD" 6-character limit so we can pack the struct better, and to conform to POSIX. if (name.len > 6) return error.Malformed; // rfc8536: Time zone designations SHOULD consist of at least three (3) and no more than six (6) ASCII characters. - std.mem.copy(u8, tt.name_data[0..], name); + @memcpy(tt.name_data[0..name.len], name); tt.name_data[name.len] = 0; } diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 2cb1d170b4..d8a8c8e9ed 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -1889,11 +1889,11 @@ fn renderArrayInit( // A place to store the width of each expression and its column's maximum const widths = try gpa.alloc(usize, row_exprs.len + row_size); defer gpa.free(widths); - mem.set(usize, widths, 0); + @memset(widths, 0); const expr_newlines = try gpa.alloc(bool, row_exprs.len); defer gpa.free(expr_newlines); - mem.set(bool, expr_newlines, false); + @memset(expr_newlines, false); const expr_widths = widths[0..row_exprs.len]; const column_widths = widths[row_exprs.len..]; diff --git a/lib/std/zig/system/NativeTargetInfo.zig b/lib/std/zig/system/NativeTargetInfo.zig index 987358ed5a..539ad96365 100644 --- a/lib/std/zig/system/NativeTargetInfo.zig +++ b/lib/std/zig/system/NativeTargetInfo.zig @@ -877,17 +877,17 @@ pub fn abiAndDynamicLinkerFromFile( const cpu_arch = @tagName(result.target.cpu.arch); const os_tag = @tagName(result.target.os.tag); const abi = @tagName(result.target.abi); - mem.copy(u8, path_buf[index..], prefix); + @memcpy(path_buf[index..][0..prefix.len], prefix); index += prefix.len; - mem.copy(u8, path_buf[index..], cpu_arch); + @memcpy(path_buf[index..][0..cpu_arch.len], cpu_arch); index += cpu_arch.len; path_buf[index] = '-'; index += 1; - mem.copy(u8, path_buf[index..], os_tag); + @memcpy(path_buf[index..][0..os_tag.len], os_tag); index += os_tag.len; path_buf[index] = '-'; index += 1; - mem.copy(u8, path_buf[index..], abi); + @memcpy(path_buf[index..][0..abi.len], abi); index += abi.len; const rpath = path_buf[0..index]; if (glibcVerFromRPath(rpath)) |ver| { diff --git a/lib/std/zig/system/windows.zig b/lib/std/zig/system/windows.zig index 45b44560a2..6039425b1a 100644 --- a/lib/std/zig/system/windows.zig +++ b/lib/std/zig/system/windows.zig @@ -171,10 +171,10 @@ fn getCpuInfoFromRegistry(core: usize, args: anytype) !void { const entry = @ptrCast([*]align(1) const u8, table[i + 1].EntryContext); switch (@field(args, field.name).value_type) { REG.DWORD, REG.DWORD_BIG_ENDIAN => { - mem.copy(u8, @field(args, field.name).value_buf[0..4], entry[0..4]); + @memcpy(@field(args, field.name).value_buf[0..4], entry[0..4]); }, REG.QWORD => { - mem.copy(u8, @field(args, field.name).value_buf[0..8], entry[0..8]); + @memcpy(@field(args, field.name).value_buf[0..8], entry[0..8]); }, else => unreachable, } |
