authorgravatar for frmdstryr@protonmail.comfrmdstryr <frmdstryr@protonmail.com> 2020-11-16 09:38:32-05:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-12-24 01:27:21+02:00
log577b57784ae70a85f5f4fef37e749687d0e9b6b0
tree469a989c36cec6e6baf5663dc52f55581102f469
parent71548824237174aa5310c339c3161eae1623cf73

Return encoded slice from base64 encode


4 files changed, 10 insertions(+), 11 deletions(-)

lib/std/base64.zig+4-4
......@@ -38,8 +38,8 @@ pub const Base64Encoder = struct {
3838 }
3939
4040 /// dest.len must be what you get from ::calcSize.
41 pub fn encode(encoder: *const Base64Encoder, dest: []u8, source: []const u8) void {
42 assert(dest.len == Base64Encoder.calcSize(source.len));
41 pub fn encode(encoder: *const Base64Encoder, dest: []u8, source: []const u8) []const u8 {
42 assert(dest.len >= Base64Encoder.calcSize(source.len));
4343
4444 var i: usize = 0;
4545 var out_index: usize = 0;
......@@ -78,6 +78,7 @@ pub const Base64Encoder = struct {
7878 dest[out_index] = encoder.pad_char;
7979 out_index += 1;
8080 }
81 return dest[0..out_index];
8182 }
8283};
8384
......@@ -398,8 +399,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void
398399 // Base64Encoder
399400 {
400401 var buffer: [0x100]u8 = undefined;
401 var encoded = buffer[0..Base64Encoder.calcSize(expected_decoded.len)];
402 standard_encoder.encode(encoded, expected_decoded);
402 const encoded = standard_encoder.encode(&buffer, expected_decoded);
403403 testing.expectEqualSlices(u8, expected_encoded, encoded);
404404 }
405405
lib/std/build/write_file.zig+1-1
......@@ -72,7 +72,7 @@ pub const WriteFileStep = struct {
7272 var digest: [48]u8 = undefined;
7373 hash.final(&digest);
7474 var hash_basename: [64]u8 = undefined;
75 fs.base64_encoder.encode(&hash_basename, &digest);
75 _ = fs.base64_encoder.encode(&hash_basename, &digest);
7676 self.output_dir = try fs.path.join(self.builder.allocator, &[_][]const u8{
7777 self.builder.cache_root,
7878 "o",
lib/std/fs.zig+4-5
......@@ -83,7 +83,7 @@ pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path:
8383 tmp_path[dirname.len] = path.sep;
8484 while (true) {
8585 crypto.random.bytes(rand_buf[0..]);
86 base64_encoder.encode(tmp_path[dirname.len + 1 ..], &rand_buf);
86 _ = base64_encoder.encode(tmp_path[dirname.len + 1 ..], &rand_buf);
8787
8888 if (cwd().symLink(existing_path, tmp_path, .{})) {
8989 return cwd().rename(tmp_path, new_path);
......@@ -153,15 +153,14 @@ pub const AtomicFile = struct {
153153 ) InitError!AtomicFile {
154154 var rand_buf: [RANDOM_BYTES]u8 = undefined;
155155 var tmp_path_buf: [TMP_PATH_LEN:0]u8 = undefined;
156 // TODO: should be able to use TMP_PATH_LEN here.
157 tmp_path_buf[base64.Base64Encoder.calcSize(RANDOM_BYTES)] = 0;
158156
159157 while (true) {
160158 crypto.random.bytes(rand_buf[0..]);
161 base64_encoder.encode(&tmp_path_buf, &rand_buf);
159 const tmp_path = base64_encoder.encode(&tmp_path_buf, &rand_buf);
160 tmp_path_buf[tmp_path.len] = 0;
162161
163162 const file = dir.createFile(
164 &tmp_path_buf,
163 tmp_path,
165164 .{ .mode = mode, .exclusive = true },
166165 ) catch |err| switch (err) {
167166 error.PathAlreadyExists => continue,
lib/std/testing.zig+1-1
......@@ -305,7 +305,7 @@ pub fn tmpDir(opts: std.fs.Dir.OpenDirOptions) TmpDir {
305305 var random_bytes: [TmpDir.random_bytes_count]u8 = undefined;
306306 std.crypto.random.bytes(&random_bytes);
307307 var sub_path: [TmpDir.sub_path_len]u8 = undefined;
308 std.fs.base64_encoder.encode(&sub_path, &random_bytes);
308 _ = std.fs.base64_encoder.encode(&sub_path, &random_bytes);
309309
310310 var cwd = getCwdOrWasiPreopen();
311311 var cache_dir = cwd.makeOpenPath("zig-cache", .{}) catch