authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-19 14:48:47-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-19 14:48:47-04:00
logf614d94faa3b2a259c3a82cd66f167029f20d224
tree1ecaaeda6c54d626ff3898d109c99d30152483fd
parent1d7861a36e1bcd8f7bfdb53716ef53467704922b
signaturelock-open Commit is signed but in an unrecognized format.

update std lib to take advantage of slicing with comptime indexes


13 files changed, 108 insertions(+), 119 deletions(-)

lib/std/crypto/aes.zig+19-19
......@@ -15,10 +15,10 @@ fn rotw(w: u32) u32 {
1515
1616// Encrypt one block from src into dst, using the expanded key xk.
1717fn encryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {
18 var s0 = mem.readIntSliceBig(u32, src[0..4]);
19 var s1 = mem.readIntSliceBig(u32, src[4..8]);
20 var s2 = mem.readIntSliceBig(u32, src[8..12]);
21 var s3 = mem.readIntSliceBig(u32, src[12..16]);
18 var s0 = mem.readIntBig(u32, src[0..4]);
19 var s1 = mem.readIntBig(u32, src[4..8]);
20 var s2 = mem.readIntBig(u32, src[8..12]);
21 var s3 = mem.readIntBig(u32, src[12..16]);
2222
2323 // First round just XORs input with key.
2424 s0 ^= xk[0];
......@@ -58,18 +58,18 @@ fn encryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {
5858 s2 ^= xk[k + 2];
5959 s3 ^= xk[k + 3];
6060
61 mem.writeIntSliceBig(u32, dst[0..4], s0);
62 mem.writeIntSliceBig(u32, dst[4..8], s1);
63 mem.writeIntSliceBig(u32, dst[8..12], s2);
64 mem.writeIntSliceBig(u32, dst[12..16], s3);
61 mem.writeIntBig(u32, dst[0..4], s0);
62 mem.writeIntBig(u32, dst[4..8], s1);
63 mem.writeIntBig(u32, dst[8..12], s2);
64 mem.writeIntBig(u32, dst[12..16], s3);
6565}
6666
6767// Decrypt one block from src into dst, using the expanded key xk.
6868pub fn decryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {
69 var s0 = mem.readIntSliceBig(u32, src[0..4]);
70 var s1 = mem.readIntSliceBig(u32, src[4..8]);
71 var s2 = mem.readIntSliceBig(u32, src[8..12]);
72 var s3 = mem.readIntSliceBig(u32, src[12..16]);
69 var s0 = mem.readIntBig(u32, src[0..4]);
70 var s1 = mem.readIntBig(u32, src[4..8]);
71 var s2 = mem.readIntBig(u32, src[8..12]);
72 var s3 = mem.readIntBig(u32, src[12..16]);
7373
7474 // First round just XORs input with key.
7575 s0 ^= xk[0];
......@@ -109,10 +109,10 @@ pub fn decryptBlock(xk: []const u32, dst: []u8, src: []const u8) void {
109109 s2 ^= xk[k + 2];
110110 s3 ^= xk[k + 3];
111111
112 mem.writeIntSliceBig(u32, dst[0..4], s0);
113 mem.writeIntSliceBig(u32, dst[4..8], s1);
114 mem.writeIntSliceBig(u32, dst[8..12], s2);
115 mem.writeIntSliceBig(u32, dst[12..16], s3);
112 mem.writeIntBig(u32, dst[0..4], s0);
113 mem.writeIntBig(u32, dst[4..8], s1);
114 mem.writeIntBig(u32, dst[8..12], s2);
115 mem.writeIntBig(u32, dst[12..16], s3);
116116}
117117
118118fn xorBytes(dst: []u8, a: []const u8, b: []const u8) usize {
......@@ -154,8 +154,8 @@ fn AES(comptime keysize: usize) type {
154154 var n: usize = 0;
155155 while (n < src.len) {
156156 ctx.encrypt(keystream[0..], ctrbuf[0..]);
157 var ctr_i = std.mem.readIntSliceBig(u128, ctrbuf[0..]);
158 std.mem.writeIntSliceBig(u128, ctrbuf[0..], ctr_i +% 1);
157 var ctr_i = std.mem.readIntBig(u128, ctrbuf[0..]);
158 std.mem.writeIntBig(u128, ctrbuf[0..], ctr_i +% 1);
159159
160160 n += xorBytes(dst[n..], src[n..], &keystream);
161161 }
......@@ -251,7 +251,7 @@ fn expandKey(key: []const u8, enc: []u32, dec: []u32) void {
251251 var i: usize = 0;
252252 var nk = key.len / 4;
253253 while (i < nk) : (i += 1) {
254 enc[i] = mem.readIntSliceBig(u32, key[4 * i .. 4 * i + 4]);
254 enc[i] = mem.readIntBig(u32, key[4 * i ..][0..4]);
255255 }
256256 while (i < enc.len) : (i += 1) {
257257 var t = enc[i - 1];
lib/std/crypto/blake2.zig+4-7
......@@ -123,8 +123,7 @@ fn Blake2s(comptime out_len: usize) type {
123123 const rr = d.h[0 .. out_len / 32];
124124
125125 for (rr) |s, j| {
126 // TODO https://github.com/ziglang/zig/issues/863
127 mem.writeIntSliceLittle(u32, out[4 * j .. 4 * j + 4], s);
126 mem.writeIntLittle(u32, out[4 * j ..][0..4], s);
128127 }
129128 }
130129
......@@ -135,8 +134,7 @@ fn Blake2s(comptime out_len: usize) type {
135134 var v: [16]u32 = undefined;
136135
137136 for (m) |*r, i| {
138 // TODO https://github.com/ziglang/zig/issues/863
139 r.* = mem.readIntSliceLittle(u32, b[4 * i .. 4 * i + 4]);
137 r.* = mem.readIntLittle(u32, b[4 * i ..][0..4]);
140138 }
141139
142140 var k: usize = 0;
......@@ -358,8 +356,7 @@ fn Blake2b(comptime out_len: usize) type {
358356 const rr = d.h[0 .. out_len / 64];
359357
360358 for (rr) |s, j| {
361 // TODO https://github.com/ziglang/zig/issues/863
362 mem.writeIntSliceLittle(u64, out[8 * j .. 8 * j + 8], s);
359 mem.writeIntLittle(u64, out[8 * j ..][0..8], s);
363360 }
364361 }
365362
......@@ -370,7 +367,7 @@ fn Blake2b(comptime out_len: usize) type {
370367 var v: [16]u64 = undefined;
371368
372369 for (m) |*r, i| {
373 r.* = mem.readIntSliceLittle(u64, b[8 * i .. 8 * i + 8]);
370 r.* = mem.readIntLittle(u64, b[8 * i ..][0..8]);
374371 }
375372
376373 var k: usize = 0;
lib/std/crypto/chacha20.zig+30-31
......@@ -61,8 +61,7 @@ fn salsa20_wordtobyte(out: []u8, input: [16]u32) void {
6161 }
6262
6363 for (x) |_, i| {
64 // TODO https://github.com/ziglang/zig/issues/863
65 mem.writeIntSliceLittle(u32, out[4 * i .. 4 * i + 4], x[i] +% input[i]);
64 mem.writeIntLittle(u32, out[4 * i ..][0..4], x[i] +% input[i]);
6665 }
6766}
6867
......@@ -73,10 +72,10 @@ fn chaCha20_internal(out: []u8, in: []const u8, key: [8]u32, counter: [4]u32) vo
7372
7473 const c = "expand 32-byte k";
7574 const constant_le = [_]u32{
76 mem.readIntSliceLittle(u32, c[0..4]),
77 mem.readIntSliceLittle(u32, c[4..8]),
78 mem.readIntSliceLittle(u32, c[8..12]),
79 mem.readIntSliceLittle(u32, c[12..16]),
75 mem.readIntLittle(u32, c[0..4]),
76 mem.readIntLittle(u32, c[4..8]),
77 mem.readIntLittle(u32, c[8..12]),
78 mem.readIntLittle(u32, c[12..16]),
8079 };
8180
8281 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:
120119 var k: [8]u32 = undefined;
121120 var c: [4]u32 = undefined;
122121
123 k[0] = mem.readIntSliceLittle(u32, key[0..4]);
124 k[1] = mem.readIntSliceLittle(u32, key[4..8]);
125 k[2] = mem.readIntSliceLittle(u32, key[8..12]);
126 k[3] = mem.readIntSliceLittle(u32, key[12..16]);
127 k[4] = mem.readIntSliceLittle(u32, key[16..20]);
128 k[5] = mem.readIntSliceLittle(u32, key[20..24]);
129 k[6] = mem.readIntSliceLittle(u32, key[24..28]);
130 k[7] = mem.readIntSliceLittle(u32, key[28..32]);
122 k[0] = mem.readIntLittle(u32, key[0..4]);
123 k[1] = mem.readIntLittle(u32, key[4..8]);
124 k[2] = mem.readIntLittle(u32, key[8..12]);
125 k[3] = mem.readIntLittle(u32, key[12..16]);
126 k[4] = mem.readIntLittle(u32, key[16..20]);
127 k[5] = mem.readIntLittle(u32, key[20..24]);
128 k[6] = mem.readIntLittle(u32, key[24..28]);
129 k[7] = mem.readIntLittle(u32, key[28..32]);
131130
132131 c[0] = counter;
133 c[1] = mem.readIntSliceLittle(u32, nonce[0..4]);
134 c[2] = mem.readIntSliceLittle(u32, nonce[4..8]);
135 c[3] = mem.readIntSliceLittle(u32, nonce[8..12]);
132 c[1] = mem.readIntLittle(u32, nonce[0..4]);
133 c[2] = mem.readIntLittle(u32, nonce[4..8]);
134 c[3] = mem.readIntLittle(u32, nonce[8..12]);
136135 chaCha20_internal(out, in, k, c);
137136}
138137
......@@ -147,19 +146,19 @@ pub fn chaCha20With64BitNonce(out: []u8, in: []const u8, counter: u64, key: [32]
147146 var k: [8]u32 = undefined;
148147 var c: [4]u32 = undefined;
149148
150 k[0] = mem.readIntSliceLittle(u32, key[0..4]);
151 k[1] = mem.readIntSliceLittle(u32, key[4..8]);
152 k[2] = mem.readIntSliceLittle(u32, key[8..12]);
153 k[3] = mem.readIntSliceLittle(u32, key[12..16]);
154 k[4] = mem.readIntSliceLittle(u32, key[16..20]);
155 k[5] = mem.readIntSliceLittle(u32, key[20..24]);
156 k[6] = mem.readIntSliceLittle(u32, key[24..28]);
157 k[7] = mem.readIntSliceLittle(u32, key[28..32]);
149 k[0] = mem.readIntLittle(u32, key[0..4]);
150 k[1] = mem.readIntLittle(u32, key[4..8]);
151 k[2] = mem.readIntLittle(u32, key[8..12]);
152 k[3] = mem.readIntLittle(u32, key[12..16]);
153 k[4] = mem.readIntLittle(u32, key[16..20]);
154 k[5] = mem.readIntLittle(u32, key[20..24]);
155 k[6] = mem.readIntLittle(u32, key[24..28]);
156 k[7] = mem.readIntLittle(u32, key[28..32]);
158157
159158 c[0] = @truncate(u32, counter);
160159 c[1] = @truncate(u32, counter >> 32);
161 c[2] = mem.readIntSliceLittle(u32, nonce[0..4]);
162 c[3] = mem.readIntSliceLittle(u32, nonce[4..8]);
160 c[2] = mem.readIntLittle(u32, nonce[0..4]);
161 c[3] = mem.readIntLittle(u32, nonce[4..8]);
163162
164163 const block_size = (1 << 6);
165164 // 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,
463462 mac.update(zeros[0..padding]);
464463 }
465464 var lens: [16]u8 = undefined;
466 mem.writeIntSliceLittle(u64, lens[0..8], data.len);
467 mem.writeIntSliceLittle(u64, lens[8..16], plaintext.len);
465 mem.writeIntLittle(u64, lens[0..8], data.len);
466 mem.writeIntLittle(u64, lens[8..16], plaintext.len);
468467 mac.update(lens[0..]);
469468 mac.final(dst[plaintext.len..]);
470469}
......@@ -500,8 +499,8 @@ pub fn chacha20poly1305Open(dst: []u8, msgAndTag: []const u8, data: []const u8,
500499 mac.update(zeros[0..padding]);
501500 }
502501 var lens: [16]u8 = undefined;
503 mem.writeIntSliceLittle(u64, lens[0..8], data.len);
504 mem.writeIntSliceLittle(u64, lens[8..16], ciphertext.len);
502 mem.writeIntLittle(u64, lens[0..8], data.len);
503 mem.writeIntLittle(u64, lens[8..16], ciphertext.len);
505504 mac.update(lens[0..]);
506505 var computedTag: [16]u8 = undefined;
507506 mac.final(computedTag[0..]);
lib/std/crypto/md5.zig+1-2
......@@ -112,8 +112,7 @@ pub const Md5 = struct {
112112 d.round(d.buf[0..]);
113113
114114 for (d.s) |s, j| {
115 // TODO https://github.com/ziglang/zig/issues/863
116 mem.writeIntSliceLittle(u32, out[4 * j .. 4 * j + 4], s);
115 mem.writeIntLittle(u32, out[4 * j ..][0..4], s);
117116 }
118117 }
119118
lib/std/crypto/poly1305.zig+14-15
......@@ -3,11 +3,11 @@
33// https://monocypher.org/
44
55const std = @import("../std.zig");
6const builtin = @import("builtin");
6const builtin = std.builtin;
77
88const Endian = builtin.Endian;
9const readIntSliceLittle = std.mem.readIntSliceLittle;
10const writeIntSliceLittle = std.mem.writeIntSliceLittle;
9const readIntLittle = std.mem.readIntLittle;
10const writeIntLittle = std.mem.writeIntLittle;
1111
1212pub const Poly1305 = struct {
1313 const Self = @This();
......@@ -59,19 +59,19 @@ pub const Poly1305 = struct {
5959 {
6060 var i: usize = 0;
6161 while (i < 1) : (i += 1) {
62 ctx.r[0] = readIntSliceLittle(u32, key[0..4]) & 0x0fffffff;
62 ctx.r[0] = readIntLittle(u32, key[0..4]) & 0x0fffffff;
6363 }
6464 }
6565 {
6666 var i: usize = 1;
6767 while (i < 4) : (i += 1) {
68 ctx.r[i] = readIntSliceLittle(u32, key[i * 4 .. i * 4 + 4]) & 0x0ffffffc;
68 ctx.r[i] = readIntLittle(u32, key[i * 4 ..][0..4]) & 0x0ffffffc;
6969 }
7070 }
7171 {
7272 var i: usize = 0;
7373 while (i < 4) : (i += 1) {
74 ctx.pad[i] = readIntSliceLittle(u32, key[i * 4 + 16 .. i * 4 + 16 + 4]);
74 ctx.pad[i] = readIntLittle(u32, key[i * 4 + 16 ..][0..4]);
7575 }
7676 }
7777
......@@ -168,10 +168,10 @@ pub const Poly1305 = struct {
168168 const nb_blocks = nmsg.len >> 4;
169169 var i: usize = 0;
170170 while (i < nb_blocks) : (i += 1) {
171 ctx.c[0] = readIntSliceLittle(u32, nmsg[0..4]);
172 ctx.c[1] = readIntSliceLittle(u32, nmsg[4..8]);
173 ctx.c[2] = readIntSliceLittle(u32, nmsg[8..12]);
174 ctx.c[3] = readIntSliceLittle(u32, nmsg[12..16]);
171 ctx.c[0] = readIntLittle(u32, nmsg[0..4]);
172 ctx.c[1] = readIntLittle(u32, nmsg[4..8]);
173 ctx.c[2] = readIntLittle(u32, nmsg[8..12]);
174 ctx.c[3] = readIntLittle(u32, nmsg[12..16]);
175175 polyBlock(ctx);
176176 nmsg = nmsg[16..];
177177 }
......@@ -210,11 +210,10 @@ pub const Poly1305 = struct {
210210 const uu2 = (uu1 >> 32) + ctx.h[2] + ctx.pad[2]; // <= 2_00000000
211211 const uu3 = (uu2 >> 32) + ctx.h[3] + ctx.pad[3]; // <= 2_00000000
212212
213 // TODO https://github.com/ziglang/zig/issues/863
214 writeIntSliceLittle(u32, out[0..], @truncate(u32, uu0));
215 writeIntSliceLittle(u32, out[4..], @truncate(u32, uu1));
216 writeIntSliceLittle(u32, out[8..], @truncate(u32, uu2));
217 writeIntSliceLittle(u32, out[12..], @truncate(u32, uu3));
213 writeIntLittle(u32, out[0..4], @truncate(u32, uu0));
214 writeIntLittle(u32, out[4..8], @truncate(u32, uu1));
215 writeIntLittle(u32, out[8..12], @truncate(u32, uu2));
216 writeIntLittle(u32, out[12..16], @truncate(u32, uu3));
218217
219218 ctx.secureZero();
220219 }
lib/std/crypto/sha1.zig+1-2
......@@ -109,8 +109,7 @@ pub const Sha1 = struct {
109109 d.round(d.buf[0..]);
110110
111111 for (d.s) |s, j| {
112 // TODO https://github.com/ziglang/zig/issues/863
113 mem.writeIntSliceBig(u32, out[4 * j .. 4 * j + 4], s);
112 mem.writeIntBig(u32, out[4 * j ..][0..4], s);
114113 }
115114 }
116115
lib/std/crypto/sha3.zig+2-3
......@@ -120,7 +120,7 @@ fn keccak_f(comptime F: usize, d: []u8) void {
120120 var c = [_]u64{0} ** 5;
121121
122122 for (s) |*r, i| {
123 r.* = mem.readIntSliceLittle(u64, d[8 * i .. 8 * i + 8]);
123 r.* = mem.readIntLittle(u64, d[8 * i ..][0..8]);
124124 }
125125
126126 comptime var x: usize = 0;
......@@ -167,8 +167,7 @@ fn keccak_f(comptime F: usize, d: []u8) void {
167167 }
168168
169169 for (s) |r, i| {
170 // TODO https://github.com/ziglang/zig/issues/863
171 mem.writeIntSliceLittle(u64, d[8 * i .. 8 * i + 8], r);
170 mem.writeIntLittle(u64, d[8 * i ..][0..8], r);
172171 }
173172}
174173
lib/std/crypto/x25519.zig+20-21
......@@ -7,8 +7,8 @@ const builtin = @import("builtin");
77const fmt = std.fmt;
88
99const Endian = builtin.Endian;
10const readIntSliceLittle = std.mem.readIntSliceLittle;
11const writeIntSliceLittle = std.mem.writeIntSliceLittle;
10const readIntLittle = std.mem.readIntLittle;
11const writeIntLittle = std.mem.writeIntLittle;
1212
1313// Based on Supercop's ref10 implementation.
1414pub const X25519 = struct {
......@@ -255,16 +255,16 @@ const Fe = struct {
255255
256256 var t: [10]i64 = undefined;
257257
258 t[0] = readIntSliceLittle(u32, s[0..4]);
259 t[1] = @as(u32, readIntSliceLittle(u24, s[4..7])) << 6;
260 t[2] = @as(u32, readIntSliceLittle(u24, s[7..10])) << 5;
261 t[3] = @as(u32, readIntSliceLittle(u24, s[10..13])) << 3;
262 t[4] = @as(u32, readIntSliceLittle(u24, s[13..16])) << 2;
263 t[5] = readIntSliceLittle(u32, s[16..20]);
264 t[6] = @as(u32, readIntSliceLittle(u24, s[20..23])) << 7;
265 t[7] = @as(u32, readIntSliceLittle(u24, s[23..26])) << 5;
266 t[8] = @as(u32, readIntSliceLittle(u24, s[26..29])) << 4;
267 t[9] = (@as(u32, readIntSliceLittle(u24, s[29..32])) & 0x7fffff) << 2;
258 t[0] = readIntLittle(u32, s[0..4]);
259 t[1] = @as(u32, readIntLittle(u24, s[4..7])) << 6;
260 t[2] = @as(u32, readIntLittle(u24, s[7..10])) << 5;
261 t[3] = @as(u32, readIntLittle(u24, s[10..13])) << 3;
262 t[4] = @as(u32, readIntLittle(u24, s[13..16])) << 2;
263 t[5] = readIntLittle(u32, s[16..20]);
264 t[6] = @as(u32, readIntLittle(u24, s[20..23])) << 7;
265 t[7] = @as(u32, readIntLittle(u24, s[23..26])) << 5;
266 t[8] = @as(u32, readIntLittle(u24, s[26..29])) << 4;
267 t[9] = (@as(u32, readIntLittle(u24, s[29..32])) & 0x7fffff) << 2;
268268
269269 carry1(h, t[0..]);
270270 }
......@@ -544,15 +544,14 @@ const Fe = struct {
544544 ut[i] = @bitCast(u32, @intCast(i32, t[i]));
545545 }
546546
547 // TODO https://github.com/ziglang/zig/issues/863
548 writeIntSliceLittle(u32, s[0..4], (ut[0] >> 0) | (ut[1] << 26));
549 writeIntSliceLittle(u32, s[4..8], (ut[1] >> 6) | (ut[2] << 19));
550 writeIntSliceLittle(u32, s[8..12], (ut[2] >> 13) | (ut[3] << 13));
551 writeIntSliceLittle(u32, s[12..16], (ut[3] >> 19) | (ut[4] << 6));
552 writeIntSliceLittle(u32, s[16..20], (ut[5] >> 0) | (ut[6] << 25));
553 writeIntSliceLittle(u32, s[20..24], (ut[6] >> 7) | (ut[7] << 19));
554 writeIntSliceLittle(u32, s[24..28], (ut[7] >> 13) | (ut[8] << 12));
555 writeIntSliceLittle(u32, s[28..], (ut[8] >> 20) | (ut[9] << 6));
547 writeIntLittle(u32, s[0..4], (ut[0] >> 0) | (ut[1] << 26));
548 writeIntLittle(u32, s[4..8], (ut[1] >> 6) | (ut[2] << 19));
549 writeIntLittle(u32, s[8..12], (ut[2] >> 13) | (ut[3] << 13));
550 writeIntLittle(u32, s[12..16], (ut[3] >> 19) | (ut[4] << 6));
551 writeIntLittle(u32, s[16..20], (ut[5] >> 0) | (ut[6] << 25));
552 writeIntLittle(u32, s[20..24], (ut[6] >> 7) | (ut[7] << 19));
553 writeIntLittle(u32, s[24..28], (ut[7] >> 13) | (ut[8] << 12));
554 writeIntLittle(u32, s[28..32], (ut[8] >> 20) | (ut[9] << 6));
556555
557556 std.mem.secureZero(i64, t[0..]);
558557 }
lib/std/hash/siphash.zig+3-3
......@@ -39,8 +39,8 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
3939 pub fn init(key: []const u8) Self {
4040 assert(key.len >= 16);
4141
42 const k0 = mem.readIntSliceLittle(u64, key[0..8]);
43 const k1 = mem.readIntSliceLittle(u64, key[8..16]);
42 const k0 = mem.readIntLittle(u64, key[0..8]);
43 const k1 = mem.readIntLittle(u64, key[8..16]);
4444
4545 var d = Self{
4646 .v0 = k0 ^ 0x736f6d6570736575,
......@@ -111,7 +111,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
111111 fn round(self: *Self, b: []const u8) void {
112112 assert(b.len == 8);
113113
114 const m = mem.readIntSliceLittle(u64, b[0..]);
114 const m = mem.readIntLittle(u64, b[0..8]);
115115 self.v3 ^= m;
116116
117117 // TODO this is a workaround, should be able to supply the value without a separate variable
lib/std/hash/wyhash.zig+1-1
......@@ -11,7 +11,7 @@ const primes = [_]u64{
1111
1212fn read_bytes(comptime bytes: u8, data: []const u8) u64 {
1313 const T = std.meta.IntType(false, 8 * bytes);
14 return mem.readIntSliceLittle(T, data[0..bytes]);
14 return mem.readIntLittle(T, data[0..bytes]);
1515}
1616
1717fn read_8bytes_swapped(data: []const u8) u64 {
lib/std/mem.zig+2-4
......@@ -824,8 +824,7 @@ pub const readIntBig = switch (builtin.endian) {
824824pub fn readIntSliceNative(comptime T: type, bytes: []const u8) T {
825825 const n = @divExact(T.bit_count, 8);
826826 assert(bytes.len >= n);
827 // TODO https://github.com/ziglang/zig/issues/863
828 return readIntNative(T, @ptrCast(*const [n]u8, bytes.ptr));
827 return readIntNative(T, bytes[0..n]);
829828}
830829
831830/// 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
863862pub fn readIntSlice(comptime T: type, bytes: []const u8, endian: builtin.Endian) T {
864863 const n = @divExact(T.bit_count, 8);
865864 assert(bytes.len >= n);
866 // TODO https://github.com/ziglang/zig/issues/863
867 return readInt(T, @ptrCast(*const [n]u8, bytes.ptr), endian);
865 return readInt(T, bytes[0..n], endian);
868866}
869867
870868test "comptime read/write int" {
lib/std/rand.zig+1-1
......@@ -5,7 +5,7 @@
55// ```
66// var buf: [8]u8 = undefined;
77// try std.crypto.randomBytes(buf[0..]);
8// const seed = mem.readIntSliceLittle(u64, buf[0..8]);
8// const seed = mem.readIntLittle(u64, buf[0..8]);
99//
1010// var r = DefaultPrng.init(seed);
1111//
lib/std/unicode.zig+10-10
......@@ -251,12 +251,12 @@ pub const Utf16LeIterator = struct {
251251 pub fn nextCodepoint(it: *Utf16LeIterator) !?u21 {
252252 assert(it.i <= it.bytes.len);
253253 if (it.i == it.bytes.len) return null;
254 const c0: u21 = mem.readIntSliceLittle(u16, it.bytes[it.i .. it.i + 2]);
254 const c0: u21 = mem.readIntLittle(u16, it.bytes[it.i..][0..2]);
255255 if (c0 & ~@as(u21, 0x03ff) == 0xd800) {
256256 // surrogate pair
257257 it.i += 2;
258258 if (it.i >= it.bytes.len) return error.DanglingSurrogateHalf;
259 const c1: u21 = mem.readIntSliceLittle(u16, it.bytes[it.i .. it.i + 2]);
259 const c1: u21 = mem.readIntLittle(u16, it.bytes[it.i..][0..2]);
260260 if (c1 & ~@as(u21, 0x03ff) != 0xdc00) return error.ExpectedSecondSurrogateHalf;
261261 it.i += 2;
262262 return 0x10000 + (((c0 & 0x03ff) << 10) | (c1 & 0x03ff));
......@@ -630,11 +630,11 @@ test "utf8ToUtf16LeWithNull" {
630630 }
631631}
632632
633/// Converts a UTF-8 string literal into a UTF-16LE string literal.
634pub fn utf8ToUtf16LeStringLiteral(comptime utf8: []const u8) *const [calcUtf16LeLen(utf8) :0] u16 {
633/// Converts a UTF-8 string literal into a UTF-16LE string literal.
634pub fn utf8ToUtf16LeStringLiteral(comptime utf8: []const u8) *const [calcUtf16LeLen(utf8):0]u16 {
635635 comptime {
636636 const len: usize = calcUtf16LeLen(utf8);
637 var utf16le: [len :0]u16 = [_ :0]u16{0} ** len;
637 var utf16le: [len:0]u16 = [_:0]u16{0} ** len;
638638 const utf16le_len = utf8ToUtf16Le(&utf16le, utf8[0..]) catch |err| @compileError(err);
639639 assert(len == utf16le_len);
640640 return &utf16le;
......@@ -660,8 +660,8 @@ fn calcUtf16LeLen(utf8: []const u8) usize {
660660}
661661
662662test "utf8ToUtf16LeStringLiteral" {
663{
664 const bytes = [_:0]u16{ 0x41 };
663 {
664 const bytes = [_:0]u16{0x41};
665665 const utf16 = utf8ToUtf16LeStringLiteral("A");
666666 testing.expectEqualSlices(u16, &bytes, utf16);
667667 testing.expect(utf16[1] == 0);
......@@ -673,19 +673,19 @@ test "utf8ToUtf16LeStringLiteral" {
673673 testing.expect(utf16[2] == 0);
674674 }
675675 {
676 const bytes = [_:0]u16{ 0x02FF };
676 const bytes = [_:0]u16{0x02FF};
677677 const utf16 = utf8ToUtf16LeStringLiteral("\u{02FF}");
678678 testing.expectEqualSlices(u16, &bytes, utf16);
679679 testing.expect(utf16[1] == 0);
680680 }
681681 {
682 const bytes = [_:0]u16{ 0x7FF };
682 const bytes = [_:0]u16{0x7FF};
683683 const utf16 = utf8ToUtf16LeStringLiteral("\u{7FF}");
684684 testing.expectEqualSlices(u16, &bytes, utf16);
685685 testing.expect(utf16[1] == 0);
686686 }
687687 {
688 const bytes = [_:0]u16{ 0x801 };
688 const bytes = [_:0]u16{0x801};
689689 const utf16 = utf8ToUtf16LeStringLiteral("\u{801}");
690690 testing.expectEqualSlices(u16, &bytes, utf16);
691691 testing.expect(utf16[1] == 0);