Skip to content

Commit dc04e97

Browse files
authored
Merge pull request #4752 from ziglang/slice-array
slicing with comptime start and end indexes results in pointer-to-array
2 parents 555a2c0 + 160367e commit dc04e97

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+906
-431
lines changed

doc/langref.html.in

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,8 +2093,9 @@ var foo: u8 align(4) = 100;
20932093
test "global variable alignment" {
20942094
assert(@TypeOf(&foo).alignment == 4);
20952095
assert(@TypeOf(&foo) == *align(4) u8);
2096-
const slice = @as(*[1]u8, &foo)[0..];
2097-
assert(@TypeOf(slice) == []align(4) u8);
2096+
const as_pointer_to_array: *[1]u8 = &foo;
2097+
const as_slice: []u8 = as_pointer_to_array;
2098+
assert(@TypeOf(as_slice) == []align(4) u8);
20982099
}
20992100

21002101
fn derp() align(@sizeOf(usize) * 2) i32 { return 1234; }
@@ -2187,7 +2188,8 @@ test "basic slices" {
21872188
// a slice is that the array's length is part of the type and known at
21882189
// compile-time, whereas the slice's length is known at runtime.
21892190
// Both can be accessed with the `len` field.
2190-
const slice = array[0..array.len];
2191+
var known_at_runtime_zero: usize = 0;
2192+
const slice = array[known_at_runtime_zero..array.len];
21912193
assert(&slice[0] == &array[0]);
21922194
assert(slice.len == array.len);
21932195

@@ -2207,13 +2209,15 @@ test "basic slices" {
22072209
{#code_end#}
22082210
<p>This is one reason we prefer slices to pointers.</p>
22092211
{#code_begin|test|slices#}
2210-
const assert = @import("std").debug.assert;
2211-
const mem = @import("std").mem;
2212-
const fmt = @import("std").fmt;
2212+
const std = @import("std");
2213+
const assert = std.debug.assert;
2214+
const mem = std.mem;
2215+
const fmt = std.fmt;
22132216

22142217
test "using slices for strings" {
2215-
// Zig has no concept of strings. String literals are arrays of u8, and
2216-
// in general the string type is []u8 (slice of u8).
2218+
// Zig has no concept of strings. String literals are const pointers to
2219+
// arrays of u8, and by convention parameters that are "strings" are
2220+
// expected to be UTF-8 encoded slices of u8.
22172221
// Here we coerce [5]u8 to []const u8
22182222
const hello: []const u8 = "hello";
22192223
const world: []const u8 = "世界";
@@ -2222,7 +2226,7 @@ test "using slices for strings" {
22222226
// You can use slice syntax on an array to convert an array into a slice.
22232227
const all_together_slice = all_together[0..];
22242228
// String concatenation example.
2225-
const hello_world = try fmt.bufPrint(all_together_slice, "{} {}", .{hello, world});
2229+
const hello_world = try fmt.bufPrint(all_together_slice, "{} {}", .{ hello, world });
22262230

22272231
// Generally, you can use UTF-8 and not worry about whether something is a
22282232
// string. If you don't need to deal with individual characters, no need
@@ -2239,23 +2243,15 @@ test "slice pointer" {
22392243
slice[2] = 3;
22402244
assert(slice[2] == 3);
22412245
// The slice is mutable because we sliced a mutable pointer.
2242-
assert(@TypeOf(slice) == []u8);
2246+
// Furthermore, it is actually a pointer to an array, since the start
2247+
// and end indexes were both comptime-known.
2248+
assert(@TypeOf(slice) == *[5]u8);
22432249

22442250
// You can also slice a slice:
22452251
const slice2 = slice[2..3];
22462252
assert(slice2.len == 1);
22472253
assert(slice2[0] == 3);
22482254
}
2249-
2250-
test "slice widening" {
2251-
// Zig supports slice widening and slice narrowing. Cast a slice of u8
2252-
// to a slice of anything else, and Zig will perform the length conversion.
2253-
const array align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13 };
2254-
const slice = mem.bytesAsSlice(u32, array[0..]);
2255-
assert(slice.len == 2);
2256-
assert(slice[0] == 0x12121212);
2257-
assert(slice[1] == 0x13131313);
2258-
}
22592255
{#code_end#}
22602256
{#see_also|Pointers|for|Arrays#}
22612257

lib/std/crypto/aes.zig

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ fn rotw(w: u32) u32 {
1515

1616
// Encrypt one block from src into dst, using the expanded key xk.
1717
fn 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.
6868
pub 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

118118
fn 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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
// https://monocypher.org/
44

55
const std = @import("../std.zig");
6-
const builtin = @import("builtin");
6+
const builtin = std.builtin;
77

88
const Endian = builtin.Endian;
9-
const readIntSliceLittle = std.mem.readIntSliceLittle;
10-
const writeIntSliceLittle = std.mem.writeIntSliceLittle;
9+
const readIntLittle = std.mem.readIntLittle;
10+
const writeIntLittle = std.mem.writeIntLittle;
1111

1212
pub 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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)