mirror of
https://github.com/ziglang/zig.git
synced 2026-02-12 20:37:54 +00:00
std: update to use @memcpy directly
This commit is contained in:
parent
73d3fb9883
commit
125221cce9
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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");
|
||||
|
||||
@ -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)
|
||||
@ -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);
|
||||
}
|
||||
@ -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) {
|
||||
|
||||
@ -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;
|
||||
@ -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)
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
@ -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) {
|
||||
|
||||
@ -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(
|
||||
|
||||
@ -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;
|
||||
}
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
},
|
||||
};
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -107,7 +107,7 @@ pub const Address = extern union {
|
||||
if (path.len + 1 > sock_addr.path.len) return error.NameTooLong;
|
||||
|
||||
@memset(&sock_addr.path, 0);
|
||||
mem.copy(u8, &sock_addr.path, path);
|
||||
@memcpy(sock_addr.path[0..path.len], path);
|
||||
|
||||
return Address{ .un = sock_addr };
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -1856,7 +1856,7 @@ test "write_fixed/read_fixed" {
|
||||
var raw_buffers: [2][11]u8 = undefined;
|
||||
// First buffer will be written to the file.
|
||||
@memset(&raw_buffers[0], 'z');
|
||||
std.mem.copy(u8, &raw_buffers[0], "foobar");
|
||||
raw_buffers[0][0.."foobar".len].* = "foobar".*;
|
||||
|
||||
var buffers = [2]os.iovec{
|
||||
.{ .iov_base = &raw_buffers[0], .iov_len = raw_buffers[0].len },
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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).
|
||||
|
||||
@ -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);
|
||||
@memcpy(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.
|
||||
|
||||
@ -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.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user