diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-03-19 18:06:16 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-19 18:06:16 -0400 |
| commit | dc04e97098010f590d109e6e70d4afe79cd8f01b (patch) | |
| tree | bed11818fd80fe7b4557f4253c8d5562de773624 /lib/std | |
| parent | 555a2c03286507ffe4bd3bea2154dbfb719ebef1 (diff) | |
| parent | 160367e0ddcb36b6957e603d869507b9d7542edc (diff) | |
| download | zig-dc04e97098010f590d109e6e70d4afe79cd8f01b.tar.gz zig-dc04e97098010f590d109e6e70d4afe79cd8f01b.zip | |
Merge pull request #4752 from ziglang/slice-array
slicing with comptime start and end indexes results in pointer-to-array
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/crypto/aes.zig | 38 | ||||
| -rw-r--r-- | lib/std/crypto/blake2.zig | 11 | ||||
| -rw-r--r-- | lib/std/crypto/chacha20.zig | 61 | ||||
| -rw-r--r-- | lib/std/crypto/md5.zig | 3 | ||||
| -rw-r--r-- | lib/std/crypto/poly1305.zig | 29 | ||||
| -rw-r--r-- | lib/std/crypto/sha1.zig | 3 | ||||
| -rw-r--r-- | lib/std/crypto/sha2.zig | 6 | ||||
| -rw-r--r-- | lib/std/crypto/sha3.zig | 5 | ||||
| -rw-r--r-- | lib/std/crypto/x25519.zig | 41 | ||||
| -rw-r--r-- | lib/std/fmt.zig | 3 | ||||
| -rw-r--r-- | lib/std/fs.zig | 2 | ||||
| -rw-r--r-- | lib/std/hash/auto_hash.zig | 12 | ||||
| -rw-r--r-- | lib/std/hash/siphash.zig | 6 | ||||
| -rw-r--r-- | lib/std/hash/wyhash.zig | 2 | ||||
| -rw-r--r-- | lib/std/json.zig | 23 | ||||
| -rw-r--r-- | lib/std/mem.zig | 113 | ||||
| -rw-r--r-- | lib/std/meta.zig | 65 | ||||
| -rw-r--r-- | lib/std/meta/trait.zig | 12 | ||||
| -rw-r--r-- | lib/std/net.zig | 12 | ||||
| -rw-r--r-- | lib/std/os/windows.zig | 10 | ||||
| -rw-r--r-- | lib/std/rand.zig | 2 | ||||
| -rw-r--r-- | lib/std/unicode.zig | 20 |
22 files changed, 254 insertions, 225 deletions
diff --git a/lib/std/crypto/aes.zig b/lib/std/crypto/aes.zig index 1cc166f943..81dc56f0b3 100644 --- a/lib/std/crypto/aes.zig +++ b/lib/std/crypto/aes.zig @@ -15,10 +15,10 @@ fn rotw(w: u32) u32 { // Encrypt one block from src into dst, using the expanded key xk. fn encryptBlock(xk: []const u32, dst: []u8, src: []const u8) void { - var s0 = mem.readIntSliceBig(u32, src[0..4]); - var s1 = mem.readIntSliceBig(u32, src[4..8]); - var s2 = mem.readIntSliceBig(u32, src[8..12]); - var s3 = mem.readIntSliceBig(u32, src[12..16]); + var s0 = mem.readIntBig(u32, src[0..4]); + var s1 = mem.readIntBig(u32, src[4..8]); + var s2 = mem.readIntBig(u32, src[8..12]); + var s3 = mem.readIntBig(u32, src[12..16]); // First round just XORs input with key. s0 ^= xk[0]; @@ -58,18 +58,18 @@ fn encryptBlock(xk: []const u32, dst: []u8, src: []const u8) void { s2 ^= xk[k + 2]; s3 ^= xk[k + 3]; - mem.writeIntSliceBig(u32, dst[0..4], s0); - mem.writeIntSliceBig(u32, dst[4..8], s1); - mem.writeIntSliceBig(u32, dst[8..12], s2); - mem.writeIntSliceBig(u32, dst[12..16], s3); + mem.writeIntBig(u32, dst[0..4], s0); + mem.writeIntBig(u32, dst[4..8], s1); + mem.writeIntBig(u32, dst[8..12], s2); + mem.writeIntBig(u32, dst[12..16], s3); } // Decrypt one block from src into dst, using the expanded key xk. pub fn decryptBlock(xk: []const u32, dst: []u8, src: []const u8) void { - var s0 = mem.readIntSliceBig(u32, src[0..4]); - var s1 = mem.readIntSliceBig(u32, src[4..8]); - var s2 = mem.readIntSliceBig(u32, src[8..12]); - var s3 = mem.readIntSliceBig(u32, src[12..16]); + var s0 = mem.readIntBig(u32, src[0..4]); + var s1 = mem.readIntBig(u32, src[4..8]); + var s2 = mem.readIntBig(u32, src[8..12]); + var s3 = mem.readIntBig(u32, src[12..16]); // First round just XORs input with key. s0 ^= xk[0]; @@ -109,10 +109,10 @@ pub fn decryptBlock(xk: []const u32, dst: []u8, src: []const u8) void { s2 ^= xk[k + 2]; s3 ^= xk[k + 3]; - mem.writeIntSliceBig(u32, dst[0..4], s0); - mem.writeIntSliceBig(u32, dst[4..8], s1); - mem.writeIntSliceBig(u32, dst[8..12], s2); - mem.writeIntSliceBig(u32, dst[12..16], s3); + mem.writeIntBig(u32, dst[0..4], s0); + mem.writeIntBig(u32, dst[4..8], s1); + mem.writeIntBig(u32, dst[8..12], s2); + mem.writeIntBig(u32, dst[12..16], s3); } fn xorBytes(dst: []u8, a: []const u8, b: []const u8) usize { @@ -154,8 +154,8 @@ fn AES(comptime keysize: usize) type { var n: usize = 0; while (n < src.len) { ctx.encrypt(keystream[0..], ctrbuf[0..]); - var ctr_i = std.mem.readIntSliceBig(u128, ctrbuf[0..]); - std.mem.writeIntSliceBig(u128, ctrbuf[0..], ctr_i +% 1); + var ctr_i = std.mem.readIntBig(u128, ctrbuf[0..]); + std.mem.writeIntBig(u128, ctrbuf[0..], ctr_i +% 1); n += xorBytes(dst[n..], src[n..], &keystream); } @@ -251,7 +251,7 @@ fn expandKey(key: []const u8, enc: []u32, dec: []u32) void { var i: usize = 0; var nk = key.len / 4; while (i < nk) : (i += 1) { - enc[i] = mem.readIntSliceBig(u32, key[4 * i .. 4 * i + 4]); + enc[i] = mem.readIntBig(u32, key[4 * i ..][0..4]); } while (i < enc.len) : (i += 1) { var t = enc[i - 1]; diff --git a/lib/std/crypto/blake2.zig b/lib/std/crypto/blake2.zig index e03d8f7dab..fc1d59290e 100644 --- a/lib/std/crypto/blake2.zig +++ b/lib/std/crypto/blake2.zig @@ -123,8 +123,7 @@ fn Blake2s(comptime out_len: usize) type { const rr = d.h[0 .. out_len / 32]; for (rr) |s, j| { - // TODO https://github.com/ziglang/zig/issues/863 - mem.writeIntSliceLittle(u32, out[4 * j .. 4 * j + 4], s); + mem.writeIntLittle(u32, out[4 * j ..][0..4], s); } } @@ -135,8 +134,7 @@ fn Blake2s(comptime out_len: usize) type { var v: [16]u32 = undefined; for (m) |*r, i| { - // TODO https://github.com/ziglang/zig/issues/863 - r.* = mem.readIntSliceLittle(u32, b[4 * i .. 4 * i + 4]); + r.* = mem.readIntLittle(u32, b[4 * i ..][0..4]); } var k: usize = 0; @@ -358,8 +356,7 @@ fn Blake2b(comptime out_len: usize) type { const rr = d.h[0 .. out_len / 64]; for (rr) |s, j| { - // TODO https://github.com/ziglang/zig/issues/863 - mem.writeIntSliceLittle(u64, out[8 * j .. 8 * j + 8], s); + mem.writeIntLittle(u64, out[8 * j ..][0..8], s); } } @@ -370,7 +367,7 @@ fn Blake2b(comptime out_len: usize) type { var v: [16]u64 = undefined; for (m) |*r, i| { - r.* = mem.readIntSliceLittle(u64, b[8 * i .. 8 * i + 8]); + r.* = mem.readIntLittle(u64, b[8 * i ..][0..8]); } var k: usize = 0; diff --git a/lib/std/crypto/chacha20.zig b/lib/std/crypto/chacha20.zig index d67877b051..f6008745af 100644 --- a/lib/std/crypto/chacha20.zig +++ b/lib/std/crypto/chacha20.zig @@ -61,8 +61,7 @@ fn salsa20_wordtobyte(out: []u8, input: [16]u32) void { } for (x) |_, i| { - // TODO https://github.com/ziglang/zig/issues/863 - mem.writeIntSliceLittle(u32, out[4 * i .. 4 * i + 4], x[i] +% input[i]); + mem.writeIntLittle(u32, out[4 * i ..][0..4], x[i] +% input[i]); } } @@ -73,10 +72,10 @@ fn chaCha20_internal(out: []u8, in: []const u8, key: [8]u32, counter: [4]u32) vo const c = "expand 32-byte k"; const constant_le = [_]u32{ - mem.readIntSliceLittle(u32, c[0..4]), - mem.readIntSliceLittle(u32, c[4..8]), - mem.readIntSliceLittle(u32, c[8..12]), - mem.readIntSliceLittle(u32, c[12..16]), + mem.readIntLittle(u32, c[0..4]), + mem.readIntLittle(u32, c[4..8]), + mem.readIntLittle(u32, c[8..12]), + mem.readIntLittle(u32, c[12..16]), }; mem.copy(u32, ctx[0..], constant_le[0..4]); @@ -120,19 +119,19 @@ pub fn chaCha20IETF(out: []u8, in: []const u8, counter: u32, key: [32]u8, nonce: var k: [8]u32 = undefined; var c: [4]u32 = undefined; - k[0] = mem.readIntSliceLittle(u32, key[0..4]); - k[1] = mem.readIntSliceLittle(u32, key[4..8]); - k[2] = mem.readIntSliceLittle(u32, key[8..12]); - k[3] = mem.readIntSliceLittle(u32, key[12..16]); - k[4] = mem.readIntSliceLittle(u32, key[16..20]); - k[5] = mem.readIntSliceLittle(u32, key[20..24]); - k[6] = mem.readIntSliceLittle(u32, key[24..28]); - k[7] = mem.readIntSliceLittle(u32, key[28..32]); + k[0] = mem.readIntLittle(u32, key[0..4]); + k[1] = mem.readIntLittle(u32, key[4..8]); + k[2] = mem.readIntLittle(u32, key[8..12]); + k[3] = mem.readIntLittle(u32, key[12..16]); + k[4] = mem.readIntLittle(u32, key[16..20]); + k[5] = mem.readIntLittle(u32, key[20..24]); + k[6] = mem.readIntLittle(u32, key[24..28]); + k[7] = mem.readIntLittle(u32, key[28..32]); c[0] = counter; - c[1] = mem.readIntSliceLittle(u32, nonce[0..4]); - c[2] = mem.readIntSliceLittle(u32, nonce[4..8]); - c[3] = mem.readIntSliceLittle(u32, nonce[8..12]); + c[1] = mem.readIntLittle(u32, nonce[0..4]); + c[2] = mem.readIntLittle(u32, nonce[4..8]); + c[3] = mem.readIntLittle(u32, nonce[8..12]); chaCha20_internal(out, in, k, c); } @@ -147,19 +146,19 @@ pub fn chaCha20With64BitNonce(out: []u8, in: []const u8, counter: u64, key: [32] var k: [8]u32 = undefined; var c: [4]u32 = undefined; - k[0] = mem.readIntSliceLittle(u32, key[0..4]); - k[1] = mem.readIntSliceLittle(u32, key[4..8]); - k[2] = mem.readIntSliceLittle(u32, key[8..12]); - k[3] = mem.readIntSliceLittle(u32, key[12..16]); - k[4] = mem.readIntSliceLittle(u32, key[16..20]); - k[5] = mem.readIntSliceLittle(u32, key[20..24]); - k[6] = mem.readIntSliceLittle(u32, key[24..28]); - k[7] = mem.readIntSliceLittle(u32, key[28..32]); + k[0] = mem.readIntLittle(u32, key[0..4]); + k[1] = mem.readIntLittle(u32, key[4..8]); + k[2] = mem.readIntLittle(u32, key[8..12]); + k[3] = mem.readIntLittle(u32, key[12..16]); + k[4] = mem.readIntLittle(u32, key[16..20]); + k[5] = mem.readIntLittle(u32, key[20..24]); + k[6] = mem.readIntLittle(u32, key[24..28]); + k[7] = mem.readIntLittle(u32, key[28..32]); c[0] = @truncate(u32, counter); c[1] = @truncate(u32, counter >> 32); - c[2] = mem.readIntSliceLittle(u32, nonce[0..4]); - c[3] = mem.readIntSliceLittle(u32, nonce[4..8]); + c[2] = mem.readIntLittle(u32, nonce[0..4]); + c[3] = mem.readIntLittle(u32, nonce[4..8]); const block_size = (1 << 6); // The full block size is greater than the address space on a 32bit machine @@ -463,8 +462,8 @@ pub fn chacha20poly1305Seal(dst: []u8, plaintext: []const u8, data: []const u8, mac.update(zeros[0..padding]); } var lens: [16]u8 = undefined; - mem.writeIntSliceLittle(u64, lens[0..8], data.len); - mem.writeIntSliceLittle(u64, lens[8..16], plaintext.len); + mem.writeIntLittle(u64, lens[0..8], data.len); + mem.writeIntLittle(u64, lens[8..16], plaintext.len); mac.update(lens[0..]); mac.final(dst[plaintext.len..]); } @@ -500,8 +499,8 @@ pub fn chacha20poly1305Open(dst: []u8, msgAndTag: []const u8, data: []const u8, mac.update(zeros[0..padding]); } var lens: [16]u8 = undefined; - mem.writeIntSliceLittle(u64, lens[0..8], data.len); - mem.writeIntSliceLittle(u64, lens[8..16], ciphertext.len); + mem.writeIntLittle(u64, lens[0..8], data.len); + mem.writeIntLittle(u64, lens[8..16], ciphertext.len); mac.update(lens[0..]); var computedTag: [16]u8 = undefined; mac.final(computedTag[0..]); diff --git a/lib/std/crypto/md5.zig b/lib/std/crypto/md5.zig index d9dd08c904..ac8948ca20 100644 --- a/lib/std/crypto/md5.zig +++ b/lib/std/crypto/md5.zig @@ -112,8 +112,7 @@ pub const Md5 = struct { d.round(d.buf[0..]); for (d.s) |s, j| { - // TODO https://github.com/ziglang/zig/issues/863 - mem.writeIntSliceLittle(u32, out[4 * j .. 4 * j + 4], s); + mem.writeIntLittle(u32, out[4 * j ..][0..4], s); } } diff --git a/lib/std/crypto/poly1305.zig b/lib/std/crypto/poly1305.zig index 2395b1c7aa..fda978307d 100644 --- a/lib/std/crypto/poly1305.zig +++ b/lib/std/crypto/poly1305.zig @@ -3,11 +3,11 @@ // https://monocypher.org/ const std = @import("../std.zig"); -const builtin = @import("builtin"); +const builtin = std.builtin; const Endian = builtin.Endian; -const readIntSliceLittle = std.mem.readIntSliceLittle; -const writeIntSliceLittle = std.mem.writeIntSliceLittle; +const readIntLittle = std.mem.readIntLittle; +const writeIntLittle = std.mem.writeIntLittle; pub const Poly1305 = struct { const Self = @This(); @@ -59,19 +59,19 @@ pub const Poly1305 = struct { { var i: usize = 0; while (i < 1) : (i += 1) { - ctx.r[0] = readIntSliceLittle(u32, key[0..4]) & 0x0fffffff; + ctx.r[0] = readIntLittle(u32, key[0..4]) & 0x0fffffff; } } { var i: usize = 1; while (i < 4) : (i += 1) { - ctx.r[i] = readIntSliceLittle(u32, key[i * 4 .. i * 4 + 4]) & 0x0ffffffc; + ctx.r[i] = readIntLittle(u32, key[i * 4 ..][0..4]) & 0x0ffffffc; } } { var i: usize = 0; while (i < 4) : (i += 1) { - ctx.pad[i] = readIntSliceLittle(u32, key[i * 4 + 16 .. i * 4 + 16 + 4]); + ctx.pad[i] = readIntLittle(u32, key[i * 4 + 16 ..][0..4]); } } @@ -168,10 +168,10 @@ pub const Poly1305 = struct { const nb_blocks = nmsg.len >> 4; var i: usize = 0; while (i < nb_blocks) : (i += 1) { - ctx.c[0] = readIntSliceLittle(u32, nmsg[0..4]); - ctx.c[1] = readIntSliceLittle(u32, nmsg[4..8]); - ctx.c[2] = readIntSliceLittle(u32, nmsg[8..12]); - ctx.c[3] = readIntSliceLittle(u32, nmsg[12..16]); + ctx.c[0] = readIntLittle(u32, nmsg[0..4]); + ctx.c[1] = readIntLittle(u32, nmsg[4..8]); + ctx.c[2] = readIntLittle(u32, nmsg[8..12]); + ctx.c[3] = readIntLittle(u32, nmsg[12..16]); polyBlock(ctx); nmsg = nmsg[16..]; } @@ -210,11 +210,10 @@ pub const Poly1305 = struct { const uu2 = (uu1 >> 32) + ctx.h[2] + ctx.pad[2]; // <= 2_00000000 const uu3 = (uu2 >> 32) + ctx.h[3] + ctx.pad[3]; // <= 2_00000000 - // TODO https://github.com/ziglang/zig/issues/863 - writeIntSliceLittle(u32, out[0..], @truncate(u32, uu0)); - writeIntSliceLittle(u32, out[4..], @truncate(u32, uu1)); - writeIntSliceLittle(u32, out[8..], @truncate(u32, uu2)); - writeIntSliceLittle(u32, out[12..], @truncate(u32, uu3)); + writeIntLittle(u32, out[0..4], @truncate(u32, uu0)); + writeIntLittle(u32, out[4..8], @truncate(u32, uu1)); + writeIntLittle(u32, out[8..12], @truncate(u32, uu2)); + writeIntLittle(u32, out[12..16], @truncate(u32, uu3)); ctx.secureZero(); } diff --git a/lib/std/crypto/sha1.zig b/lib/std/crypto/sha1.zig index 5be42180a1..6edf7b745e 100644 --- a/lib/std/crypto/sha1.zig +++ b/lib/std/crypto/sha1.zig @@ -109,8 +109,7 @@ pub const Sha1 = struct { d.round(d.buf[0..]); for (d.s) |s, j| { - // TODO https://github.com/ziglang/zig/issues/863 - mem.writeIntSliceBig(u32, out[4 * j .. 4 * j + 4], s); + mem.writeIntBig(u32, out[4 * j ..][0..4], s); } } diff --git a/lib/std/crypto/sha2.zig b/lib/std/crypto/sha2.zig index fd7ad532a3..f004bceac3 100644 --- a/lib/std/crypto/sha2.zig +++ b/lib/std/crypto/sha2.zig @@ -167,8 +167,7 @@ fn Sha2_32(comptime params: Sha2Params32) type { const rr = d.s[0 .. params.out_len / 32]; for (rr) |s, j| { - // TODO https://github.com/ziglang/zig/issues/863 - mem.writeIntSliceBig(u32, out[4 * j .. 4 * j + 4], s); + mem.writeIntBig(u32, out[4 * j ..][0..4], s); } } @@ -509,8 +508,7 @@ fn Sha2_64(comptime params: Sha2Params64) type { const rr = d.s[0 .. params.out_len / 64]; for (rr) |s, j| { - // TODO https://github.com/ziglang/zig/issues/863 - mem.writeIntSliceBig(u64, out[8 * j .. 8 * j + 8], s); + mem.writeIntBig(u64, out[8 * j ..][0..8], s); } } diff --git a/lib/std/crypto/sha3.zig b/lib/std/crypto/sha3.zig index d7b2fbe256..7c60674d75 100644 --- a/lib/std/crypto/sha3.zig +++ b/lib/std/crypto/sha3.zig @@ -120,7 +120,7 @@ fn keccak_f(comptime F: usize, d: []u8) void { var c = [_]u64{0} ** 5; for (s) |*r, i| { - r.* = mem.readIntSliceLittle(u64, d[8 * i .. 8 * i + 8]); + r.* = mem.readIntLittle(u64, d[8 * i ..][0..8]); } comptime var x: usize = 0; @@ -167,8 +167,7 @@ fn keccak_f(comptime F: usize, d: []u8) void { } for (s) |r, i| { - // TODO https://github.com/ziglang/zig/issues/863 - mem.writeIntSliceLittle(u64, d[8 * i .. 8 * i + 8], r); + mem.writeIntLittle(u64, d[8 * i ..][0..8], r); } } diff --git a/lib/std/crypto/x25519.zig b/lib/std/crypto/x25519.zig index 16e3f073f8..e2e2bf90e5 100644 --- a/lib/std/crypto/x25519.zig +++ b/lib/std/crypto/x25519.zig @@ -7,8 +7,8 @@ const builtin = @import("builtin"); const fmt = std.fmt; const Endian = builtin.Endian; -const readIntSliceLittle = std.mem.readIntSliceLittle; -const writeIntSliceLittle = std.mem.writeIntSliceLittle; +const readIntLittle = std.mem.readIntLittle; +const writeIntLittle = std.mem.writeIntLittle; // Based on Supercop's ref10 implementation. pub const X25519 = struct { @@ -255,16 +255,16 @@ const Fe = struct { var t: [10]i64 = undefined; - t[0] = readIntSliceLittle(u32, s[0..4]); - t[1] = @as(u32, readIntSliceLittle(u24, s[4..7])) << 6; - t[2] = @as(u32, readIntSliceLittle(u24, s[7..10])) << 5; - t[3] = @as(u32, readIntSliceLittle(u24, s[10..13])) << 3; - t[4] = @as(u32, readIntSliceLittle(u24, s[13..16])) << 2; - t[5] = readIntSliceLittle(u32, s[16..20]); - t[6] = @as(u32, readIntSliceLittle(u24, s[20..23])) << 7; - t[7] = @as(u32, readIntSliceLittle(u24, s[23..26])) << 5; - t[8] = @as(u32, readIntSliceLittle(u24, s[26..29])) << 4; - t[9] = (@as(u32, readIntSliceLittle(u24, s[29..32])) & 0x7fffff) << 2; + t[0] = readIntLittle(u32, s[0..4]); + t[1] = @as(u32, readIntLittle(u24, s[4..7])) << 6; + t[2] = @as(u32, readIntLittle(u24, s[7..10])) << 5; + t[3] = @as(u32, readIntLittle(u24, s[10..13])) << 3; + t[4] = @as(u32, readIntLittle(u24, s[13..16])) << 2; + t[5] = readIntLittle(u32, s[16..20]); + t[6] = @as(u32, readIntLittle(u24, s[20..23])) << 7; + t[7] = @as(u32, readIntLittle(u24, s[23..26])) << 5; + t[8] = @as(u32, readIntLittle(u24, s[26..29])) << 4; + t[9] = (@as(u32, readIntLittle(u24, s[29..32])) & 0x7fffff) << 2; carry1(h, t[0..]); } @@ -544,15 +544,14 @@ const Fe = struct { ut[i] = @bitCast(u32, @intCast(i32, t[i])); } - // TODO https://github.com/ziglang/zig/issues/863 - writeIntSliceLittle(u32, s[0..4], (ut[0] >> 0) | (ut[1] << 26)); - writeIntSliceLittle(u32, s[4..8], (ut[1] >> 6) | (ut[2] << 19)); - writeIntSliceLittle(u32, s[8..12], (ut[2] >> 13) | (ut[3] << 13)); - writeIntSliceLittle(u32, s[12..16], (ut[3] >> 19) | (ut[4] << 6)); - writeIntSliceLittle(u32, s[16..20], (ut[5] >> 0) | (ut[6] << 25)); - writeIntSliceLittle(u32, s[20..24], (ut[6] >> 7) | (ut[7] << 19)); - writeIntSliceLittle(u32, s[24..28], (ut[7] >> 13) | (ut[8] << 12)); - writeIntSliceLittle(u32, s[28..], (ut[8] >> 20) | (ut[9] << 6)); + writeIntLittle(u32, s[0..4], (ut[0] >> 0) | (ut[1] << 26)); + writeIntLittle(u32, s[4..8], (ut[1] >> 6) | (ut[2] << 19)); + writeIntLittle(u32, s[8..12], (ut[2] >> 13) | (ut[3] << 13)); + writeIntLittle(u32, s[12..16], (ut[3] >> 19) | (ut[4] << 6)); + writeIntLittle(u32, s[16..20], (ut[5] >> 0) | (ut[6] << 25)); + writeIntLittle(u32, s[20..24], (ut[6] >> 7) | (ut[7] << 19)); + writeIntLittle(u32, s[24..28], (ut[7] >> 13) | (ut[8] << 12)); + writeIntLittle(u32, s[28..32], (ut[8] >> 20) | (ut[9] << 6)); std.mem.secureZero(i64, t[0..]); } diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 592fe9bb4e..7fc9961938 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -1223,7 +1223,8 @@ test "slice" { try testFmt("slice: abc\n", "slice: {}\n", .{value}); } { - const value = @intToPtr([*]align(1) const []const u8, 0xdeadbeef)[0..0]; + var runtime_zero: usize = 0; + const value = @intToPtr([*]align(1) const []const u8, 0xdeadbeef)[runtime_zero..runtime_zero]; try testFmt("slice: []const u8@deadbeef\n", "slice: {}\n", .{value}); } diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 53cf4b3ce9..333419d02b 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -341,7 +341,7 @@ pub const Dir = struct { if (self.index >= self.end_index) { const rc = os.system.getdirentries( self.dir.fd, - self.buf[0..].ptr, + &self.buf, self.buf.len, &self.seek, ); diff --git a/lib/std/hash/auto_hash.zig b/lib/std/hash/auto_hash.zig index 2c6b37e3db..b5d6c528a5 100644 --- a/lib/std/hash/auto_hash.zig +++ b/lib/std/hash/auto_hash.zig @@ -40,7 +40,9 @@ pub fn hashPointer(hasher: var, key: var, comptime strat: HashStrategy) void { .DeepRecursive => hashArray(hasher, key, .DeepRecursive), }, - .Many, .C, => switch (strat) { + .Many, + .C, + => switch (strat) { .Shallow => hash(hasher, @ptrToInt(key), .Shallow), else => @compileError( \\ unknown-length pointers and C pointers cannot be hashed deeply. @@ -236,9 +238,11 @@ test "hash slice shallow" { defer std.testing.allocator.destroy(array1); array1.* = [_]u32{ 1, 2, 3, 4, 5, 6 }; const array2 = [_]u32{ 1, 2, 3, 4, 5, 6 }; - const a = array1[0..]; - const b = array2[0..]; - const c = array1[0..3]; + // TODO audit deep/shallow - maybe it has the wrong behavior with respect to array pointers and slices + var runtime_zero: usize = 0; + const a = array1[runtime_zero..]; + const b = array2[runtime_zero..]; + const c = array1[runtime_zero..3]; testing.expect(testHashShallow(a) == testHashShallow(a)); testing.expect(testHashShallow(a) != testHashShallow(array1)); testing.expect(testHashShallow(a) != testHashShallow(b)); diff --git a/lib/std/hash/siphash.zig b/lib/std/hash/siphash.zig index ccef47c4b2..ebafdd6855 100644 --- a/lib/std/hash/siphash.zig +++ b/lib/std/hash/siphash.zig @@ -39,8 +39,8 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round pub fn init(key: []const u8) Self { assert(key.len >= 16); - const k0 = mem.readIntSliceLittle(u64, key[0..8]); - const k1 = mem.readIntSliceLittle(u64, key[8..16]); + const k0 = mem.readIntLittle(u64, key[0..8]); + const k1 = mem.readIntLittle(u64, key[8..16]); var d = Self{ .v0 = k0 ^ 0x736f6d6570736575, @@ -111,7 +111,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round fn round(self: *Self, b: []const u8) void { assert(b.len == 8); - const m = mem.readIntSliceLittle(u64, b[0..]); + const m = mem.readIntLittle(u64, b[0..8]); self.v3 ^= m; // TODO this is a workaround, should be able to supply the value without a separate variable diff --git a/lib/std/hash/wyhash.zig b/lib/std/hash/wyhash.zig index 8fcbbbce4c..8b7edd246f 100644 --- a/lib/std/hash/wyhash.zig +++ b/lib/std/hash/wyhash.zig @@ -11,7 +11,7 @@ const primes = [_]u64{ fn read_bytes(comptime bytes: u8, data: []const u8) u64 { const T = std.meta.IntType(false, 8 * bytes); - return mem.readIntSliceLittle(T, data[0..bytes]); + return mem.readIntLittle(T, data[0..bytes]); } fn read_8bytes_swapped(data: []const u8) u64 { diff --git a/lib/std/json.zig b/lib/std/json.zig index f5a72d86da..3dd8088785 100644 --- a/lib/std/json.zig +++ b/lib/std/json.zig @@ -2249,11 +2249,16 @@ pub const StringifyOptions = struct { // TODO: allow picking if []u8 is string or array? }; +pub const StringifyError = error{ + TooMuchData, + DifferentData, +}; + pub fn stringify( value: var, options: StringifyOptions, out_stream: var, -) !void { +) StringifyError!void { const T = @TypeOf(value); switch (@typeInfo(T)) { .Float, .ComptimeFloat => { @@ -2320,9 +2325,15 @@ pub fn stringify( return; }, .Pointer => |ptr_info| switch (ptr_info.size) { - .One => { - // TODO: avoid loops? - return try stringify(value.*, options, out_stream); + .One => switch (@typeInfo(ptr_info.child)) { + .Array => { + const Slice = []const std.meta.Elem(ptr_info.child); + return stringify(@as(Slice, value), options, out_stream); + }, + else => { + // TODO: avoid loops? + return stringify(value.*, options, out_stream); + }, }, // TODO: .Many when there is a sentinel (waiting for https://github.com/ziglang/zig/pull/3972) .Slice => { @@ -2381,9 +2392,7 @@ pub fn stringify( }, else => @compileError("Unable to stringify type '" ++ @typeName(T) ++ "'"), }, - .Array => |info| { - return try stringify(value[0..], options, out_stream); - }, + .Array => return stringify(&value, options, out_stream), else => @compileError("Unable to stringify type '" ++ @typeName(T) ++ "'"), } unreachable; diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 438c15c2eb..2f7a21c6b6 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -560,7 +560,7 @@ pub fn span(ptr: var) Span(@TypeOf(ptr)) { test "span" { var array: [5]u16 = [_]u16{ 1, 2, 3, 4, 5 }; - const ptr = array[0..2 :3].ptr; + const ptr = @as([*:3]u16, array[0..2 :3]); testing.expect(eql(u16, span(ptr), &[_]u16{ 1, 2 })); testing.expect(eql(u16, span(&array), &[_]u16{ 1, 2, 3, 4, 5 })); } @@ -602,7 +602,7 @@ test "len" { testing.expect(len(&array) == 5); testing.expect(len(array[0..3]) == 3); array[2] = 0; - const ptr = array[0..2 :0].ptr; + const ptr = @as([*:0]u16, array[0..2 :0]); testing.expect(len(ptr) == 2); } { @@ -824,8 +824,7 @@ pub const readIntBig = switch (builtin.endian) { pub fn readIntSliceNative(comptime T: type, bytes: []const u8) T { const n = @divExact(T.bit_count, 8); assert(bytes.len >= n); - // TODO https://github.com/ziglang/zig/issues/863 - return readIntNative(T, @ptrCast(*const [n]u8, bytes.ptr)); + return readIntNative(T, bytes[0..n]); } /// Asserts that bytes.len >= T.bit_count / 8. Reads the integer starting from index 0 @@ -863,8 +862,7 @@ pub fn readInt(comptime T: type, bytes: *const [@divExact(T.bit_count, 8)]u8, en pub fn readIntSlice(comptime T: type, bytes: []const u8, endian: builtin.Endian) T { const n = @divExact(T.bit_count, 8); assert(bytes.len >= n); - // TODO https://github.com/ziglang/zig/issues/863 - return readInt(T, @ptrCast(*const [n]u8, bytes.ptr), endian); + return readInt(T, bytes[0..n], endian); } test "comptime read/write int" { @@ -1586,24 +1584,24 @@ pub fn nativeToBig(comptime T: type, x: T) T { } fn AsBytesReturnType(comptime P: type) type { - if (comptime !trait.isSingleItemPtr(P)) + if (!trait.isSingleItemPtr(P)) @compileError("expected single item pointer, passed " ++ @typeName(P)); - const size = @as(usize, @sizeOf(meta.Child(P))); - const alignment = comptime meta.alignment(P); + const size = @sizeOf(meta.Child(P)); + const alignment = meta.alignment(P); if (alignment == 0) { - if (comptime trait.isConstPtr(P)) + if (trait.isConstPtr(P)) return *const [size]u8; return *[size]u8; } - if (comptime trait.isConstPtr(P)) + if (trait.isConstPtr(P)) return *align(alignment) const [size]u8; return *align(alignment) [size]u8; } -///Given a pointer to a single item, returns a slice of the underlying bytes, preserving constness. +/// Given a pointer to a single item, returns a slice of the underlying bytes, preserving constness. pub fn asBytes(ptr: var) AsBytesReturnType(@TypeOf(ptr)) { const P = @TypeOf(ptr); return @ptrCast(AsBytesReturnType(P), ptr); @@ -1750,34 +1748,50 @@ fn BytesAsSliceReturnType(comptime T: type, comptime bytesType: type) type { } pub fn bytesAsSlice(comptime T: type, bytes: var) BytesAsSliceReturnType(T, @TypeOf(bytes)) { - const bytesSlice = if (comptime trait.isPtrTo(.Array)(@TypeOf(bytes))) bytes[0..] else bytes; - // let's not give an undefined pointer to @ptrCast // it may be equal to zero and fail a null check - if (bytesSlice.len == 0) { + if (bytes.len == 0) { return &[0]T{}; } - const bytesType = @TypeOf(bytesSlice); - const alignment = comptime meta.alignment(bytesType); + const Bytes = @TypeOf(bytes); + const alignment = comptime meta.alignment(Bytes); - const castTarget = if (comptime trait.isConstPtr(bytesType)) [*]align(alignment) const T else [*]align(alignment) T; + const cast_target = if (comptime trait.isConstPtr(Bytes)) [*]align(alignment) const T else [*]align(alignment) T; - return @ptrCast(castTarget, bytesSlice.ptr)[0..@divExact(bytes.len, @sizeOf(T))]; + return @ptrCast(cast_target, bytes)[0..@divExact(bytes.len, @sizeOf(T))]; } test "bytesAsSlice" { - const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF }; - const slice = bytesAsSlice(u16, bytes[0..]); - testing.expect(slice.len == 2); - testing.expect(bigToNative(u16, slice[0]) == 0xDEAD); - testing.expect(bigToNative(u16, slice[1]) == 0xBEEF); + { + const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF }; + const slice = bytesAsSlice(u16, bytes[0..]); + testing.expect(slice.len == 2); + testing.expect(bigToNative(u16, slice[0]) == 0xDEAD); + testing.expect(bigToNative(u16, slice[1]) == 0xBEEF); + } + { + const bytes = [_]u8{ 0xDE, 0xAD, 0xBE, 0xEF }; + var runtime_zero: usize = 0; + const slice = bytesAsSlice(u16, bytes[runtime_zero..]); + testing.expect(slice.len == 2); + testing.expect(bigToNative(u16, slice[0]) == 0xDEAD); + testing.expect(bigToNative(u16, slice[1]) == 0xBEEF); + } } test "bytesAsSlice keeps pointer alignment" { - var bytes = [_]u8{ 0x01, 0x02, 0x03, 0x04 }; - const numbers = bytesAsSlice(u32, bytes[0..]); - comptime testing.expect(@TypeOf(numbers) == []align(@alignOf(@TypeOf(bytes))) u32); + { + var bytes = [_]u8{ 0x01, 0x02, 0x03, 0x04 }; + const numbers = bytesAsSlice(u32, bytes[0..]); + comptime testing.expect(@TypeOf(numbers) == []align(@alignOf(@TypeOf(bytes))) u32); + } + { + var bytes = [_]u8{ 0x01, 0x02, 0x03, 0x04 }; + var runtime_zero: usize = 0; + const numbers = bytesAsSlice(u32, bytes[runtime_zero..]); + comptime testing.expect(@TypeOf(numbers) == []align(@alignOf(@TypeOf(bytes))) u32); + } } test "bytesAsSlice on a packed struct" { @@ -1813,21 +1827,19 @@ fn SliceAsBytesReturnType(comptime sliceType: type) type { } pub fn sliceAsBytes(slice: var) SliceAsBytesReturnType(@TypeOf(slice)) { - const actualSlice = if (comptime trait.isPtrTo(.Array)(@TypeOf(slice))) slice[0..] else slice; - const actualSliceTypeInfo = @typeInfo(@TypeOf(actualSlice)).Pointer; + const Slice = @TypeOf(slice); // let's not give an undefined pointer to @ptrCast // it may be equal to zero and fail a null check - if (actualSlice.len == 0 and actualSliceTypeInfo.sentinel == null) { + if (slice.len == 0 and comptime meta.sentinel(Slice) == null) { return &[0]u8{}; } - const sliceType = @TypeOf(actualSlice); - const alignment = comptime meta.alignment(sliceType); + const alignment = comptime meta.alignment(Slice); - const castTarget = if (comptime trait.isConstPtr(sliceType)) [*]align(alignment) const u8 else [*]align(alignment) u8; + const cast_target = if (comptime trait.isConstPtr(Slice)) [*]align(alignment) const u8 else [*]align(alignment) u8; - return @ptrCast(castTarget, actualSlice.ptr)[0 .. actualSlice.len * @sizeOf(comptime meta.Child(sliceType))]; + return @ptrCast(cast_target, slice)[0 .. slice.len * @sizeOf(meta.Elem(Slice))]; } test "sliceAsBytes" { @@ -1897,39 +1909,6 @@ test "sliceAsBytes and bytesAsSlice back" { testing.expect(bytes[11] == math.maxInt(u8)); } -fn SubArrayPtrReturnType(comptime T: type, comptime length: usize) type { - if (trait.isConstPtr(T)) - return *const [length]meta.Child(meta.Child(T)); - return *[length]meta.Child(meta.Child(T)); -} - -/// Given a pointer to an array, returns a pointer to a portion of that array, preserving constness. -/// TODO this will be obsoleted by https://github.com/ziglang/zig/issues/863 -pub fn subArrayPtr( - ptr: var, - comptime start: usize, - comptime length: usize, -) SubArrayPtrReturnType(@TypeOf(ptr), length) { - assert(start + length <= ptr.*.len); - - const ReturnType = SubArrayPtrReturnType(@TypeOf(ptr), length); - const T = meta.Child(meta.Child(@TypeOf(ptr))); - return @ptrCast(ReturnType, &ptr[start]); -} - -test "subArrayPtr" { - const a1: [6]u8 = "abcdef".*; - const sub1 = subArrayPtr(&a1, 2, 3); - testing.expect(eql(u8, sub1, "cde")); - - var a2: [6]u8 = "abcdef".*; - var sub2 = subArrayPtr(&a2, 2, 3); - - testing.expect(eql(u8, sub2, "cde")); - sub2[1] = 'X'; - testing.expect(eql(u8, &a2, "abcXef")); -} - /// Round an address up to the nearest aligned address /// The alignment must be a power of 2 and greater than 0. pub fn alignForward(addr: usize, alignment: usize) usize { diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 65809abb5c..7343cfc51a 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -104,7 +104,7 @@ pub fn Child(comptime T: type) type { .Array => |info| info.child, .Pointer => |info| info.child, .Optional => |info| info.child, - else => @compileError("Expected pointer, optional, or array type, " ++ "found '" ++ @typeName(T) ++ "'"), + else => @compileError("Expected pointer, optional, or array type, found '" ++ @typeName(T) ++ "'"), }; } @@ -115,30 +115,65 @@ test "std.meta.Child" { testing.expect(Child(?u8) == u8); } -/// Given a type with a sentinel e.g. `[:0]u8`, returns the sentinel -pub fn Sentinel(comptime T: type) Child(T) { - // comptime asserts that ptr has a sentinel +/// Given a "memory span" type, returns the "element type". +pub fn Elem(comptime T: type) type { switch (@typeInfo(T)) { - .Array => |arrayInfo| { - return comptime arrayInfo.sentinel.?; + .Array => |info| return info.child, + .Pointer => |info| switch (info.size) { + .One => switch (@typeInfo(info.child)) { + .Array => |array_info| return array_info.child, + else => {}, + }, + .Many, .C, .Slice => return info.child, }, - .Pointer => |ptrInfo| { - switch (ptrInfo.size) { - .Many, .Slice => { - return comptime ptrInfo.sentinel.?; + else => {}, + } + @compileError("Expected pointer, slice, or array, found '" ++ @typeName(T) ++ "'"); +} + +test "std.meta.Elem" { + testing.expect(Elem([1]u8) == u8); + testing.expect(Elem([*]u8) == u8); + testing.expect(Elem([]u8) == u8); + testing.expect(Elem(*[10]u8) == u8); +} + +/// Given a type which can have a sentinel e.g. `[:0]u8`, returns the sentinel value, +/// or `null` if there is not one. +/// Types which cannot possibly have a sentinel will be a compile error. +pub fn sentinel(comptime T: type) ?Elem(T) { + switch (@typeInfo(T)) { + .Array => |info| return info.sentinel, + .Pointer => |info| { + switch (info.size) { + .Many, .Slice => return info.sentinel, + .One => switch (@typeInfo(info.child)) { + .Array => |array_info| return array_info.sentinel, + else => {}, }, else => {}, } }, else => {}, } - @compileError("not a sentinel type, found '" ++ @typeName(T) ++ "'"); + @compileError("type '" ++ @typeName(T) ++ "' cannot possibly have a sentinel"); } -test "std.meta.Sentinel" { - testing.expectEqual(@as(u8, 0), Sentinel([:0]u8)); - testing.expectEqual(@as(u8, 0), Sentinel([*:0]u8)); - testing.expectEqual(@as(u8, 0), Sentinel([5:0]u8)); +test "std.meta.sentinel" { + testSentinel(); + comptime testSentinel(); +} + +fn testSentinel() void { + testing.expectEqual(@as(u8, 0), sentinel([:0]u8).?); + testing.expectEqual(@as(u8, 0), sentinel([*:0]u8).?); + testing.expectEqual(@as(u8, 0), sentinel([5:0]u8).?); + testing.expectEqual(@as(u8, 0), sentinel(*const [5:0]u8).?); + + testing.expect(sentinel([]u8) == null); + testing.expect(sentinel([*]u8) == null); + testing.expect(sentinel([5]u8) == null); + testing.expect(sentinel(*const [5]u8) == null); } pub fn containerLayout(comptime T: type) TypeInfo.ContainerLayout { diff --git a/lib/std/meta/trait.zig b/lib/std/meta/trait.zig index c0fb8c5025..c99e1389f4 100644 --- a/lib/std/meta/trait.zig +++ b/lib/std/meta/trait.zig @@ -230,9 +230,10 @@ pub fn isSingleItemPtr(comptime T: type) bool { test "std.meta.trait.isSingleItemPtr" { const array = [_]u8{0} ** 10; - testing.expect(isSingleItemPtr(@TypeOf(&array[0]))); - testing.expect(!isSingleItemPtr(@TypeOf(array))); - testing.expect(!isSingleItemPtr(@TypeOf(array[0..1]))); + comptime testing.expect(isSingleItemPtr(@TypeOf(&array[0]))); + comptime testing.expect(!isSingleItemPtr(@TypeOf(array))); + var runtime_zero: usize = 0; + testing.expect(!isSingleItemPtr(@TypeOf(array[runtime_zero..1]))); } pub fn isManyItemPtr(comptime T: type) bool { @@ -259,7 +260,8 @@ pub fn isSlice(comptime T: type) bool { test "std.meta.trait.isSlice" { const array = [_]u8{0} ** 10; - testing.expect(isSlice(@TypeOf(array[0..]))); + var runtime_zero: usize = 0; + testing.expect(isSlice(@TypeOf(array[runtime_zero..]))); testing.expect(!isSlice(@TypeOf(array))); testing.expect(!isSlice(@TypeOf(&array[0]))); } @@ -276,7 +278,7 @@ pub fn isIndexable(comptime T: type) bool { test "std.meta.trait.isIndexable" { const array = [_]u8{0} ** 10; - const slice = array[0..]; + const slice = @as([]const u8, &array); testing.expect(isIndexable(@TypeOf(array))); testing.expect(isIndexable(@TypeOf(&array))); diff --git a/lib/std/net.zig b/lib/std/net.zig index c2328b9bd0..b9c1281191 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -612,8 +612,7 @@ fn linuxLookupName( } 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"); - // TODO https://github.com/ziglang/zig/issues/863 - mem.writeIntNative(u32, @ptrCast(*[4]u8, da6.addr[12..].ptr), addr.addr.in.addr); + mem.writeIntNative(u32, da6.addr[12..], addr.addr.in.addr); da4.addr = addr.addr.in.addr; da = @ptrCast(*os.sockaddr, &da4); dalen = @sizeOf(os.sockaddr_in); @@ -821,7 +820,7 @@ fn linuxLookupNameFromHosts( // Skip to the delimiter in the stream, to fix parsing try stream.skipUntilDelimiterOrEof('\n'); // Use the truncated line. A truncated comment or hostname will be handled correctly. - break :blk line_buf[0..]; + break :blk @as([]u8, &line_buf); // TODO the cast should not be necessary }, else => |e| return e, }) |line| { @@ -958,7 +957,10 @@ fn linuxLookupNameFromDns( } } - var ap = [2][]u8{ apbuf[0][0..0], apbuf[1][0..0] }; + var ap = [2][]u8{ apbuf[0], apbuf[1] }; + ap[0].len = 0; + ap[1].len = 0; + try resMSendRc(qp[0..nq], ap[0..nq], apbuf[0..nq], rc); var i: usize = 0; @@ -1015,7 +1017,7 @@ fn getResolvConf(allocator: *mem.Allocator, rc: *ResolvConf) !void { // Skip to the delimiter in the stream, to fix parsing try stream.skipUntilDelimiterOrEof('\n'); // Give an empty line to the while loop, which will be skipped. - break :blk line_buf[0..0]; + break :blk @as([]u8, line_buf[0..0]); // TODO the cast should not be necessary }, else => |e| return e, }) |line| { diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index f5acb70adb..813a77c275 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -1276,7 +1276,15 @@ pub fn unexpectedError(err: Win32Error) std.os.UnexpectedError { // 614 is the length of the longest windows error desciption var buf_u16: [614]u16 = undefined; var buf_u8: [614]u8 = undefined; - var len = kernel32.FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, null, err, MAKELANGID(LANG.NEUTRAL, SUBLANG.DEFAULT), buf_u16[0..].ptr, buf_u16.len / @sizeOf(TCHAR), null); + const len = kernel32.FormatMessageW( + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + null, + err, + MAKELANGID(LANG.NEUTRAL, SUBLANG.DEFAULT), + &buf_u16, + buf_u16.len / @sizeOf(TCHAR), + null, + ); _ = std.unicode.utf16leToUtf8(&buf_u8, buf_u16[0..len]) catch unreachable; std.debug.warn("error.Unexpected: GetLastError({}): {}\n", .{ @enumToInt(err), buf_u8[0..len] }); std.debug.dumpCurrentStackTrace(null); diff --git a/lib/std/rand.zig b/lib/std/rand.zig index 31891f5f0e..ede46e3065 100644 --- a/lib/std/rand.zig +++ b/lib/std/rand.zig @@ -5,7 +5,7 @@ // ``` // var buf: [8]u8 = undefined; // try std.crypto.randomBytes(buf[0..]); -// const seed = mem.readIntSliceLittle(u64, buf[0..8]); +// const seed = mem.readIntLittle(u64, buf[0..8]); // // var r = DefaultPrng.init(seed); // diff --git a/lib/std/unicode.zig b/lib/std/unicode.zig index 8ed51fa145..a971a730e8 100644 --- a/lib/std/unicode.zig +++ b/lib/std/unicode.zig @@ -251,12 +251,12 @@ pub const Utf16LeIterator = struct { pub fn nextCodepoint(it: *Utf16LeIterator) !?u21 { assert(it.i <= it.bytes.len); if (it.i == it.bytes.len) return null; - const c0: u21 = mem.readIntSliceLittle(u16, it.bytes[it.i .. it.i + 2]); + const c0: u21 = mem.readIntLittle(u16, it.bytes[it.i..][0..2]); if (c0 & ~@as(u21, 0x03ff) == 0xd800) { // surrogate pair it.i += 2; if (it.i >= it.bytes.len) return error.DanglingSurrogateHalf; - const c1: u21 = mem.readIntSliceLittle(u16, it.bytes[it.i .. it.i + 2]); + const c1: u21 = mem.readIntLittle(u16, it.bytes[it.i..][0..2]); if (c1 & ~@as(u21, 0x03ff) != 0xdc00) return error.ExpectedSecondSurrogateHalf; it.i += 2; return 0x10000 + (((c0 & 0x03ff) << 10) | (c1 & 0x03ff)); @@ -630,11 +630,11 @@ test "utf8ToUtf16LeWithNull" { } } -/// Converts a UTF-8 string literal into a UTF-16LE string literal. -pub fn utf8ToUtf16LeStringLiteral(comptime utf8: []const u8) *const [calcUtf16LeLen(utf8) :0] u16 { +/// Converts a UTF-8 string literal into a UTF-16LE string literal. +pub fn utf8ToUtf16LeStringLiteral(comptime utf8: []const u8) *const [calcUtf16LeLen(utf8):0]u16 { comptime { const len: usize = calcUtf16LeLen(utf8); - var utf16le: [len :0]u16 = [_ :0]u16{0} ** len; + var utf16le: [len:0]u16 = [_:0]u16{0} ** len; const utf16le_len = utf8ToUtf16Le(&utf16le, utf8[0..]) catch |err| @compileError(err); assert(len == utf16le_len); return &utf16le; @@ -660,8 +660,8 @@ fn calcUtf16LeLen(utf8: []const u8) usize { } test "utf8ToUtf16LeStringLiteral" { -{ - const bytes = [_:0]u16{ 0x41 }; + { + const bytes = [_:0]u16{0x41}; const utf16 = utf8ToUtf16LeStringLiteral("A"); testing.expectEqualSlices(u16, &bytes, utf16); testing.expect(utf16[1] == 0); @@ -673,19 +673,19 @@ test "utf8ToUtf16LeStringLiteral" { testing.expect(utf16[2] == 0); } { - const bytes = [_:0]u16{ 0x02FF }; + const bytes = [_:0]u16{0x02FF}; const utf16 = utf8ToUtf16LeStringLiteral("\u{02FF}"); testing.expectEqualSlices(u16, &bytes, utf16); testing.expect(utf16[1] == 0); } { - const bytes = [_:0]u16{ 0x7FF }; + const bytes = [_:0]u16{0x7FF}; const utf16 = utf8ToUtf16LeStringLiteral("\u{7FF}"); testing.expectEqualSlices(u16, &bytes, utf16); testing.expect(utf16[1] == 0); } { - const bytes = [_:0]u16{ 0x801 }; + const bytes = [_:0]u16{0x801}; const utf16 = utf8ToUtf16LeStringLiteral("\u{801}"); testing.expectEqualSlices(u16, &bytes, utf16); testing.expect(utf16[1] == 0); |
