aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/compiler_rt/udivmodei4.zig6
-rw-r--r--lib/std/Build.zig4
-rw-r--r--lib/std/Build/Cache.zig2
-rw-r--r--lib/std/Build/CompileStep.zig2
-rw-r--r--lib/std/Build/RunStep.zig19
-rw-r--r--lib/std/Progress.zig2
-rw-r--r--lib/std/Thread.zig2
-rw-r--r--lib/std/array_hash_map.zig6
-rw-r--r--lib/std/array_list.zig48
-rw-r--r--lib/std/base64.zig4
-rw-r--r--lib/std/bit_set.zig4
-rw-r--r--lib/std/bounded_array.zig20
-rw-r--r--lib/std/buf_set.zig2
-rw-r--r--lib/std/child_process.zig6
-rw-r--r--lib/std/compress/deflate.zig14
-rw-r--r--lib/std/compress/deflate/compressor.zig25
-rw-r--r--lib/std/compress/deflate/decompressor.zig5
-rw-r--r--lib/std/compress/deflate/deflate_fast.zig6
-rw-r--r--lib/std/compress/deflate/deflate_fast_test.zig10
-rw-r--r--lib/std/compress/deflate/dict_decoder.zig10
-rw-r--r--lib/std/compress/deflate/huffman_code.zig2
-rw-r--r--lib/std/compress/deflate/mem_utils.zig15
-rw-r--r--lib/std/compress/lzma.zig6
-rw-r--r--lib/std/compress/lzma/decode/rangecoder.zig2
-rw-r--r--lib/std/compress/lzma/vec2d.zig4
-rw-r--r--lib/std/compress/xz/block.zig6
-rw-r--r--lib/std/compress/zstandard/decode/block.zig17
-rw-r--r--lib/std/crypto/25519/ed25519.zig18
-rw-r--r--lib/std/crypto/25519/edwards25519.zig8
-rw-r--r--lib/std/crypto/25519/scalar.zig6
-rw-r--r--lib/std/crypto/25519/x25519.zig14
-rw-r--r--lib/std/crypto/Certificate.zig14
-rw-r--r--lib/std/crypto/aegis.zig50
-rw-r--r--lib/std/crypto/aes_gcm.zig4
-rw-r--r--lib/std/crypto/aes_ocb.zig20
-rw-r--r--lib/std/crypto/argon2.zig16
-rw-r--r--lib/std/crypto/ascon.zig14
-rw-r--r--lib/std/crypto/bcrypt.zig6
-rw-r--r--lib/std/crypto/benchmark.zig4
-rw-r--r--lib/std/crypto/blake2.zig30
-rw-r--r--lib/std/crypto/blake3.zig8
-rw-r--r--lib/std/crypto/chacha20.zig8
-rw-r--r--lib/std/crypto/ecdsa.zig20
-rw-r--r--lib/std/crypto/hkdf.zig2
-rw-r--r--lib/std/crypto/hmac.zig8
-rw-r--r--lib/std/crypto/isap.zig2
-rw-r--r--lib/std/crypto/keccak_p.zig16
-rw-r--r--lib/std/crypto/kyber_d00.zig32
-rw-r--r--lib/std/crypto/md5.zig11
-rw-r--r--lib/std/crypto/modes.zig6
-rw-r--r--lib/std/crypto/pbkdf2.zig4
-rw-r--r--lib/std/crypto/pcurves/common.zig4
-rw-r--r--lib/std/crypto/pcurves/p256.zig6
-rw-r--r--lib/std/crypto/pcurves/p256/scalar.zig10
-rw-r--r--lib/std/crypto/pcurves/p384.zig6
-rw-r--r--lib/std/crypto/pcurves/p384/scalar.zig8
-rw-r--r--lib/std/crypto/pcurves/secp256k1.zig6
-rw-r--r--lib/std/crypto/pcurves/secp256k1/scalar.zig10
-rw-r--r--lib/std/crypto/phc_encoding.zig2
-rw-r--r--lib/std/crypto/salsa20.zig12
-rw-r--r--lib/std/crypto/scrypt.zig6
-rw-r--r--lib/std/crypto/sha1.zig8
-rw-r--r--lib/std/crypto/sha2.zig18
-rw-r--r--lib/std/crypto/sha3.zig4
-rw-r--r--lib/std/crypto/siphash.zig9
-rw-r--r--lib/std/crypto/tls.zig4
-rw-r--r--lib/std/crypto/tls/Client.zig30
-rw-r--r--lib/std/crypto/utils.zig12
-rw-r--r--lib/std/cstr.zig4
-rw-r--r--lib/std/debug.zig4
-rw-r--r--lib/std/dynamic_library.zig2
-rw-r--r--lib/std/enums.zig4
-rw-r--r--lib/std/fifo.zig26
-rw-r--r--lib/std/fmt/errol.zig4
-rw-r--r--lib/std/fs.zig34
-rw-r--r--lib/std/fs/path.zig12
-rw-r--r--lib/std/hash/cityhash.zig4
-rw-r--r--lib/std/hash/wyhash.zig5
-rw-r--r--lib/std/hash/xxhash.zig12
-rw-r--r--lib/std/heap/WasmAllocator.zig2
-rw-r--r--lib/std/heap/WasmPageAllocator.zig2
-rw-r--r--lib/std/heap/general_purpose_allocator.zig4
-rw-r--r--lib/std/http/Client.zig2
-rw-r--r--lib/std/http/Headers.zig3
-rw-r--r--lib/std/http/Server.zig2
-rw-r--r--lib/std/http/protocol.zig2
-rw-r--r--lib/std/io/buffered_reader.zig11
-rw-r--r--lib/std/io/buffered_writer.zig5
-rw-r--r--lib/std/io/fixed_buffer_stream.zig6
-rw-r--r--lib/std/io/writer.zig2
-rw-r--r--lib/std/json.zig4
-rw-r--r--lib/std/json/test.zig2
-rw-r--r--lib/std/math/big/int.zig64
-rw-r--r--lib/std/mem.zig29
-rw-r--r--lib/std/mem/Allocator.zig4
-rw-r--r--lib/std/multi_array_list.zig6
-rw-r--r--lib/std/net.zig28
-rw-r--r--lib/std/os.zig34
-rw-r--r--lib/std/os/linux/bpf.zig2
-rw-r--r--lib/std/os/linux/io_uring.zig10
-rw-r--r--lib/std/os/linux/tls.zig4
-rw-r--r--lib/std/os/test.zig2
-rw-r--r--lib/std/os/uefi/protocols/device_path_protocol.zig2
-rw-r--r--lib/std/os/windows.zig14
-rw-r--r--lib/std/process.zig2
-rw-r--r--lib/std/rand/ChaCha.zig13
-rw-r--r--lib/std/rand/Isaac64.zig4
-rw-r--r--lib/std/segmented_list.zig23
-rw-r--r--lib/std/sort.zig59
-rw-r--r--lib/std/tar.zig14
-rw-r--r--lib/std/target.zig4
-rw-r--r--lib/std/testing/failing_allocator.zig2
-rw-r--r--lib/std/tz.zig2
-rw-r--r--lib/std/zig/render.zig4
-rw-r--r--lib/std/zig/system/NativeTargetInfo.zig8
-rw-r--r--lib/std/zig/system/windows.zig4
-rw-r--r--src/AstGen.zig2
-rw-r--r--src/Autodoc.zig2
-rw-r--r--src/Compilation.zig6
-rw-r--r--src/Liveness.zig14
-rw-r--r--src/Sema.zig52
-rw-r--r--src/arch/aarch64/CodeGen.zig8
-rw-r--r--src/arch/arm/CodeGen.zig8
-rw-r--r--src/arch/riscv64/CodeGen.zig6
-rw-r--r--src/arch/sparc64/CodeGen.zig6
-rw-r--r--src/arch/x86_64/CodeGen.zig4
-rw-r--r--src/arch/x86_64/Encoding.zig8
-rw-r--r--src/arch/x86_64/abi.zig2
-rw-r--r--src/arch/x86_64/encoder.zig4
-rw-r--r--src/codegen.zig2
-rw-r--r--src/codegen/c.zig117
-rw-r--r--src/codegen/llvm.zig82
-rw-r--r--src/link/Coff.zig42
-rw-r--r--src/link/Dwarf.zig27
-rw-r--r--src/link/Elf.zig2
-rw-r--r--src/link/MachO.zig15
-rw-r--r--src/link/MachO/CodeSignature.zig2
-rw-r--r--src/link/MachO/Object.zig12
-rw-r--r--src/link/MachO/Trie.zig2
-rw-r--r--src/link/MachO/UnwindInfo.zig2
-rw-r--r--src/link/MachO/zld.zig20
-rw-r--r--src/link/Plan9.zig2
-rw-r--r--src/link/Wasm.zig11
-rw-r--r--src/objcopy.zig38
-rw-r--r--src/print_air.zig2
-rw-r--r--src/print_zir.zig2
-rw-r--r--src/translate_c.zig2
-rw-r--r--src/type.zig2
-rw-r--r--src/value.zig53
-rw-r--r--stage1/zig.h8
-rw-r--r--stage1/zig1.wasmbin2440105 -> 2454149 bytes
-rw-r--r--test/behavior.zig2
-rw-r--r--test/behavior/basic.zig90
-rw-r--r--test/behavior/memcpy.zig44
-rw-r--r--test/behavior/memset.zig146
155 files changed, 1188 insertions, 882 deletions
diff --git a/lib/compiler_rt/udivmodei4.zig b/lib/compiler_rt/udivmodei4.zig
index 1a18612015..354a926b81 100644
--- a/lib/compiler_rt/udivmodei4.zig
+++ b/lib/compiler_rt/udivmodei4.zig
@@ -29,8 +29,8 @@ inline fn limb_set(x: []u32, i: usize, v: u32) void {
// Uses Knuth's Algorithm D, 4.3.1, p. 272.
fn divmod(q: ?[]u32, r: ?[]u32, u: []const u32, v: []const u32) !void {
- if (q) |q_| std.mem.set(u32, q_[0..], 0);
- if (r) |r_| std.mem.set(u32, r_[0..], 0);
+ if (q) |q_| @memset(q_[0..], 0);
+ if (r) |r_| @memset(r_[0..], 0);
if (u.len == 0 or v.len == 0) return error.DivisionByZero;
@@ -44,7 +44,7 @@ fn divmod(q: ?[]u32, r: ?[]u32, u: []const u32, v: []const u32) !void {
}
if (n > m) {
- if (r) |r_| std.mem.copy(u32, r_[0..], u[0..]);
+ if (r) |r_| @memcpy(r_[0..u.len], u);
return;
}
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,
}
diff --git a/src/AstGen.zig b/src/AstGen.zig
index 6c9972bc95..aece3eafec 100644
--- a/src/AstGen.zig
+++ b/src/AstGen.zig
@@ -3604,7 +3604,7 @@ const WipMembers = struct {
fn appendToDeclSlice(self: *Self, data: []const u32) void {
assert(self.decls_end + data.len <= self.field_bits_start);
- mem.copy(u32, self.payload.items[self.decls_end..], data);
+ @memcpy(self.payload.items[self.decls_end..][0..data.len], data);
self.decls_end += @intCast(u32, data.len);
}
diff --git a/src/Autodoc.zig b/src/Autodoc.zig
index 91e252a3f8..467ff55994 100644
--- a/src/Autodoc.zig
+++ b/src/Autodoc.zig
@@ -1146,7 +1146,7 @@ fn walkInstruction(
const limb_bytes = file.zir.string_bytes[str.start..][0..byte_count];
var limbs = try self.arena.alloc(std.math.big.Limb, str.len);
- std.mem.copy(u8, std.mem.sliceAsBytes(limbs), limb_bytes);
+ @memcpy(std.mem.sliceAsBytes(limbs)[0..limb_bytes.len], limb_bytes);
const big_int = std.math.big.int.Const{
.limbs = limbs,
diff --git a/src/Compilation.zig b/src/Compilation.zig
index f11f158e1b..a5b785cc67 100644
--- a/src/Compilation.zig
+++ b/src/Compilation.zig
@@ -2165,7 +2165,7 @@ fn wholeCacheModeSetBinFilePath(comp: *Compilation, digest: *const [Cache.hex_di
const digest_start = 2; // "o/[digest]/[basename]"
if (comp.whole_bin_sub_path) |sub_path| {
- mem.copy(u8, sub_path[digest_start..], digest);
+ @memcpy(sub_path[digest_start..][0..digest.len], digest);
comp.bin_file.options.emit = .{
.directory = comp.local_cache_directory,
@@ -2174,7 +2174,7 @@ fn wholeCacheModeSetBinFilePath(comp: *Compilation, digest: *const [Cache.hex_di
}
if (comp.whole_implib_sub_path) |sub_path| {
- mem.copy(u8, sub_path[digest_start..], digest);
+ @memcpy(sub_path[digest_start..][0..digest.len], digest);
comp.bin_file.options.implib_emit = .{
.directory = comp.local_cache_directory,
@@ -4432,7 +4432,7 @@ pub fn addCCArgs(
assert(prefix.len == prefix_len);
var march_buf: [prefix_len + letters.len + 1]u8 = undefined;
var march_index: usize = prefix_len;
- mem.copy(u8, &march_buf, prefix);
+ @memcpy(march_buf[0..prefix.len], prefix);
if (std.Target.riscv.featureSetHas(target.cpu.features, .e)) {
march_buf[march_index] = 'e';
diff --git a/src/Liveness.zig b/src/Liveness.zig
index 6990ade327..a1bfb73e2a 100644
--- a/src/Liveness.zig
+++ b/src/Liveness.zig
@@ -156,7 +156,7 @@ pub fn analyze(gpa: Allocator, air: Air) Allocator.Error!Liveness {
errdefer a.special.deinit(gpa);
defer a.extra.deinit(gpa);
- std.mem.set(usize, a.tomb_bits, 0);
+ @memset(a.tomb_bits, 0);
const main_body = air.getMainBody();
@@ -1150,7 +1150,7 @@ fn analyzeInst(
if (args.len + 1 <= bpi - 1) {
var buf = [1]Air.Inst.Ref{.none} ** (bpi - 1);
buf[0] = callee;
- std.mem.copy(Air.Inst.Ref, buf[1..], args);
+ @memcpy(buf[1..][0..args.len], args);
return analyzeOperands(a, pass, data, inst, buf);
}
@@ -1189,7 +1189,7 @@ fn analyzeInst(
if (elements.len <= bpi - 1) {
var buf = [1]Air.Inst.Ref{.none} ** (bpi - 1);
- std.mem.copy(Air.Inst.Ref, &buf, elements);
+ @memcpy(buf[0..elements.len], elements);
return analyzeOperands(a, pass, data, inst, buf);
}
@@ -1255,7 +1255,7 @@ fn analyzeInst(
if (buf_index + inputs.len > buf.len) {
break :simple buf_index + inputs.len;
}
- std.mem.copy(Air.Inst.Ref, buf[buf_index..], inputs);
+ @memcpy(buf[buf_index..][0..inputs.len], inputs);
return analyzeOperands(a, pass, data, inst, buf);
};
@@ -1841,7 +1841,7 @@ fn analyzeInstSwitchBr(
var case_infos = try gpa.alloc(ControlBranchInfo, ncases + 1); // +1 for else
defer gpa.free(case_infos);
- std.mem.set(ControlBranchInfo, case_infos, .{});
+ @memset(case_infos, .{});
defer for (case_infos) |*info| {
info.branch_deaths.deinit(gpa);
info.live_set.deinit(gpa);
@@ -1898,7 +1898,7 @@ fn analyzeInstSwitchBr(
const mirrored_deaths = try gpa.alloc(DeathList, ncases + 1);
defer gpa.free(mirrored_deaths);
- std.mem.set(DeathList, mirrored_deaths, .{});
+ @memset(mirrored_deaths, .{});
defer for (mirrored_deaths) |*md| md.deinit(gpa);
{
@@ -1993,7 +1993,7 @@ fn AnalyzeBigOperands(comptime pass: LivenessPass) type {
};
errdefer a.gpa.free(extra_tombs);
- std.mem.set(u32, extra_tombs, 0);
+ @memset(extra_tombs, 0);
const will_die_immediately: bool = switch (pass) {
.loop_analysis => false, // track everything, since we don't have full liveness information yet
diff --git a/src/Sema.zig b/src/Sema.zig
index 8b47f1877b..327ff3800f 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -206,9 +206,9 @@ pub const InstMap = struct {
const start_diff = old_start - better_start;
const new_items = try allocator.alloc(Air.Inst.Ref, better_capacity);
- mem.set(Air.Inst.Ref, new_items[0..start_diff], .none);
- mem.copy(Air.Inst.Ref, new_items[start_diff..], map.items);
- mem.set(Air.Inst.Ref, new_items[start_diff + map.items.len ..], .none);
+ @memset(new_items[0..start_diff], .none);
+ @memcpy(new_items[start_diff..][0..map.items.len], map.items);
+ @memset(new_items[start_diff + map.items.len ..], .none);
allocator.free(map.items);
map.items = new_items;
@@ -4307,7 +4307,7 @@ fn validateStructInit(
// Maps field index to field_ptr index of where it was already initialized.
const found_fields = try gpa.alloc(Zir.Inst.Index, struct_ty.structFieldCount());
defer gpa.free(found_fields);
- mem.set(Zir.Inst.Index, found_fields, 0);
+ @memset(found_fields, 0);
var struct_ptr_zir_ref: Zir.Inst.Ref = undefined;
@@ -5113,7 +5113,7 @@ fn zirIntBig(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
const byte_count = int.len * @sizeOf(std.math.big.Limb);
const limb_bytes = sema.code.string_bytes[int.start..][0..byte_count];
const limbs = try arena.alloc(std.math.big.Limb, int.len);
- mem.copy(u8, mem.sliceAsBytes(limbs), limb_bytes);
+ @memcpy(mem.sliceAsBytes(limbs), limb_bytes);
return sema.addConstant(
Type.initTag(.comptime_int),
@@ -5967,7 +5967,7 @@ fn addDbgVar(
const elements_used = name.len / 4 + 1;
try sema.air_extra.ensureUnusedCapacity(sema.gpa, elements_used);
const buffer = mem.sliceAsBytes(sema.air_extra.unusedCapacitySlice());
- mem.copy(u8, buffer, name);
+ @memcpy(buffer[0..name.len], name);
buffer[name.len] = 0;
sema.air_extra.items.len += elements_used;
@@ -10354,7 +10354,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
.Enum => {
seen_enum_fields = try gpa.alloc(?Module.SwitchProngSrc, operand_ty.enumFieldCount());
empty_enum = seen_enum_fields.len == 0 and !operand_ty.isNonexhaustiveEnum();
- mem.set(?Module.SwitchProngSrc, seen_enum_fields, null);
+ @memset(seen_enum_fields, null);
// `range_set` is used for non-exhaustive enum values that do not correspond to any tags.
var extra_index: usize = special.end;
@@ -12809,8 +12809,8 @@ fn analyzeTupleMul(
}
i = 0;
while (i < factor) : (i += 1) {
- mem.copy(Type, types[tuple_len * i ..], types[0..tuple_len]);
- mem.copy(Value, values[tuple_len * i ..], values[0..tuple_len]);
+ mem.copyForwards(Type, types[tuple_len * i ..], types[0..tuple_len]);
+ mem.copyForwards(Value, values[tuple_len * i ..], values[0..tuple_len]);
}
break :rs runtime_src;
};
@@ -12835,7 +12835,7 @@ fn analyzeTupleMul(
}
i = 1;
while (i < factor) : (i += 1) {
- mem.copy(Air.Inst.Ref, element_refs[tuple_len * i ..], element_refs[0..tuple_len]);
+ @memcpy(element_refs[tuple_len * i ..][0..tuple_len], element_refs[0..tuple_len]);
}
return block.addAggregateInit(tuple_ty, element_refs);
@@ -15057,29 +15057,29 @@ fn zirAsm(
sema.appendRefsAssumeCapacity(args);
for (outputs) |o| {
const buffer = mem.sliceAsBytes(sema.air_extra.unusedCapacitySlice());
- mem.copy(u8, buffer, o.c);
+ @memcpy(buffer[0..o.c.len], o.c);
buffer[o.c.len] = 0;
- mem.copy(u8, buffer[o.c.len + 1 ..], o.n);
+ @memcpy(buffer[o.c.len + 1 ..][0..o.n.len], o.n);
buffer[o.c.len + 1 + o.n.len] = 0;
sema.air_extra.items.len += (o.c.len + o.n.len + (2 + 3)) / 4;
}
for (inputs) |input| {
const buffer = mem.sliceAsBytes(sema.air_extra.unusedCapacitySlice());
- mem.copy(u8, buffer, input.c);
+ @memcpy(buffer[0..input.c.len], input.c);
buffer[input.c.len] = 0;
- mem.copy(u8, buffer[input.c.len + 1 ..], input.n);
+ @memcpy(buffer[input.c.len + 1 ..][0..input.n.len], input.n);
buffer[input.c.len + 1 + input.n.len] = 0;
sema.air_extra.items.len += (input.c.len + input.n.len + (2 + 3)) / 4;
}
for (clobbers) |clobber| {
const buffer = mem.sliceAsBytes(sema.air_extra.unusedCapacitySlice());
- mem.copy(u8, buffer, clobber);
+ @memcpy(buffer[0..clobber.len], clobber);
buffer[clobber.len] = 0;
sema.air_extra.items.len += clobber.len / 4 + 1;
}
{
const buffer = mem.sliceAsBytes(sema.air_extra.unusedCapacitySlice());
- mem.copy(u8, buffer, asm_source);
+ @memcpy(buffer[0..asm_source.len], asm_source);
sema.air_extra.items.len += (asm_source.len + 3) / 4;
}
return asm_air;
@@ -17582,7 +17582,7 @@ fn structInitEmpty(
// The init values to use for the struct instance.
const field_inits = try gpa.alloc(Air.Inst.Ref, struct_ty.structFieldCount());
defer gpa.free(field_inits);
- mem.set(Air.Inst.Ref, field_inits, .none);
+ @memset(field_inits, .none);
return sema.finishStructInit(block, init_src, dest_src, field_inits, struct_ty, false);
}
@@ -17675,7 +17675,7 @@ fn zirStructInit(
// The init values to use for the struct instance.
const field_inits = try gpa.alloc(Air.Inst.Ref, resolved_ty.structFieldCount());
defer gpa.free(field_inits);
- mem.set(Air.Inst.Ref, field_inits, .none);
+ @memset(field_inits, .none);
var field_i: u32 = 0;
var extra_index = extra.end;
@@ -22039,7 +22039,7 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
if (try sema.resolveMaybeUndefVal(uncoerced_elem)) |_| {
for (0..len) |i| {
const elem_index = try sema.addIntUnsigned(Type.usize, i);
- const elem_ptr = try sema.elemPtr(
+ const elem_ptr = try sema.elemPtrOneLayerOnly(
block,
src,
dest_ptr,
@@ -26953,9 +26953,13 @@ fn storePtrVal(
defer sema.gpa.free(buffer);
reinterpret.val_ptr.*.writeToMemory(mut_kit.ty, sema.mod, buffer) catch |err| switch (err) {
error.ReinterpretDeclRef => unreachable,
+ error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already
+ error.Unimplemented => return sema.fail(block, src, "TODO: implement writeToMemory for type '{}'", .{mut_kit.ty.fmt(sema.mod)}),
};
operand_val.writeToMemory(operand_ty, sema.mod, buffer[reinterpret.byte_offset..]) catch |err| switch (err) {
error.ReinterpretDeclRef => unreachable,
+ error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already
+ error.Unimplemented => return sema.fail(block, src, "TODO: implement writeToMemory for type '{}'", .{mut_kit.ty.fmt(sema.mod)}),
};
const arena = mut_kit.beginArena(sema.mod);
@@ -27075,7 +27079,7 @@ fn beginComptimePtrMutation(
const array_len_including_sentinel =
try sema.usizeCast(block, src, parent.ty.arrayLenIncludingSentinel());
const elems = try arena.alloc(Value, array_len_including_sentinel);
- mem.set(Value, elems, Value.undef);
+ @memset(elems, Value.undef);
val_ptr.* = try Value.Tag.aggregate.create(arena, elems);
@@ -27273,7 +27277,7 @@ fn beginComptimePtrMutation(
switch (parent.ty.zigTypeTag()) {
.Struct => {
const fields = try arena.alloc(Value, parent.ty.structFieldCount());
- mem.set(Value, fields, Value.undef);
+ @memset(fields, Value.undef);
val_ptr.* = try Value.Tag.aggregate.create(arena, fields);
@@ -27905,6 +27909,8 @@ fn bitCastVal(
defer sema.gpa.free(buffer);
val.writeToMemory(old_ty, sema.mod, buffer) catch |err| switch (err) {
error.ReinterpretDeclRef => return null,
+ error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already
+ error.Unimplemented => return sema.fail(block, src, "TODO: implement writeToMemory for type '{}'", .{old_ty.fmt(sema.mod)}),
};
return try Value.readFromMemory(new_ty, sema.mod, buffer[buffer_offset..], sema.arena);
}
@@ -28419,7 +28425,7 @@ fn coerceTupleToStruct(
const fields = struct_ty.structFields();
const field_vals = try sema.arena.alloc(Value, fields.count());
const field_refs = try sema.arena.alloc(Air.Inst.Ref, field_vals.len);
- mem.set(Air.Inst.Ref, field_refs, .none);
+ @memset(field_refs, .none);
const inst_ty = sema.typeOf(inst);
var runtime_src: ?LazySrcLoc = null;
@@ -28508,7 +28514,7 @@ fn coerceTupleToTuple(
const dest_field_count = tuple_ty.structFieldCount();
const field_vals = try sema.arena.alloc(Value, dest_field_count);
const field_refs = try sema.arena.alloc(Air.Inst.Ref, field_vals.len);
- mem.set(Air.Inst.Ref, field_refs, .none);
+ @memset(field_refs, .none);
const inst_ty = sema.typeOf(inst);
const inst_field_count = inst_ty.structFieldCount();
diff --git a/src/arch/aarch64/CodeGen.zig b/src/arch/aarch64/CodeGen.zig
index 948dad73b9..649edd3b9c 100644
--- a/src/arch/aarch64/CodeGen.zig
+++ b/src/arch/aarch64/CodeGen.zig
@@ -1630,7 +1630,7 @@ fn allocRegs(
const read_locks = locks[0..read_args.len];
const write_locks = locks[read_args.len..];
- std.mem.set(?RegisterLock, locks, null);
+ @memset(locks, null);
defer for (locks) |lock| {
if (lock) |locked_reg| self.register_manager.unlockReg(locked_reg);
};
@@ -4395,7 +4395,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
if (args.len + 1 <= Liveness.bpi - 1) {
var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1);
buf[0] = callee;
- std.mem.copy(Air.Inst.Ref, buf[1..], args);
+ @memcpy(buf[1..][0..args.len], args);
return self.finishAir(inst, result, buf);
}
var bt = try self.iterateBigTomb(inst, 1 + args.len);
@@ -5348,7 +5348,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
buf_index += 1;
}
if (buf_index + inputs.len > buf.len) break :simple;
- std.mem.copy(Air.Inst.Ref, buf[buf_index..], inputs);
+ @memcpy(buf[buf_index..][0..inputs.len], inputs);
return self.finishAir(inst, result, buf);
}
var bt = try self.iterateBigTomb(inst, outputs.len + inputs.len);
@@ -6055,7 +6055,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void {
if (elements.len <= Liveness.bpi - 1) {
var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1);
- std.mem.copy(Air.Inst.Ref, &buf, elements);
+ @memcpy(buf[0..elements.len], elements);
return self.finishAir(inst, result, buf);
}
var bt = try self.iterateBigTomb(inst, elements.len);
diff --git a/src/arch/arm/CodeGen.zig b/src/arch/arm/CodeGen.zig
index 3676b2a865..5353b78e4d 100644
--- a/src/arch/arm/CodeGen.zig
+++ b/src/arch/arm/CodeGen.zig
@@ -3114,7 +3114,7 @@ fn allocRegs(
const read_locks = locks[0..read_args.len];
const write_locks = locks[read_args.len..];
- std.mem.set(?RegisterLock, locks, null);
+ @memset(locks, null);
defer for (locks) |lock| {
if (lock) |locked_reg| self.register_manager.unlockReg(locked_reg);
};
@@ -4341,7 +4341,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
if (args.len <= Liveness.bpi - 2) {
var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1);
buf[0] = callee;
- std.mem.copy(Air.Inst.Ref, buf[1..], args);
+ @memcpy(buf[1..][0..args.len], args);
return self.finishAir(inst, result, buf);
}
var bt = try self.iterateBigTomb(inst, 1 + args.len);
@@ -5263,7 +5263,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
buf_index += 1;
}
if (buf_index + inputs.len > buf.len) break :simple;
- std.mem.copy(Air.Inst.Ref, buf[buf_index..], inputs);
+ @memcpy(buf[buf_index..][0..inputs.len], inputs);
return self.finishAir(inst, result, buf);
}
var bt = try self.iterateBigTomb(inst, outputs.len + inputs.len);
@@ -6000,7 +6000,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void {
if (elements.len <= Liveness.bpi - 1) {
var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1);
- std.mem.copy(Air.Inst.Ref, &buf, elements);
+ @memcpy(buf[0..elements.len], elements);
return self.finishAir(inst, result, buf);
}
var bt = try self.iterateBigTomb(inst, elements.len);
diff --git a/src/arch/riscv64/CodeGen.zig b/src/arch/riscv64/CodeGen.zig
index a0ebc1becc..d4c7eb0c70 100644
--- a/src/arch/riscv64/CodeGen.zig
+++ b/src/arch/riscv64/CodeGen.zig
@@ -1784,7 +1784,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
if (args.len <= Liveness.bpi - 2) {
var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1);
buf[0] = callee;
- std.mem.copy(Air.Inst.Ref, buf[1..], args);
+ @memcpy(buf[1..][0..args.len], args);
return self.finishAir(inst, result, buf);
}
var bt = try self.iterateBigTomb(inst, 1 + args.len);
@@ -2225,7 +2225,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
buf_index += 1;
}
if (buf_index + inputs.len > buf.len) break :simple;
- std.mem.copy(Air.Inst.Ref, buf[buf_index..], inputs);
+ @memcpy(buf[buf_index..][0..inputs.len], inputs);
return self.finishAir(inst, result, buf);
}
var bt = try self.iterateBigTomb(inst, outputs.len + inputs.len);
@@ -2500,7 +2500,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void {
if (elements.len <= Liveness.bpi - 1) {
var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1);
- std.mem.copy(Air.Inst.Ref, &buf, elements);
+ @memcpy(buf[0..elements.len], elements);
return self.finishAir(inst, result, buf);
}
var bt = try self.iterateBigTomb(inst, elements.len);
diff --git a/src/arch/sparc64/CodeGen.zig b/src/arch/sparc64/CodeGen.zig
index cc5c9e9832..2686852bab 100644
--- a/src/arch/sparc64/CodeGen.zig
+++ b/src/arch/sparc64/CodeGen.zig
@@ -843,7 +843,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void {
if (elements.len <= Liveness.bpi - 1) {
var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1);
- std.mem.copy(Air.Inst.Ref, &buf, elements);
+ @memcpy(buf[0..elements.len], elements);
return self.finishAir(inst, result, buf);
}
var bt = try self.iterateBigTomb(inst, elements.len);
@@ -987,7 +987,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
buf_index += 1;
}
if (buf_index + inputs.len > buf.len) break :simple;
- std.mem.copy(Air.Inst.Ref, buf[buf_index..], inputs);
+ @memcpy(buf[buf_index..][0..inputs.len], inputs);
return self.finishAir(inst, result, buf);
}
@@ -1314,7 +1314,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
if (args.len + 1 <= Liveness.bpi - 1) {
var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1);
buf[0] = callee;
- std.mem.copy(Air.Inst.Ref, buf[1..], args);
+ @memcpy(buf[1..][0..args.len], args);
return self.finishAir(inst, result, buf);
}
diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig
index df0db882ba..be972d7aea 100644
--- a/src/arch/x86_64/CodeGen.zig
+++ b/src/arch/x86_64/CodeGen.zig
@@ -7117,7 +7117,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
buf_index += 1;
}
if (buf_index + inputs.len > buf.len) break :simple;
- std.mem.copy(Air.Inst.Ref, buf[buf_index..], inputs);
+ @memcpy(buf[buf_index..][0..inputs.len], inputs);
return self.finishAir(inst, result, buf);
}
var bt = self.liveness.iterateBigTomb(inst);
@@ -8505,7 +8505,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void {
if (elements.len <= Liveness.bpi - 1) {
var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1);
- std.mem.copy(Air.Inst.Ref, &buf, elements);
+ @memcpy(buf[0..elements.len], elements);
return self.finishAir(inst, result, buf);
}
var bt = self.liveness.iterateBigTomb(inst);
diff --git a/src/arch/x86_64/Encoding.zig b/src/arch/x86_64/Encoding.zig
index 21899b912b..a977af7842 100644
--- a/src/arch/x86_64/Encoding.zig
+++ b/src/arch/x86_64/Encoding.zig
@@ -546,7 +546,7 @@ fn estimateInstructionLength(prefix: Prefix, encoding: Encoding, ops: []const Op
.encoding = encoding,
.ops = [1]Operand{.none} ** 4,
};
- std.mem.copy(Operand, &inst.ops, ops);
+ @memcpy(inst.ops[0..ops.len], ops);
var cwriter = std.io.countingWriter(std.io.null_writer);
inst.encode(cwriter.writer(), .{ .allow_frame_loc = true }) catch unreachable; // Not allowed to fail here unless OOM.
@@ -575,8 +575,10 @@ const mnemonic_to_encodings_map = init: {
.modrm_ext = entry[4],
.mode = entry[5],
};
- std.mem.copy(Op, &data.ops, entry[2]);
- std.mem.copy(u8, &data.opc, entry[3]);
+ // TODO: use `@memcpy` for these. When I did that, I got a false positive
+ // compile error for this copy happening at compile time.
+ std.mem.copyForwards(Op, &data.ops, entry[2]);
+ std.mem.copyForwards(u8, &data.opc, entry[3]);
while (mnemonic_int < @enumToInt(entry[0])) : (mnemonic_int += 1) {
mnemonic_map[mnemonic_int] = data_storage[mnemonic_start..data_index];
diff --git a/src/arch/x86_64/abi.zig b/src/arch/x86_64/abi.zig
index e9da09b999..ff1a0ee520 100644
--- a/src/arch/x86_64/abi.zig
+++ b/src/arch/x86_64/abi.zig
@@ -321,7 +321,7 @@ pub fn classifySystemV(ty: Type, target: Target, ctx: Context) [8]Class {
byte_i = 0;
result_i += 1;
}
- std.mem.copy(Class, result[result_i..], field_class);
+ @memcpy(result[result_i..][0..field_class.len], field_class);
result_i += field_class.len;
// If there are any bytes leftover, we have to try to combine
// the next field with them.
diff --git a/src/arch/x86_64/encoder.zig b/src/arch/x86_64/encoder.zig
index 73b40ea3be..329dfca924 100644
--- a/src/arch/x86_64/encoder.zig
+++ b/src/arch/x86_64/encoder.zig
@@ -182,7 +182,7 @@ pub const Instruction = struct {
.encoding = encoding,
.ops = [1]Operand{.none} ** 4,
};
- std.mem.copy(Operand, &inst.ops, ops);
+ @memcpy(inst.ops[0..ops.len], ops);
return inst;
}
@@ -859,7 +859,7 @@ fn expectEqualHexStrings(expected: []const u8, given: []const u8, assembly: []co
const idx = std.mem.indexOfDiff(u8, expected_fmt, given_fmt).?;
var padding = try testing.allocator.alloc(u8, idx + 5);
defer testing.allocator.free(padding);
- std.mem.set(u8, padding, ' ');
+ @memset(padding, ' ');
std.debug.print("\nASM: {s}\nEXP: {s}\nGIV: {s}\n{s}^ -- first differing byte\n", .{
assembly,
expected_fmt,
diff --git a/src/codegen.zig b/src/codegen.zig
index dbcd76118a..bf80a90cc3 100644
--- a/src/codegen.zig
+++ b/src/codegen.zig
@@ -552,7 +552,7 @@ pub fn generateSymbol(
.ty = field_ty,
.val = field_val,
}, &tmp_list, debug_output, reloc_info)) {
- .ok => mem.copy(u8, code.items[current_pos..], tmp_list.items),
+ .ok => @memcpy(code.items[current_pos..][0..tmp_list.items.len], tmp_list.items),
.fail => |em| return Result{ .fail = em },
}
} else {
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index 385094e495..f69cec960e 100644
--- a/src/codegen/c.zig
+++ b/src/codegen/c.zig
@@ -2411,9 +2411,9 @@ pub fn genErrDecls(o: *Object) !void {
const name_buf = try o.dg.gpa.alloc(u8, name_prefix.len + max_name_len);
defer o.dg.gpa.free(name_buf);
- mem.copy(u8, name_buf, name_prefix);
+ @memcpy(name_buf[0..name_prefix.len], name_prefix);
for (o.dg.module.error_name_list.items) |name| {
- mem.copy(u8, name_buf[name_prefix.len..], name);
+ @memcpy(name_buf[name_prefix.len..][0..name.len], name);
const identifier = name_buf[0 .. name_prefix.len + name.len];
var name_ty_pl = Type.Payload.Len{ .base = .{ .tag = .array_u8_sentinel_0 }, .data = name.len };
@@ -3858,7 +3858,7 @@ fn airCmpOp(
try reap(f, inst, &.{ data.lhs, data.rhs });
const rhs_ty = f.air.typeOf(data.rhs);
- const need_cast = lhs_ty.isSinglePointer() != rhs_ty.isSinglePointer();
+ const need_cast = lhs_ty.isSinglePointer() or rhs_ty.isSinglePointer();
const writer = f.object.writer();
const local = try f.allocLocal(inst, inst_ty);
const v = try Vectorize.start(f, inst, writer, lhs_ty);
@@ -4419,51 +4419,94 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {
const dest_ty = f.air.typeOfIndex(inst);
const operand = try f.resolveInst(ty_op.operand);
- try reap(f, inst, &.{ty_op.operand});
const operand_ty = f.air.typeOf(ty_op.operand);
- const target = f.object.dg.module.getTarget();
- const writer = f.object.writer();
- const local = try f.allocLocal(inst, dest_ty);
+ const bitcasted = try bitcast(f, dest_ty, operand, operand_ty);
+ try reap(f, inst, &.{ty_op.operand});
+ return bitcasted.move(f, inst, dest_ty);
+}
+
+const LocalResult = struct {
+ c_value: CValue,
+ need_free: bool,
+
+ fn move(lr: LocalResult, f: *Function, inst: Air.Inst.Index, dest_ty: Type) !CValue {
+ if (lr.need_free) {
+ // Move the freshly allocated local to be owned by this instruction,
+ // by returning it here instead of freeing it.
+ return lr.c_value;
+ }
+
+ const local = try f.allocLocal(inst, dest_ty);
+ try lr.free(f);
+ const writer = f.object.writer();
+ try f.writeCValue(writer, local, .Other);
+ if (dest_ty.isAbiInt()) {
+ try writer.writeAll(" = ");
+ } else {
+ try writer.writeAll(" = (");
+ try f.renderType(writer, dest_ty);
+ try writer.writeByte(')');
+ }
+ try f.writeCValue(writer, lr.c_value, .Initializer);
+ try writer.writeAll(";\n");
+ return local;
+ }
+
+ fn free(lr: LocalResult, f: *Function) !void {
+ if (lr.need_free) {
+ try freeLocal(f, 0, lr.c_value.new_local, 0);
+ }
+ }
+};
- // If the assignment looks like 'x = x', we don't need it
- const can_elide = operand == .local and operand.local == local.new_local;
+fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !LocalResult {
+ const target = f.object.dg.module.getTarget();
+ const writer = f.object.writer();
if (operand_ty.isAbiInt() and dest_ty.isAbiInt()) {
- if (can_elide) return local;
const src_info = dest_ty.intInfo(target);
const dest_info = operand_ty.intInfo(target);
if (src_info.signedness == dest_info.signedness and
src_info.bits == dest_info.bits)
{
- try f.writeCValue(writer, local, .Other);
- try writer.writeAll(" = ");
- try f.writeCValue(writer, operand, .Initializer);
- try writer.writeAll(";\n");
- return local;
+ return .{
+ .c_value = operand,
+ .need_free = false,
+ };
}
}
if (dest_ty.isPtrAtRuntime() and operand_ty.isPtrAtRuntime()) {
- if (can_elide) return local;
+ const local = try f.allocLocal(0, dest_ty);
try f.writeCValue(writer, local, .Other);
try writer.writeAll(" = (");
try f.renderType(writer, dest_ty);
try writer.writeByte(')');
try f.writeCValue(writer, operand, .Other);
try writer.writeAll(";\n");
- return local;
+ return .{
+ .c_value = local,
+ .need_free = true,
+ };
}
const operand_lval = if (operand == .constant) blk: {
- const operand_local = try f.allocLocal(inst, operand_ty);
+ const operand_local = try f.allocLocal(0, operand_ty);
try f.writeCValue(writer, operand_local, .Other);
- try writer.writeAll(" = ");
+ if (operand_ty.isAbiInt()) {
+ try writer.writeAll(" = ");
+ } else {
+ try writer.writeAll(" = (");
+ try f.renderType(writer, operand_ty);
+ try writer.writeByte(')');
+ }
try f.writeCValue(writer, operand, .Initializer);
try writer.writeAll(";\n");
break :blk operand_local;
} else operand;
+ const local = try f.allocLocal(0, dest_ty);
try writer.writeAll("memcpy(&");
try f.writeCValue(writer, local, .Other);
try writer.writeAll(", &");
@@ -4528,10 +4571,13 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {
}
if (operand == .constant) {
- try freeLocal(f, inst, operand_lval.new_local, 0);
+ try freeLocal(f, 0, operand_lval.new_local, 0);
}
- return local;
+ return .{
+ .c_value = local,
+ .need_free = true,
+ };
}
fn airTrap(writer: anytype) !CValue {
@@ -4831,7 +4877,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
const literal = mem.sliceTo(asm_source[src_i..], '%');
src_i += literal.len;
- mem.copy(u8, fixed_asm_source[dst_i..], literal);
+ @memcpy(fixed_asm_source[dst_i..][0..literal.len], literal);
dst_i += literal.len;
if (src_i >= asm_source.len) break;
@@ -4856,9 +4902,9 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
const name = desc[0..colon];
const modifier = desc[colon + 1 ..];
- mem.copy(u8, fixed_asm_source[dst_i..], modifier);
+ @memcpy(fixed_asm_source[dst_i..][0..modifier.len], modifier);
dst_i += modifier.len;
- mem.copy(u8, fixed_asm_source[dst_i..], name);
+ @memcpy(fixed_asm_source[dst_i..][0..name.len], name);
dst_i += name.len;
src_i += desc.len;
@@ -6288,15 +6334,19 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
}
try writer.writeAll("; ++");
try f.writeCValue(writer, index, .Other);
- try writer.writeAll(") ((");
+ try writer.writeAll(") ");
+
+ const a = try Assignment.start(f, writer, elem_ty);
+ try writer.writeAll("((");
try f.renderType(writer, elem_ptr_ty);
try writer.writeByte(')');
try writeSliceOrPtr(f, writer, dest_slice, dest_ty);
try writer.writeAll(")[");
try f.writeCValue(writer, index, .Other);
- try writer.writeAll("] = ");
- try f.writeCValue(writer, value, .FunctionArgument);
- try writer.writeAll(";\n");
+ try writer.writeByte(']');
+ try a.assign(f, writer);
+ try f.writeCValue(writer, value, .Other);
+ try a.end(f, writer);
try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
try freeLocal(f, inst, index.new_local, 0);
@@ -6304,12 +6354,14 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
return .none;
}
+ const bitcasted = try bitcast(f, Type.u8, value, elem_ty);
+
try writer.writeAll("memset(");
switch (dest_ty.ptrSize()) {
.Slice => {
try f.writeCValueMember(writer, dest_slice, .{ .identifier = "ptr" });
try writer.writeAll(", ");
- try f.writeCValue(writer, value, .FunctionArgument);
+ try f.writeCValue(writer, bitcasted.c_value, .FunctionArgument);
try writer.writeAll(", ");
try f.writeCValueMember(writer, dest_slice, .{ .identifier = "len" });
try writer.writeAll(");\n");
@@ -6320,11 +6372,12 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
try f.writeCValue(writer, dest_slice, .FunctionArgument);
try writer.writeAll(", ");
- try f.writeCValue(writer, value, .FunctionArgument);
+ try f.writeCValue(writer, bitcasted.c_value, .FunctionArgument);
try writer.print(", {d});\n", .{len});
},
.Many, .C => unreachable,
}
+ try bitcasted.free(f);
try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
return .none;
}
@@ -7394,7 +7447,7 @@ fn formatIntLiteral(
var int_buf: Value.BigIntSpace = undefined;
const int = if (data.val.isUndefDeep()) blk: {
undef_limbs = try allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(data.int_info.bits));
- mem.set(BigIntLimb, undef_limbs, undefPattern(BigIntLimb));
+ @memset(undef_limbs, undefPattern(BigIntLimb));
var undef_int = BigInt.Mutable{
.limbs = undef_limbs,
@@ -7489,7 +7542,7 @@ fn formatIntLiteral(
} else {
try data.cty.renderLiteralPrefix(writer, data.kind);
wrap.convertToTwosComplement(int, data.int_info.signedness, c_bits);
- mem.set(BigIntLimb, wrap.limbs[wrap.len..], 0);
+ @memset(wrap.limbs[wrap.len..], 0);
wrap.len = wrap.limbs.len;
const limbs_per_c_limb = @divExact(wrap.len, c_limb_info.count);
diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig
index 94f49e801d..d697a41988 100644
--- a/src/codegen/llvm.zig
+++ b/src/codegen/llvm.zig
@@ -7939,11 +7939,15 @@ pub const FuncGen = struct {
return self.builder.buildPtrToInt(operand_ptr, dest_llvm_ty, "");
}
- fn airBitCast(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value {
+ fn airBitCast(self: *FuncGen, inst: Air.Inst.Index) !*llvm.Value {
const ty_op = self.air.instructions.items(.data)[inst].ty_op;
const operand_ty = self.air.typeOf(ty_op.operand);
const inst_ty = self.air.typeOfIndex(inst);
const operand = try self.resolveInst(ty_op.operand);
+ return self.bitCast(operand, operand_ty, inst_ty);
+ }
+
+ fn bitCast(self: *FuncGen, operand: *llvm.Value, operand_ty: Type, inst_ty: Type) !*llvm.Value {
const operand_is_ref = isByRef(operand_ty);
const result_is_ref = isByRef(inst_ty);
const llvm_dest_ty = try self.dg.lowerType(inst_ty);
@@ -7954,6 +7958,12 @@ pub const FuncGen = struct {
return operand;
}
+ if (llvm_dest_ty.getTypeKind() == .Integer and
+ operand.typeOf().getTypeKind() == .Integer)
+ {
+ return self.builder.buildZExtOrBitCast(operand, llvm_dest_ty, "");
+ }
+
if (operand_ty.zigTypeTag() == .Int and inst_ty.isPtrAtRuntime()) {
return self.builder.buildIntToPtr(operand, llvm_dest_ty, "");
}
@@ -8414,27 +8424,45 @@ pub const FuncGen = struct {
const dest_slice = try self.resolveInst(bin_op.lhs);
const ptr_ty = self.air.typeOf(bin_op.lhs);
const elem_ty = self.air.typeOf(bin_op.rhs);
- const target = self.dg.module.getTarget();
- const val_is_undef = if (self.air.value(bin_op.rhs)) |val| val.isUndefDeep() else false;
+ const module = self.dg.module;
+ const target = module.getTarget();
const dest_ptr_align = ptr_ty.ptrAlignment(target);
const u8_llvm_ty = self.context.intType(8);
const dest_ptr = self.sliceOrArrayPtr(dest_slice, ptr_ty);
+ const is_volatile = ptr_ty.isVolatilePtr();
+
+ if (self.air.value(bin_op.rhs)) |elem_val| {
+ if (elem_val.isUndefDeep()) {
+ // Even if safety is disabled, we still emit a memset to undefined since it conveys
+ // extra information to LLVM. However, safety makes the difference between using
+ // 0xaa or actual undefined for the fill byte.
+ const fill_byte = if (safety)
+ u8_llvm_ty.constInt(0xaa, .False)
+ else
+ u8_llvm_ty.getUndef();
+ const len = self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);
+ _ = self.builder.buildMemSet(dest_ptr, fill_byte, len, dest_ptr_align, is_volatile);
- if (val_is_undef) {
- // Even if safety is disabled, we still emit a memset to undefined since it conveys
- // extra information to LLVM. However, safety makes the difference between using
- // 0xaa or actual undefined for the fill byte.
- const fill_byte = if (safety)
- u8_llvm_ty.constInt(0xaa, .False)
- else
- u8_llvm_ty.getUndef();
- const len = self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);
- _ = self.builder.buildMemSet(dest_ptr, fill_byte, len, dest_ptr_align, ptr_ty.isVolatilePtr());
+ if (safety and module.comp.bin_file.options.valgrind) {
+ self.valgrindMarkUndef(dest_ptr, len);
+ }
+ return null;
+ }
- if (safety and self.dg.module.comp.bin_file.options.valgrind) {
- self.valgrindMarkUndef(dest_ptr, len);
+ // Test if the element value is compile-time known to be a
+ // repeating byte pattern, for example, `@as(u64, 0)` has a
+ // repeating byte pattern of 0 bytes. In such case, the memset
+ // intrinsic can be used.
+ var value_buffer: Value.Payload.U64 = undefined;
+ if (try elem_val.hasRepeatedByteRepr(elem_ty, module, &value_buffer)) |byte_val| {
+ const fill_byte = try self.resolveValue(.{
+ .ty = Type.u8,
+ .val = byte_val,
+ });
+ const len = self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);
+ _ = self.builder.buildMemSet(dest_ptr, fill_byte, len, dest_ptr_align, is_volatile);
+ return null;
}
- return null;
}
const value = try self.resolveInst(bin_op.rhs);
@@ -8442,9 +8470,9 @@ pub const FuncGen = struct {
if (elem_abi_size == 1) {
// In this case we can take advantage of LLVM's intrinsic.
- const fill_byte = self.builder.buildBitCast(value, u8_llvm_ty, "");
+ const fill_byte = try self.bitCast(value, elem_ty, Type.u8);
const len = self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);
- _ = self.builder.buildMemSet(dest_ptr, fill_byte, len, dest_ptr_align, ptr_ty.isVolatilePtr());
+ _ = self.builder.buildMemSet(dest_ptr, fill_byte, len, dest_ptr_align, is_volatile);
return null;
}
@@ -8486,8 +8514,22 @@ pub const FuncGen = struct {
_ = self.builder.buildCondBr(end, body_block, end_block);
self.builder.positionBuilderAtEnd(body_block);
- const store_inst = self.builder.buildStore(value, it_ptr);
- store_inst.setAlignment(@min(elem_ty.abiAlignment(target), dest_ptr_align));
+ const elem_abi_alignment = elem_ty.abiAlignment(target);
+ const it_ptr_alignment = @min(elem_abi_alignment, dest_ptr_align);
+ if (isByRef(elem_ty)) {
+ _ = self.builder.buildMemCpy(
+ it_ptr,
+ it_ptr_alignment,
+ value,
+ elem_abi_alignment,
+ llvm_usize_ty.constInt(elem_abi_size, .False),
+ is_volatile,
+ );
+ } else {
+ const store_inst = self.builder.buildStore(value, it_ptr);
+ store_inst.setAlignment(it_ptr_alignment);
+ store_inst.setVolatile(llvm.Bool.fromBool(is_volatile));
+ }
const one_gep = [_]*llvm.Value{llvm_usize_ty.constInt(1, .False)};
const next_ptr = self.builder.buildInBoundsGEP(elem_llvm_ty, it_ptr, &one_gep, one_gep.len, "");
_ = self.builder.buildBr(loop_block);
diff --git a/src/link/Coff.zig b/src/link/Coff.zig
index ed3e213b0e..0af681bb5e 100644
--- a/src/link/Coff.zig
+++ b/src/link/Coff.zig
@@ -1916,7 +1916,7 @@ fn writeImportTables(self: *Coff) !void {
.name_rva = header.virtual_address + dll_names_offset,
.import_address_table_rva = header.virtual_address + iat_offset,
};
- mem.copy(u8, buffer.items[dir_table_offset..], mem.asBytes(&lookup_header));
+ @memcpy(buffer.items[dir_table_offset..][0..@sizeOf(coff.ImportDirectoryEntry)], mem.asBytes(&lookup_header));
dir_table_offset += dir_header_size;
for (itable.entries.items) |entry| {
@@ -1924,15 +1924,21 @@ fn writeImportTables(self: *Coff) !void {
// IAT and lookup table entry
const lookup = coff.ImportLookupEntry64.ByName{ .name_table_rva = @intCast(u31, header.virtual_address + names_table_offset) };
- mem.copy(u8, buffer.items[iat_offset..], mem.asBytes(&lookup));
+ @memcpy(
+ buffer.items[iat_offset..][0..@sizeOf(coff.ImportLookupEntry64.ByName)],
+ mem.asBytes(&lookup),
+ );
iat_offset += lookup_entry_size;
- mem.copy(u8, buffer.items[lookup_table_offset..], mem.asBytes(&lookup));
+ @memcpy(
+ buffer.items[lookup_table_offset..][0..@sizeOf(coff.ImportLookupEntry64.ByName)],
+ mem.asBytes(&lookup),
+ );
lookup_table_offset += lookup_entry_size;
// Names table entry
mem.writeIntLittle(u16, buffer.items[names_table_offset..][0..2], 0); // Hint set to 0 until we learn how to parse DLLs
names_table_offset += 2;
- mem.copy(u8, buffer.items[names_table_offset..], import_name);
+ @memcpy(buffer.items[names_table_offset..][0..import_name.len], import_name);
names_table_offset += @intCast(u32, import_name.len);
buffer.items[names_table_offset] = 0;
names_table_offset += 1;
@@ -1947,13 +1953,16 @@ fn writeImportTables(self: *Coff) !void {
iat_offset += 8;
// Lookup table sentinel
- mem.copy(u8, buffer.items[lookup_table_offset..], mem.asBytes(&coff.ImportLookupEntry64.ByName{ .name_table_rva = 0 }));
+ @memcpy(
+ buffer.items[lookup_table_offset..][0..@sizeOf(coff.ImportLookupEntry64.ByName)],
+ mem.asBytes(&coff.ImportLookupEntry64.ByName{ .name_table_rva = 0 }),
+ );
lookup_table_offset += lookup_entry_size;
// DLL name
- mem.copy(u8, buffer.items[dll_names_offset..], lib_name);
+ @memcpy(buffer.items[dll_names_offset..][0..lib_name.len], lib_name);
dll_names_offset += @intCast(u32, lib_name.len);
- mem.copy(u8, buffer.items[dll_names_offset..], ext);
+ @memcpy(buffer.items[dll_names_offset..][0..ext.len], ext);
dll_names_offset += @intCast(u32, ext.len);
buffer.items[dll_names_offset] = 0;
dll_names_offset += 1;
@@ -1967,7 +1976,10 @@ fn writeImportTables(self: *Coff) !void {
.name_rva = 0,
.import_address_table_rva = 0,
};
- mem.copy(u8, buffer.items[dir_table_offset..], mem.asBytes(&lookup_header));
+ @memcpy(
+ buffer.items[dir_table_offset..][0..@sizeOf(coff.ImportDirectoryEntry)],
+ mem.asBytes(&lookup_header),
+ );
dir_table_offset += dir_header_size;
assert(dll_names_offset == needed_size);
@@ -2366,13 +2378,13 @@ pub fn getAtomIndexForSymbol(self: *const Coff, sym_loc: SymbolWithLoc) ?Atom.In
fn setSectionName(self: *Coff, header: *coff.SectionHeader, name: []const u8) !void {
if (name.len <= 8) {
- mem.copy(u8, &header.name, name);
- mem.set(u8, header.name[name.len..], 0);
+ @memcpy(header.name[0..name.len], name);
+ @memset(header.name[name.len..], 0);
return;
}
const offset = try self.strtab.insert(self.base.allocator, name);
const name_offset = fmt.bufPrint(&header.name, "/{d}", .{offset}) catch unreachable;
- mem.set(u8, header.name[name_offset.len..], 0);
+ @memset(header.name[name_offset.len..], 0);
}
fn getSectionName(self: *const Coff, header: *const coff.SectionHeader) []const u8 {
@@ -2385,17 +2397,17 @@ fn getSectionName(self: *const Coff, header: *const coff.SectionHeader) []const
fn setSymbolName(self: *Coff, symbol: *coff.Symbol, name: []const u8) !void {
if (name.len <= 8) {
- mem.copy(u8, &symbol.name, name);
- mem.set(u8, symbol.name[name.len..], 0);
+ @memcpy(symbol.name[0..name.len], name);
+ @memset(symbol.name[name.len..], 0);
return;
}
const offset = try self.strtab.insert(self.base.allocator, name);
- mem.set(u8, symbol.name[0..4], 0);
+ @memset(symbol.name[0..4], 0);
mem.writeIntLittle(u32, symbol.name[4..8], offset);
}
fn logSymAttributes(sym: *const coff.Symbol, buf: *[4]u8) []const u8 {
- mem.set(u8, buf[0..4], '_');
+ @memset(buf[0..4], '_');
switch (sym.section_number) {
.UNDEFINED => {
buf[3] = 'u';
diff --git a/src/link/Dwarf.zig b/src/link/Dwarf.zig
index 0f2dfbda0e..7a008ca732 100644
--- a/src/link/Dwarf.zig
+++ b/src/link/Dwarf.zig
@@ -1189,7 +1189,7 @@ pub fn commitDeclState(
if (needed_size > segment_size) {
log.debug(" allocating {d} bytes for 'debug line' information", .{needed_size - segment_size});
try debug_line.resize(self.allocator, needed_size);
- mem.set(u8, debug_line.items[segment_size..], 0);
+ @memset(debug_line.items[segment_size..], 0);
}
debug_line.items.len = needed_size;
}
@@ -1458,7 +1458,7 @@ fn writeDeclDebugInfo(self: *Dwarf, atom_index: Atom.Index, dbg_info_buf: []cons
if (needed_size > segment_size) {
log.debug(" allocating {d} bytes for 'debug info' information", .{needed_size - segment_size});
try debug_info.resize(self.allocator, needed_size);
- mem.set(u8, debug_info.items[segment_size..], 0);
+ @memset(debug_info.items[segment_size..], 0);
}
debug_info.items.len = needed_size;
}
@@ -1515,7 +1515,7 @@ pub fn updateDeclLineNumber(self: *Dwarf, module: *Module, decl_index: Module.De
const wasm_file = self.bin_file.cast(File.Wasm).?;
const offset = atom.off + self.getRelocDbgLineOff();
const line_atom_index = wasm_file.debug_line_atom.?;
- mem.copy(u8, wasm_file.getAtomPtr(line_atom_index).code.items[offset..], &data);
+ wasm_file.getAtomPtr(line_atom_index).code.items[offset..][0..data.len].* = data;
},
else => unreachable,
}
@@ -1734,7 +1734,7 @@ pub fn writeDbgAbbrev(self: *Dwarf) !void {
const wasm_file = self.bin_file.cast(File.Wasm).?;
const debug_abbrev = &wasm_file.getAtomPtr(wasm_file.debug_abbrev_atom.?).code;
try debug_abbrev.resize(wasm_file.base.allocator, needed_size);
- mem.copy(u8, debug_abbrev.items, &abbrev_buf);
+ debug_abbrev.items[0..abbrev_buf.len].* = abbrev_buf;
},
else => unreachable,
}
@@ -1976,7 +1976,7 @@ fn writeDbgLineNopsBuffered(
}
}
- mem.copy(u8, buf[offset..], content);
+ @memcpy(buf[offset..][0..content.len], content);
{
var padding_left = next_padding_size;
@@ -2076,9 +2076,9 @@ fn writeDbgInfoNopsToArrayList(
buffer.items.len,
offset + content.len + next_padding_size + 1,
));
- mem.set(u8, buffer.items[offset - prev_padding_size .. offset], @enumToInt(AbbrevKind.pad1));
- mem.copy(u8, buffer.items[offset..], content);
- mem.set(u8, buffer.items[offset + content.len ..][0..next_padding_size], @enumToInt(AbbrevKind.pad1));
+ @memset(buffer.items[offset - prev_padding_size .. offset], @enumToInt(AbbrevKind.pad1));
+ @memcpy(buffer.items[offset..][0..content.len], content);
+ @memset(buffer.items[offset + content.len ..][0..next_padding_size], @enumToInt(AbbrevKind.pad1));
if (trailing_zero) {
buffer.items[offset + content.len + next_padding_size] = 0;
@@ -2168,7 +2168,7 @@ pub fn writeDbgAranges(self: *Dwarf, addr: u64, size: u64) !void {
const wasm_file = self.bin_file.cast(File.Wasm).?;
const debug_ranges = &wasm_file.getAtomPtr(wasm_file.debug_ranges_atom.?).code;
try debug_ranges.resize(wasm_file.base.allocator, needed_size);
- mem.copy(u8, debug_ranges.items, di_buf.items);
+ @memcpy(debug_ranges.items[0..di_buf.items.len], di_buf.items);
},
else => unreachable,
}
@@ -2341,9 +2341,12 @@ pub fn writeDbgLineHeader(self: *Dwarf) !void {
.wasm => {
const wasm_file = self.bin_file.cast(File.Wasm).?;
const debug_line = &wasm_file.getAtomPtr(wasm_file.debug_line_atom.?).code;
- mem.copy(u8, buffer, debug_line.items[first_fn.off..]);
+ {
+ const src = debug_line.items[first_fn.off..];
+ @memcpy(buffer[0..src.len], src);
+ }
try debug_line.resize(self.allocator, debug_line.items.len + delta);
- mem.copy(u8, debug_line.items[first_fn.off + delta ..], buffer);
+ @memcpy(debug_line.items[first_fn.off + delta ..][0..buffer.len], buffer);
},
else => unreachable,
}
@@ -2537,7 +2540,7 @@ pub fn flushModule(self: *Dwarf, module: *Module) !void {
.wasm => {
const wasm_file = self.bin_file.cast(File.Wasm).?;
const debug_info = wasm_file.getAtomPtr(wasm_file.debug_info_atom.?).code;
- mem.copy(u8, debug_info.items[atom.off + reloc.offset ..], &buf);
+ debug_info.items[atom.off + reloc.offset ..][0..buf.len].* = buf;
},
else => unreachable,
}
diff --git a/src/link/Elf.zig b/src/link/Elf.zig
index 4a6bb99818..48d952b6cc 100644
--- a/src/link/Elf.zig
+++ b/src/link/Elf.zig
@@ -1997,7 +1997,7 @@ fn writeElfHeader(self: *Elf) !void {
// OS ABI, often set to 0 regardless of target platform
// ABI Version, possibly used by glibc but not by static executables
// padding
- mem.set(u8, hdr_buf[index..][0..9], 0);
+ @memset(hdr_buf[index..][0..9], 0);
index += 9;
assert(index == 16);
diff --git a/src/link/MachO.zig b/src/link/MachO.zig
index df9b8a768a..21633dea64 100644
--- a/src/link/MachO.zig
+++ b/src/link/MachO.zig
@@ -1454,7 +1454,7 @@ fn createThreadLocalDescriptorAtom(self: *MachO, sym_name: []const u8, target: S
});
var code: [size]u8 = undefined;
- mem.set(u8, &code, 0);
+ @memset(&code, 0);
try self.writeAtom(atom_index, &code);
return atom_index;
@@ -3234,7 +3234,7 @@ fn writeDyldInfoData(self: *MachO) !void {
var buffer = try gpa.alloc(u8, needed_size);
defer gpa.free(buffer);
- mem.set(u8, buffer, 0);
+ @memset(buffer, 0);
var stream = std.io.fixedBufferStream(buffer);
const writer = stream.writer();
@@ -3389,8 +3389,8 @@ fn writeStrtab(self: *MachO) !void {
const buffer = try gpa.alloc(u8, math.cast(usize, needed_size_aligned) orelse return error.Overflow);
defer gpa.free(buffer);
- mem.set(u8, buffer, 0);
- mem.copy(u8, buffer, self.strtab.buffer.items);
+ @memcpy(buffer[0..self.strtab.buffer.items.len], self.strtab.buffer.items);
+ @memset(buffer[self.strtab.buffer.items.len..], 0);
try self.base.file.?.pwriteAll(buffer, offset);
@@ -3668,8 +3668,7 @@ fn addUndefined(self: *MachO, name: []const u8, action: ResolveAction.Kind) !u32
pub fn makeStaticString(bytes: []const u8) [16]u8 {
var buf = [_]u8{0} ** 16;
- assert(bytes.len <= buf.len);
- mem.copy(u8, &buf, bytes);
+ @memcpy(buf[0..bytes.len], bytes);
return buf;
}
@@ -4096,8 +4095,8 @@ pub fn logSections(self: *MachO) void {
}
fn logSymAttributes(sym: macho.nlist_64, buf: *[4]u8) []const u8 {
- mem.set(u8, buf[0..4], '_');
- mem.set(u8, buf[4..], ' ');
+ @memset(buf[0..4], '_');
+ @memset(buf[4..], ' ');
if (sym.sect()) {
buf[0] = 's';
}
diff --git a/src/link/MachO/CodeSignature.zig b/src/link/MachO/CodeSignature.zig
index 6d1cd7b536..59b3e50b07 100644
--- a/src/link/MachO/CodeSignature.zig
+++ b/src/link/MachO/CodeSignature.zig
@@ -100,7 +100,7 @@ const CodeDirectory = struct {
fn addSpecialHash(self: *CodeDirectory, index: u32, hash: [hash_size]u8) void {
assert(index > 0);
self.inner.nSpecialSlots = std.math.max(self.inner.nSpecialSlots, index);
- mem.copy(u8, &self.special_slots[index - 1], &hash);
+ self.special_slots[index - 1] = hash;
}
fn slotType(self: CodeDirectory) u32 {
diff --git a/src/link/MachO/Object.zig b/src/link/MachO/Object.zig
index e407457e03..7cc6f78c7d 100644
--- a/src/link/MachO/Object.zig
+++ b/src/link/MachO/Object.zig
@@ -156,7 +156,7 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch)
// Prepopulate relocations per section lookup table.
try self.section_relocs_lookup.resize(allocator, nsects);
- mem.set(u32, self.section_relocs_lookup.items, 0);
+ @memset(self.section_relocs_lookup.items, 0);
// Parse symtab.
const symtab = while (it.next()) |cmd| switch (cmd.cmd()) {
@@ -189,10 +189,10 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch)
};
}
- mem.set(i64, self.globals_lookup, -1);
- mem.set(AtomIndex, self.atom_by_index_table, 0);
- mem.set(Entry, self.source_section_index_lookup, .{});
- mem.set(Entry, self.relocs_lookup, .{});
+ @memset(self.globals_lookup, -1);
+ @memset(self.atom_by_index_table, 0);
+ @memset(self.source_section_index_lookup, .{});
+ @memset(self.relocs_lookup, .{});
// You would expect that the symbol table is at least pre-sorted based on symbol's type:
// local < extern defined < undefined. Unfortunately, this is not guaranteed! For instance,
@@ -252,7 +252,7 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch)
self.unwind_info_sect_id = self.getSourceSectionIndexByName("__LD", "__compact_unwind");
if (self.hasUnwindRecords()) {
self.unwind_relocs_lookup = try allocator.alloc(Record, self.getUnwindRecords().len);
- mem.set(Record, self.unwind_relocs_lookup, .{ .dead = true, .reloc = .{} });
+ @memset(self.unwind_relocs_lookup, .{ .dead = true, .reloc = .{} });
}
}
diff --git a/src/link/MachO/Trie.zig b/src/link/MachO/Trie.zig
index a97e18a186..34200db7dc 100644
--- a/src/link/MachO/Trie.zig
+++ b/src/link/MachO/Trie.zig
@@ -499,7 +499,7 @@ fn expectEqualHexStrings(expected: []const u8, given: []const u8) !void {
const idx = mem.indexOfDiff(u8, expected_fmt, given_fmt).?;
var padding = try testing.allocator.alloc(u8, idx + 5);
defer testing.allocator.free(padding);
- mem.set(u8, padding, ' ');
+ @memset(padding, ' ');
std.debug.print("\nEXP: {s}\nGIV: {s}\n{s}^ -- first differing byte\n", .{ expected_fmt, given_fmt, padding });
return error.TestFailed;
}
diff --git a/src/link/MachO/UnwindInfo.zig b/src/link/MachO/UnwindInfo.zig
index e59f5fe250..0071657f8b 100644
--- a/src/link/MachO/UnwindInfo.zig
+++ b/src/link/MachO/UnwindInfo.zig
@@ -659,7 +659,7 @@ pub fn write(info: *UnwindInfo, zld: *Zld) !void {
const padding = buffer.items.len - cwriter.bytes_written;
if (padding > 0) {
const offset = math.cast(usize, cwriter.bytes_written) orelse return error.Overflow;
- mem.set(u8, buffer.items[offset..], 0);
+ @memset(buffer.items[offset..], 0);
}
try zld.file.pwriteAll(buffer.items, sect.offset);
diff --git a/src/link/MachO/zld.zig b/src/link/MachO/zld.zig
index bc658fc8d2..7e6870ecbc 100644
--- a/src/link/MachO/zld.zig
+++ b/src/link/MachO/zld.zig
@@ -2140,7 +2140,7 @@ pub const Zld = struct {
var buffer = try gpa.alloc(u8, needed_size);
defer gpa.free(buffer);
- mem.set(u8, buffer, 0);
+ @memset(buffer, 0);
var stream = std.io.fixedBufferStream(buffer);
const writer = stream.writer();
@@ -2352,8 +2352,11 @@ pub const Zld = struct {
const buffer = try self.gpa.alloc(u8, math.cast(usize, needed_size_aligned) orelse return error.Overflow);
defer self.gpa.free(buffer);
- mem.set(u8, buffer, 0);
- mem.copy(u8, buffer, mem.sliceAsBytes(out_dice.items));
+ {
+ const src = mem.sliceAsBytes(out_dice.items);
+ @memcpy(buffer[0..src.len], src);
+ @memset(buffer[src.len..], 0);
+ }
log.debug("writing data-in-code from 0x{x} to 0x{x}", .{ offset, offset + needed_size_aligned });
@@ -2484,8 +2487,8 @@ pub const Zld = struct {
const buffer = try self.gpa.alloc(u8, math.cast(usize, needed_size_aligned) orelse return error.Overflow);
defer self.gpa.free(buffer);
- mem.set(u8, buffer, 0);
- mem.copy(u8, buffer, self.strtab.buffer.items);
+ @memcpy(buffer[0..self.strtab.buffer.items.len], self.strtab.buffer.items);
+ @memset(buffer[self.strtab.buffer.items.len..], 0);
try self.file.pwriteAll(buffer, offset);
@@ -2805,8 +2808,7 @@ pub const Zld = struct {
pub fn makeStaticString(bytes: []const u8) [16]u8 {
var buf = [_]u8{0} ** 16;
- assert(bytes.len <= buf.len);
- mem.copy(u8, &buf, bytes);
+ @memcpy(buf[0..bytes.len], bytes);
return buf;
}
@@ -3199,7 +3201,7 @@ pub const Zld = struct {
scoped_log.debug(" object({d}): {s}", .{ id, object.name });
if (object.in_symtab == null) continue;
for (object.symtab, 0..) |sym, sym_id| {
- mem.set(u8, &buf, '_');
+ @memset(&buf, '_');
scoped_log.debug(" %{d}: {s} @{x} in sect({d}), {s}", .{
sym_id,
object.getSymbolName(@intCast(u32, sym_id)),
@@ -4007,7 +4009,7 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr
log.debug("zeroing out zerofill area of length {x} at {x}", .{ size, start });
var padding = try zld.gpa.alloc(u8, size);
defer zld.gpa.free(padding);
- mem.set(u8, padding, 0);
+ @memset(padding, 0);
try zld.file.pwriteAll(padding, start);
}
}
diff --git a/src/link/Plan9.zig b/src/link/Plan9.zig
index e7b401e8af..bef06d1c87 100644
--- a/src/link/Plan9.zig
+++ b/src/link/Plan9.zig
@@ -681,7 +681,7 @@ pub fn flushModule(self: *Plan9, comp: *Compilation, prog_node: *std.Progress.No
.pcsz = @intCast(u32, linecountinfo.items.len),
.entry = @intCast(u32, self.entry_val.?),
};
- std.mem.copy(u8, hdr_slice, self.hdr.toU8s()[0..hdr_size]);
+ @memcpy(hdr_slice, self.hdr.toU8s()[0..hdr_size]);
// write the fat header for 64 bit entry points
if (self.sixtyfour_bit) {
mem.writeIntSliceBig(u64, hdr_buf[32..40], self.entry_val.?);
diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig
index 0fe9ec5e3b..b6f4a4cc59 100644
--- a/src/link/Wasm.zig
+++ b/src/link/Wasm.zig
@@ -1976,7 +1976,7 @@ fn parseAtom(wasm: *Wasm, atom_index: Atom.Index, kind: Kind) !void {
// We do not have to do this when exporting the memory (the default) because the runtime
// will do it for us, and we do not emit the bss segment at all.
if ((wasm.base.options.output_mode == .Obj or wasm.base.options.import_memory) and kind.data == .uninitialized) {
- std.mem.set(u8, atom.code.items, 0);
+ @memset(atom.code.items, 0);
}
const should_merge = wasm.base.options.output_mode != .Obj;
@@ -3852,7 +3852,10 @@ fn writeToFile(
// Only when writing all sections executed properly we write the magic
// bytes. This allows us to easily detect what went wrong while generating
// the final binary.
- mem.copy(u8, binary_bytes.items, &(std.wasm.magic ++ std.wasm.version));
+ {
+ const src = std.wasm.magic ++ std.wasm.version;
+ binary_bytes.items[0..src.len].* = src;
+ }
// finally, write the entire binary into the file.
var iovec = [_]std.os.iovec_const{.{
@@ -4559,14 +4562,14 @@ fn writeVecSectionHeader(buffer: []u8, offset: u32, section: std.wasm.Section, s
buf[0] = @enumToInt(section);
leb.writeUnsignedFixed(5, buf[1..6], size);
leb.writeUnsignedFixed(5, buf[6..], items);
- mem.copy(u8, buffer[offset..], &buf);
+ buffer[offset..][0..buf.len].* = buf;
}
fn writeCustomSectionHeader(buffer: []u8, offset: u32, size: u32) !void {
var buf: [1 + 5]u8 = undefined;
buf[0] = 0; // 0 = 'custom' section
leb.writeUnsignedFixed(5, buf[1..6], size);
- mem.copy(u8, buffer[offset..], &buf);
+ buffer[offset..][0..buf.len].* = buf;
}
fn emitLinkSection(wasm: *Wasm, binary_bytes: *std.ArrayList(u8), symbol_table: *std.AutoArrayHashMap(SymbolLoc, u32)) !void {
diff --git a/src/objcopy.zig b/src/objcopy.zig
index 4a15af88e3..12129aba9c 100644
--- a/src/objcopy.zig
+++ b/src/objcopy.zig
@@ -860,7 +860,7 @@ fn ElfFile(comptime is_64: bool) type {
if (section.payload) |data| {
switch (section.section.sh_type) {
elf.DT_VERSYM => {
- std.debug.assert(section.section.sh_entsize == @sizeOf(Elf_Verdef));
+ assert(section.section.sh_entsize == @sizeOf(Elf_Verdef));
const defs = @ptrCast([*]const Elf_Verdef, data)[0 .. @intCast(usize, section.section.sh_size) / @sizeOf(Elf_Verdef)];
for (defs) |def| {
if (def.vd_ndx != elf.SHN_UNDEF)
@@ -868,7 +868,7 @@ fn ElfFile(comptime is_64: bool) type {
}
},
elf.SHT_SYMTAB, elf.SHT_DYNSYM => {
- std.debug.assert(section.section.sh_entsize == @sizeOf(Elf_Sym));
+ assert(section.section.sh_entsize == @sizeOf(Elf_Sym));
const syms = @ptrCast([*]const Elf_Sym, data)[0 .. @intCast(usize, section.section.sh_size) / @sizeOf(Elf_Sym)];
for (syms) |sym| {
@@ -952,11 +952,11 @@ fn ElfFile(comptime is_64: bool) type {
const name: []const u8 = ".gnu_debuglink";
const new_offset = @intCast(u32, strtab.payload.?.len);
const buf = try allocator.alignedAlloc(u8, section_memory_align, new_offset + name.len + 1);
- std.mem.copy(u8, buf[0..new_offset], strtab.payload.?);
- std.mem.copy(u8, buf[new_offset .. new_offset + name.len], name);
+ @memcpy(buf[0..new_offset], strtab.payload.?);
+ @memcpy(buf[new_offset..][0..name.len], name);
buf[new_offset + name.len] = 0;
- std.debug.assert(update.action == .keep);
+ assert(update.action == .keep);
update.payload = buf;
break :blk new_offset;
@@ -978,9 +978,9 @@ fn ElfFile(comptime is_64: bool) type {
// program header as-is.
// nb: for only-debug files, removing it appears to work, but is invalid by ELF specifcation.
{
- std.debug.assert(updated_elf_header.e_phoff == @sizeOf(Elf_Ehdr));
+ assert(updated_elf_header.e_phoff == @sizeOf(Elf_Ehdr));
const data = std.mem.sliceAsBytes(self.program_segments);
- std.debug.assert(data.len == @as(usize, updated_elf_header.e_phentsize) * updated_elf_header.e_phnum);
+ assert(data.len == @as(usize, updated_elf_header.e_phentsize) * updated_elf_header.e_phnum);
cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = data, .out_offset = updated_elf_header.e_phoff } });
eof_offset = updated_elf_header.e_phoff + @intCast(Elf_OffSize, data.len);
}
@@ -1006,7 +1006,7 @@ fn ElfFile(comptime is_64: bool) type {
var dest_section_idx: u32 = 1;
for (self.sections[1..], sections_update[1..]) |section, update| {
if (update.action == .strip) continue;
- std.debug.assert(update.remap_idx == dest_section_idx);
+ assert(update.remap_idx == dest_section_idx);
const src = if (update.section) |*s| s else &section.section;
const dest = &dest_sections[dest_section_idx];
@@ -1032,7 +1032,7 @@ fn ElfFile(comptime is_64: bool) type {
fatal("zig objcopy: cannot adjust program segments", .{});
}
}
- std.debug.assert(dest.sh_addr % addralign == dest.sh_offset % addralign);
+ assert(dest.sh_addr % addralign == dest.sh_offset % addralign);
if (update.action == .empty)
dest.sh_type = elf.SHT_NOBITS;
@@ -1043,7 +1043,7 @@ fn ElfFile(comptime is_64: bool) type {
const dest_data = switch (src.sh_type) {
elf.DT_VERSYM => dst_data: {
const data = try allocator.alignedAlloc(u8, section_memory_align, src_data.len);
- std.mem.copy(u8, data, src_data);
+ @memcpy(data, src_data);
const defs = @ptrCast([*]Elf_Verdef, data)[0 .. @intCast(usize, src.sh_size) / @sizeOf(Elf_Verdef)];
for (defs) |*def| {
@@ -1055,7 +1055,7 @@ fn ElfFile(comptime is_64: bool) type {
},
elf.SHT_SYMTAB, elf.SHT_DYNSYM => dst_data: {
const data = try allocator.alignedAlloc(u8, section_memory_align, src_data.len);
- std.mem.copy(u8, data, src_data);
+ @memcpy(data, src_data);
const syms = @ptrCast([*]Elf_Sym, data)[0 .. @intCast(usize, src.sh_size) / @sizeOf(Elf_Sym)];
for (syms) |*sym| {
@@ -1068,7 +1068,7 @@ fn ElfFile(comptime is_64: bool) type {
else => src_data,
};
- std.debug.assert(dest_data.len == dest.sh_size);
+ assert(dest_data.len == dest.sh_size);
cmdbuf.appendAssumeCapacity(.{ .write_data = .{ .data = dest_data, .out_offset = dest.sh_offset } });
eof_offset = dest.sh_offset + dest.sh_size;
} else {
@@ -1087,9 +1087,9 @@ fn ElfFile(comptime is_64: bool) type {
const payload = payload: {
const crc_offset = std.mem.alignForward(link.name.len + 1, 4);
const buf = try allocator.alignedAlloc(u8, 4, crc_offset + 4);
- std.mem.copy(u8, buf[0..link.name.len], link.name);
- std.mem.set(u8, buf[link.name.len..crc_offset], 0);
- std.mem.copy(u8, buf[crc_offset..], std.mem.asBytes(&link.crc32));
+ @memcpy(buf[0..link.name.len], link.name);
+ @memset(buf[link.name.len..crc_offset], 0);
+ @memcpy(buf[crc_offset..], std.mem.asBytes(&link.crc32));
break :payload buf;
};
@@ -1111,7 +1111,7 @@ fn ElfFile(comptime is_64: bool) type {
eof_offset += @intCast(Elf_OffSize, payload.len);
}
- std.debug.assert(dest_section_idx == new_shnum);
+ assert(dest_section_idx == new_shnum);
break :blk dest_sections;
};
@@ -1120,7 +1120,7 @@ fn ElfFile(comptime is_64: bool) type {
const offset = std.mem.alignForwardGeneric(Elf_OffSize, eof_offset, @alignOf(Elf_Shdr));
const data = std.mem.sliceAsBytes(updated_section_header);
- std.debug.assert(data.len == @as(usize, updated_elf_header.e_shentsize) * new_shnum);
+ assert(data.len == @as(usize, updated_elf_header.e_shentsize) * new_shnum);
updated_elf_header.e_shoff = offset;
updated_elf_header.e_shnum = new_shnum;
@@ -1215,7 +1215,7 @@ const ElfFileHelper = struct {
for (cmds) |cmd| {
switch (cmd) {
.write_data => |data| {
- std.debug.assert(data.out_offset >= offset);
+ assert(data.out_offset >= offset);
if (fused_cmd) |prev| {
consolidated.appendAssumeCapacity(prev);
fused_cmd = null;
@@ -1227,7 +1227,7 @@ const ElfFileHelper = struct {
offset = data.out_offset + data.data.len;
},
.copy_range => |range| {
- std.debug.assert(range.out_offset >= offset);
+ assert(range.out_offset >= offset);
if (fused_cmd) |prev| {
if (range.in_offset >= prev.copy_range.in_offset + prev.copy_range.len and (range.out_offset - prev.copy_range.out_offset == range.in_offset - prev.copy_range.in_offset)) {
fused_cmd = .{ .copy_range = .{
diff --git a/src/print_air.zig b/src/print_air.zig
index db3e47c0dd..2d7995842f 100644
--- a/src/print_air.zig
+++ b/src/print_air.zig
@@ -846,7 +846,7 @@ const Writer = struct {
else blk: {
const slice = w.gpa.alloc([]const Air.Inst.Index, switch_br.data.cases_len + 1) catch
@panic("out of memory");
- std.mem.set([]const Air.Inst.Index, slice, &.{});
+ @memset(slice, &.{});
break :blk Liveness.SwitchBrTable{ .deaths = slice };
};
defer w.gpa.free(liveness.deaths);
diff --git a/src/print_zir.zig b/src/print_zir.zig
index 30bfdba347..922366dc85 100644
--- a/src/print_zir.zig
+++ b/src/print_zir.zig
@@ -682,7 +682,7 @@ const Writer = struct {
const limbs = try self.gpa.alloc(std.math.big.Limb, inst_data.len);
defer self.gpa.free(limbs);
- mem.copy(u8, mem.sliceAsBytes(limbs), limb_bytes);
+ @memcpy(mem.sliceAsBytes(limbs), limb_bytes);
const big_int: std.math.big.int.Const = .{
.limbs = limbs,
.positive = true,
diff --git a/src/translate_c.zig b/src/translate_c.zig
index 7ad58329df..bc7a1138da 100644
--- a/src/translate_c.zig
+++ b/src/translate_c.zig
@@ -113,7 +113,7 @@ const Scope = struct {
const alloc_len = self.statements.items.len + @boolToInt(self.base.parent.?.id == .do_loop);
var stmts = try c.arena.alloc(Node, alloc_len);
stmts.len = self.statements.items.len;
- mem.copy(Node, stmts, self.statements.items);
+ @memcpy(stmts[0..self.statements.items.len], self.statements.items);
return Tag.block.create(c.arena, .{
.label = self.label,
.stmts = stmts,
diff --git a/src/type.zig b/src/type.zig
index c7b2844970..816a1ee2e0 100644
--- a/src/type.zig
+++ b/src/type.zig
@@ -4767,7 +4767,7 @@ pub const Type = extern union {
.fn_ccc_void_no_args => return,
.function => {
const payload = self.castTag(.function).?.data;
- std.mem.copy(Type, types, payload.param_types);
+ @memcpy(types[0..payload.param_types.len], payload.param_types);
},
else => unreachable,
diff --git a/src/value.zig b/src/value.zig
index 05e9d24ee2..16ccc0c642 100644
--- a/src/value.zig
+++ b/src/value.zig
@@ -875,7 +875,7 @@ pub const Value = extern union {
.repeated => {
const byte = @intCast(u8, val.castTag(.repeated).?.data.toUnsignedInt(target));
const result = try allocator.alloc(u8, @intCast(usize, ty.arrayLen()));
- std.mem.set(u8, result, byte);
+ @memset(result, byte);
return result;
},
.decl_ref => {
@@ -1278,12 +1278,16 @@ pub const Value = extern union {
///
/// Asserts that buffer.len >= ty.abiSize(). The buffer is allowed to extend past
/// the end of the value in memory.
- pub fn writeToMemory(val: Value, ty: Type, mod: *Module, buffer: []u8) error{ReinterpretDeclRef}!void {
+ pub fn writeToMemory(val: Value, ty: Type, mod: *Module, buffer: []u8) error{
+ ReinterpretDeclRef,
+ IllDefinedMemoryLayout,
+ Unimplemented,
+ }!void {
const target = mod.getTarget();
const endian = target.cpu.arch.endian();
if (val.isUndef()) {
const size = @intCast(usize, ty.abiSize(target));
- std.mem.set(u8, buffer[0..size], 0xaa);
+ @memset(buffer[0..size], 0xaa);
return;
}
switch (ty.zigTypeTag()) {
@@ -1345,7 +1349,7 @@ pub const Value = extern union {
return writeToPackedMemory(val, ty, mod, buffer[0..byte_count], 0);
},
.Struct => switch (ty.containerLayout()) {
- .Auto => unreachable, // Sema is supposed to have emitted a compile error already
+ .Auto => return error.IllDefinedMemoryLayout,
.Extern => {
const fields = ty.structFields().values();
const field_vals = val.castTag(.aggregate).?.data;
@@ -1366,20 +1370,20 @@ pub const Value = extern union {
std.mem.writeInt(Int, buffer[0..@sizeOf(Int)], @intCast(Int, int), endian);
},
.Union => switch (ty.containerLayout()) {
- .Auto => unreachable,
- .Extern => @panic("TODO implement writeToMemory for extern unions"),
+ .Auto => return error.IllDefinedMemoryLayout,
+ .Extern => return error.Unimplemented,
.Packed => {
const byte_count = (@intCast(usize, ty.bitSize(target)) + 7) / 8;
return writeToPackedMemory(val, ty, mod, buffer[0..byte_count], 0);
},
},
.Pointer => {
- assert(!ty.isSlice()); // No well defined layout.
+ if (ty.isSlice()) return error.IllDefinedMemoryLayout;
if (val.isDeclRef()) return error.ReinterpretDeclRef;
return val.writeToMemory(Type.usize, mod, buffer);
},
.Optional => {
- assert(ty.isPtrLikeOptional());
+ if (!ty.isPtrLikeOptional()) return error.IllDefinedMemoryLayout;
var buf: Type.Payload.ElemType = undefined;
const child = ty.optionalChild(&buf);
const opt_val = val.optionalValue();
@@ -1389,7 +1393,7 @@ pub const Value = extern union {
return writeToMemory(Value.zero, Type.usize, mod, buffer);
}
},
- else => @panic("TODO implement writeToMemory for more types"),
+ else => return error.Unimplemented,
}
}
@@ -2785,6 +2789,7 @@ pub const Value = extern union {
.field_ptr => isComptimeMutablePtr(val.castTag(.field_ptr).?.data.container_ptr),
.eu_payload_ptr => isComptimeMutablePtr(val.castTag(.eu_payload_ptr).?.data.container_ptr),
.opt_payload_ptr => isComptimeMutablePtr(val.castTag(.opt_payload_ptr).?.data.container_ptr),
+ .slice => isComptimeMutablePtr(val.castTag(.slice).?.data.ptr),
else => false,
};
@@ -5381,6 +5386,36 @@ pub const Value = extern union {
}
}
+ /// If the value is represented in-memory as a series of bytes that all
+ /// have the same value, return that byte value, otherwise null.
+ pub fn hasRepeatedByteRepr(val: Value, ty: Type, mod: *Module, value_buffer: *Payload.U64) !?Value {
+ const target = mod.getTarget();
+ const abi_size = ty.abiSize(target);
+ assert(abi_size >= 1);
+ const byte_buffer = try mod.gpa.alloc(u8, abi_size);
+ defer mod.gpa.free(byte_buffer);
+
+ writeToMemory(val, ty, mod, byte_buffer) catch |err| switch (err) {
+ error.ReinterpretDeclRef => return null,
+ // TODO: The writeToMemory function was originally created for the purpose
+ // of comptime pointer casting. However, it is now additionally being used
+ // for checking the actual memory layout that will be generated by machine
+ // code late in compilation. So, this error handling is too aggressive and
+ // causes some false negatives, causing less-than-ideal code generation.
+ error.IllDefinedMemoryLayout => return null,
+ error.Unimplemented => return null,
+ };
+ const first_byte = byte_buffer[0];
+ for (byte_buffer[1..]) |byte| {
+ if (byte != first_byte) return null;
+ }
+ value_buffer.* = .{
+ .base = .{ .tag = .int_u64 },
+ .data = first_byte,
+ };
+ return initPayload(&value_buffer.base);
+ }
+
/// This type is not copyable since it may contain pointers to its inner data.
pub const Payload = struct {
tag: Tag,
diff --git a/stage1/zig.h b/stage1/zig.h
index f73dfb72ef..34407d8cda 100644
--- a/stage1/zig.h
+++ b/stage1/zig.h
@@ -188,6 +188,14 @@ typedef char bool;
#define zig_export(sig, symbol, name) __asm(name " = " symbol)
#endif
+#if zig_has_attribute(weak) || defined(zig_gnuc)
+#define zig_weak_linkage __attribute__((weak))
+#elif _MSC_VER
+#define zig_weak_linkage __declspec(selectany)
+#else
+#define zig_weak_linkage zig_weak_linkage_unavailable
+#endif
+
#if zig_has_builtin(trap)
#define zig_trap() __builtin_trap()
#elif _MSC_VER && (_M_IX86 || _M_X64)
diff --git a/stage1/zig1.wasm b/stage1/zig1.wasm
index 4c3b17230f..b00227e45d 100644
--- a/stage1/zig1.wasm
+++ b/stage1/zig1.wasm
Binary files differ
diff --git a/test/behavior.zig b/test/behavior.zig
index b6fe8d120f..70293bf45d 100644
--- a/test/behavior.zig
+++ b/test/behavior.zig
@@ -177,6 +177,8 @@ test {
_ = @import("behavior/math.zig");
_ = @import("behavior/maximum_minimum.zig");
_ = @import("behavior/member_func.zig");
+ _ = @import("behavior/memcpy.zig");
+ _ = @import("behavior/memset.zig");
_ = @import("behavior/merge_error_sets.zig");
_ = @import("behavior/muladd.zig");
_ = @import("behavior/namespace_depends_on_compile_var.zig");
diff --git a/test/behavior/basic.zig b/test/behavior/basic.zig
index 06b1fdda64..437b1b1373 100644
--- a/test/behavior/basic.zig
+++ b/test/behavior/basic.zig
@@ -353,96 +353,6 @@ fn f2(x: bool) []const u8 {
return (if (x) &fA else &fB)();
}
-test "@memset on array pointers" {
- if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_wasm) {
- // TODO: implement memset when element ABI size > 1
- return error.SkipZigTest;
- }
-
- try testMemsetArray();
- try comptime testMemsetArray();
-}
-
-fn testMemsetArray() !void {
- {
- // memset array to non-undefined, ABI size == 1
- var foo: [20]u8 = undefined;
- @memset(&foo, 'A');
- try expect(foo[0] == 'A');
- try expect(foo[11] == 'A');
- try expect(foo[19] == 'A');
- }
- {
- // memset array to non-undefined, ABI size > 1
- var foo: [20]u32 = undefined;
- @memset(&foo, 1234);
- try expect(foo[0] == 1234);
- try expect(foo[11] == 1234);
- try expect(foo[19] == 1234);
- }
-}
-
-test "@memset on slices" {
- if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_wasm) {
- // TODO: implement memset when element ABI size > 1
- // TODO: implement memset on slices
- return error.SkipZigTest;
- }
-
- try testMemsetSlice();
- try comptime testMemsetSlice();
-}
-
-fn testMemsetSlice() !void {
- {
- // memset slice to non-undefined, ABI size == 1
- var array: [20]u8 = undefined;
- var len = array.len;
- var slice = array[0..len];
- @memset(slice, 'A');
- try expect(slice[0] == 'A');
- try expect(slice[11] == 'A');
- try expect(slice[19] == 'A');
- }
- {
- // memset slice to non-undefined, ABI size > 1
- var array: [20]u32 = undefined;
- var len = array.len;
- var slice = array[0..len];
- @memset(slice, 1234);
- try expect(slice[0] == 1234);
- try expect(slice[11] == 1234);
- try expect(slice[19] == 1234);
- }
-}
-
-test "memcpy and memset intrinsics" {
- if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
- if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
-
- try testMemcpyMemset();
- try comptime testMemcpyMemset();
-}
-
-fn testMemcpyMemset() !void {
- var foo: [20]u8 = undefined;
- var bar: [20]u8 = undefined;
-
- @memset(&foo, 'A');
- @memcpy(&bar, &foo);
-
- try expect(bar[0] == 'A');
- try expect(bar[11] == 'A');
- try expect(bar[19] == 'A');
-}
-
test "variable is allowed to be a pointer to an opaque type" {
var x: i32 = 1234;
_ = hereIsAnOpaqueType(@ptrCast(*OpaqueA, &x));
diff --git a/test/behavior/memcpy.zig b/test/behavior/memcpy.zig
new file mode 100644
index 0000000000..1d99a7163b
--- /dev/null
+++ b/test/behavior/memcpy.zig
@@ -0,0 +1,44 @@
+const std = @import("std");
+const builtin = @import("builtin");
+const expect = std.testing.expect;
+
+test "memcpy and memset intrinsics" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
+
+ try testMemcpyMemset();
+ try comptime testMemcpyMemset();
+}
+
+fn testMemcpyMemset() !void {
+ var foo: [20]u8 = undefined;
+ var bar: [20]u8 = undefined;
+
+ @memset(&foo, 'A');
+ @memcpy(&bar, &foo);
+
+ try expect(bar[0] == 'A');
+ try expect(bar[11] == 'A');
+ try expect(bar[19] == 'A');
+}
+
+test "@memcpy with both operands single-ptr-to-array, one is null-terminated" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
+
+ try testMemcpyBothSinglePtrArrayOneIsNullTerminated();
+ try comptime testMemcpyBothSinglePtrArrayOneIsNullTerminated();
+}
+
+fn testMemcpyBothSinglePtrArrayOneIsNullTerminated() !void {
+ var buf: [100]u8 = undefined;
+ const suffix = "hello";
+ @memcpy(buf[buf.len - suffix.len ..], suffix);
+ try expect(buf[95] == 'h');
+ try expect(buf[96] == 'e');
+ try expect(buf[97] == 'l');
+ try expect(buf[98] == 'l');
+ try expect(buf[99] == 'o');
+}
diff --git a/test/behavior/memset.zig b/test/behavior/memset.zig
new file mode 100644
index 0000000000..2cc390a3c9
--- /dev/null
+++ b/test/behavior/memset.zig
@@ -0,0 +1,146 @@
+const std = @import("std");
+const builtin = @import("builtin");
+const expect = std.testing.expect;
+
+test "@memset on array pointers" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_wasm) {
+ // TODO: implement memset when element ABI size > 1
+ return error.SkipZigTest;
+ }
+
+ try testMemsetArray();
+ try comptime testMemsetArray();
+}
+
+fn testMemsetArray() !void {
+ {
+ // memset array to non-undefined, ABI size == 1
+ var foo: [20]u8 = undefined;
+ @memset(&foo, 'A');
+ try expect(foo[0] == 'A');
+ try expect(foo[11] == 'A');
+ try expect(foo[19] == 'A');
+ }
+ {
+ // memset array to non-undefined, ABI size > 1
+ var foo: [20]u32 = undefined;
+ @memset(&foo, 1234);
+ try expect(foo[0] == 1234);
+ try expect(foo[11] == 1234);
+ try expect(foo[19] == 1234);
+ }
+}
+
+test "@memset on slices" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_wasm) {
+ // TODO: implement memset when element ABI size > 1
+ // TODO: implement memset on slices
+ return error.SkipZigTest;
+ }
+
+ try testMemsetSlice();
+ try comptime testMemsetSlice();
+}
+
+fn testMemsetSlice() !void {
+ {
+ // memset slice to non-undefined, ABI size == 1
+ var array: [20]u8 = undefined;
+ var len = array.len;
+ var slice = array[0..len];
+ @memset(slice, 'A');
+ try expect(slice[0] == 'A');
+ try expect(slice[11] == 'A');
+ try expect(slice[19] == 'A');
+ }
+ {
+ // memset slice to non-undefined, ABI size > 1
+ var array: [20]u32 = undefined;
+ var len = array.len;
+ var slice = array[0..len];
+ @memset(slice, 1234);
+ try expect(slice[0] == 1234);
+ try expect(slice[11] == 1234);
+ try expect(slice[19] == 1234);
+ }
+}
+
+test "memset with bool element" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
+
+ var buf: [5]bool = undefined;
+ @memset(&buf, true);
+ try expect(buf[2]);
+ try expect(buf[4]);
+}
+
+test "memset with 1-byte struct element" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
+
+ const S = struct { x: bool };
+ var buf: [5]S = undefined;
+ @memset(&buf, .{ .x = true });
+ try expect(buf[2].x);
+ try expect(buf[4].x);
+}
+
+test "memset with 1-byte array element" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
+
+ const A = [1]bool;
+ var buf: [5]A = undefined;
+ @memset(&buf, .{true});
+ try expect(buf[2][0]);
+ try expect(buf[4][0]);
+}
+
+test "memset with large array element, runtime known" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
+
+ const A = [128]u64;
+ var buf: [5]A = undefined;
+ var runtime_known_element = [_]u64{0} ** 128;
+ @memset(&buf, runtime_known_element);
+ for (buf[0]) |elem| try expect(elem == 0);
+ for (buf[1]) |elem| try expect(elem == 0);
+ for (buf[2]) |elem| try expect(elem == 0);
+ for (buf[3]) |elem| try expect(elem == 0);
+ for (buf[4]) |elem| try expect(elem == 0);
+}
+
+test "memset with large array element, comptime known" {
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
+ if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
+
+ const A = [128]u64;
+ var buf: [5]A = undefined;
+ const comptime_known_element = [_]u64{0} ** 128;
+ @memset(&buf, comptime_known_element);
+ for (buf[0]) |elem| try expect(elem == 0);
+ for (buf[1]) |elem| try expect(elem == 0);
+ for (buf[2]) |elem| try expect(elem == 0);
+ for (buf[3]) |elem| try expect(elem == 0);
+ for (buf[4]) |elem| try expect(elem == 0);
+}