authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-19 17:39:55-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-20 01:06:29-07:00
logeb4028bf30bca7036c6bdc65feb321b163e84ecd
tree2dab5bba8b6a8e6c0e95b7db47618a1e4ff280a7
parent9d00f69be58e7b807e26ba8a38050dd486d487f4

add std.fmt.hex

converts an unsigned integer into an array

9 files changed, 55 insertions(+), 47 deletions(-)

lib/std/Build.zig+3-12
......@@ -2522,7 +2522,7 @@ pub const InstallDir = union(enum) {
25222522/// function.
25232523pub fn makeTempPath(b: *Build) []const u8 {
25242524 const rand_int = std.crypto.random.int(u64);
2525 const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++ hex64(rand_int);
2525 const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++ std.fmt.hex(rand_int);
25262526 const result_path = b.cache_root.join(b.allocator, &.{tmp_dir_sub_path}) catch @panic("OOM");
25272527 b.cache_root.handle.makePath(tmp_dir_sub_path) catch |err| {
25282528 std.debug.print("unable to make tmp path '{s}': {s}\n", .{
......@@ -2532,18 +2532,9 @@ pub fn makeTempPath(b: *Build) []const u8 {
25322532 return result_path;
25332533}
25342534
2535/// There are a few copies of this function in miscellaneous places. Would be nice to find
2536/// a home for them.
2535/// Deprecated; use `std.fmt.hex` instead.
25372536pub fn hex64(x: u64) [16]u8 {
2538 const hex_charset = "0123456789abcdef";
2539 var result: [16]u8 = undefined;
2540 var i: usize = 0;
2541 while (i < 8) : (i += 1) {
2542 const byte: u8 = @truncate(x >> @as(u6, @intCast(8 * i)));
2543 result[i * 2 + 0] = hex_charset[byte >> 4];
2544 result[i * 2 + 1] = hex_charset[byte & 15];
2545 }
2546 return result;
2537 return std.fmt.hex(x);
25472538}
25482539
25492540/// A pair of target query and fully resolved target.
lib/std/Build/Step/Options.zig+1-1
......@@ -457,7 +457,7 @@ fn make(step: *Step, make_options: Step.MakeOptions) !void {
457457
458458 const rand_int = std.crypto.random.int(u64);
459459 const tmp_sub_path = "tmp" ++ fs.path.sep_str ++
460 std.Build.hex64(rand_int) ++ fs.path.sep_str ++
460 std.fmt.hex(rand_int) ++ fs.path.sep_str ++
461461 basename;
462462 const tmp_sub_path_dirname = fs.path.dirname(tmp_sub_path).?;
463463
lib/std/Build/Step/Run.zig+1-1
......@@ -743,7 +743,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
743743
744744 // We do not know the final output paths yet, use temp paths to run the command.
745745 const rand_int = std.crypto.random.int(u64);
746 const tmp_dir_path = "tmp" ++ fs.path.sep_str ++ std.Build.hex64(rand_int);
746 const tmp_dir_path = "tmp" ++ fs.path.sep_str ++ std.fmt.hex(rand_int);
747747
748748 for (output_placeholders.items) |placeholder| {
749749 const output_components = .{ tmp_dir_path, placeholder.output.basename };
lib/std/fmt.zig+35
......@@ -2718,3 +2718,38 @@ test "recursive format function" {
27182718 var r = R{ .Leaf = 1 };
27192719 try expectFmt("Leaf(1)\n", "{}\n", .{&r});
27202720}
2721
2722pub const hex_charset = "0123456789abcdef";
2723
2724/// Converts an unsigned integer of any multiple of u8 to an array of lowercase
2725/// hex bytes.
2726pub fn hex(x: anytype) [@sizeOf(@TypeOf(x)) * 2]u8 {
2727 comptime assert(@typeInfo(@TypeOf(x)).Int.signedness == .unsigned);
2728 var result: [@sizeOf(@TypeOf(x)) * 2]u8 = undefined;
2729 var i: usize = 0;
2730 while (i < result.len / 2) : (i += 1) {
2731 const byte: u8 = @truncate(x >> @intCast(8 * i));
2732 result[i * 2 + 0] = hex_charset[byte >> 4];
2733 result[i * 2 + 1] = hex_charset[byte & 15];
2734 }
2735 return result;
2736}
2737
2738test hex {
2739 {
2740 const x = hex(@as(u32, 0xdeadbeef));
2741 try std.testing.expect(x.len == 8);
2742 switch (builtin.cpu.arch.endian()) {
2743 .little => try std.testing.expectEqualStrings("efbeadde", &x),
2744 .big => try std.testing.expectEqualStrings("deadbeef", &x),
2745 }
2746 }
2747 {
2748 const s = "[" ++ hex(@as(u64, 0x12345678_abcdef00)) ++ "]";
2749 try std.testing.expect(s.len == 18);
2750 switch (builtin.cpu.arch.endian()) {
2751 .little => try std.testing.expectEqualStrings("[00efcdab78563412]", s),
2752 .big => try std.testing.expectEqualStrings("[12345678abcdef00]", s),
2753 }
2754 }
2755}
src/Compilation.zig+2-2
......@@ -2106,7 +2106,7 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) !void {
21062106 const tmp_artifact_directory = d: {
21072107 const s = std.fs.path.sep_str;
21082108 tmp_dir_rand_int = std.crypto.random.int(u64);
2109 const tmp_dir_sub_path = "tmp" ++ s ++ Package.Manifest.hex64(tmp_dir_rand_int);
2109 const tmp_dir_sub_path = "tmp" ++ s ++ std.fmt.hex(tmp_dir_rand_int);
21102110
21112111 const path = try comp.local_cache_directory.join(gpa, &.{tmp_dir_sub_path});
21122112 errdefer gpa.free(path);
......@@ -2298,7 +2298,7 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) !void {
22982298 } else unreachable;
22992299
23002300 const s = std.fs.path.sep_str;
2301 const tmp_dir_sub_path = "tmp" ++ s ++ Package.Manifest.hex64(tmp_dir_rand_int);
2301 const tmp_dir_sub_path = "tmp" ++ s ++ std.fmt.hex(tmp_dir_rand_int);
23022302 const o_sub_path = "o" ++ s ++ digest;
23032303
23042304 // Work around windows `AccessDenied` if any files within this
src/Package/Fetch.zig+1-1
......@@ -445,7 +445,7 @@ fn runResource(
445445 const s = fs.path.sep_str;
446446 const cache_root = f.job_queue.global_cache;
447447 const rand_int = std.crypto.random.int(u64);
448 const tmp_dir_sub_path = "tmp" ++ s ++ Manifest.hex64(rand_int);
448 const tmp_dir_sub_path = "tmp" ++ s ++ std.fmt.hex(rand_int);
449449
450450 const package_sub_path = blk: {
451451 const tmp_directory_path = try cache_root.join(arena, &.{tmp_dir_sub_path});
src/Package/Manifest.zig+9-26
......@@ -1,3 +1,12 @@
1const Manifest = @This();
2const std = @import("std");
3const mem = std.mem;
4const Allocator = std.mem.Allocator;
5const assert = std.debug.assert;
6const Ast = std.zig.Ast;
7const testing = std.testing;
8const hex_charset = std.fmt.hex_charset;
9
110pub const max_bytes = 10 * 1024 * 1024;
211pub const basename = "build.zig.zon";
312pub const Hash = std.crypto.hash.sha2.Sha256;
......@@ -153,24 +162,6 @@ pub fn copyErrorsIntoBundle(
153162 }
154163}
155164
156const hex_charset = "0123456789abcdef";
157
158pub fn hex64(x: u64) [16]u8 {
159 var result: [16]u8 = undefined;
160 var i: usize = 0;
161 while (i < 8) : (i += 1) {
162 const byte = @as(u8, @truncate(x >> @as(u6, @intCast(8 * i))));
163 result[i * 2 + 0] = hex_charset[byte >> 4];
164 result[i * 2 + 1] = hex_charset[byte & 15];
165 }
166 return result;
167}
168
169test hex64 {
170 const s = "[" ++ hex64(0x12345678_abcdef00) ++ "]";
171 try std.testing.expectEqualStrings("[00efcdab78563412]", s);
172}
173
174165pub fn hexDigest(digest: Digest) MultiHashHexDigest {
175166 var result: MultiHashHexDigest = undefined;
176167
......@@ -590,14 +581,6 @@ const Parse = struct {
590581 }
591582};
592583
593const Manifest = @This();
594const std = @import("std");
595const mem = std.mem;
596const Allocator = std.mem.Allocator;
597const assert = std.debug.assert;
598const Ast = std.zig.Ast;
599const testing = std.testing;
600
601584test "basic" {
602585 const gpa = testing.allocator;
603586
src/link.zig+1-1
......@@ -1031,7 +1031,7 @@ pub fn spawnLld(
10311031 error.NameTooLong => err: {
10321032 const s = fs.path.sep_str;
10331033 const rand_int = std.crypto.random.int(u64);
1034 const rsp_path = "tmp" ++ s ++ Package.Manifest.hex64(rand_int) ++ ".rsp";
1034 const rsp_path = "tmp" ++ s ++ std.fmt.hex(rand_int) ++ ".rsp";
10351035
10361036 const rsp_file = try comp.local_cache_directory.handle.createFileZ(rsp_path, .{});
10371037 defer comp.local_cache_directory.handle.deleteFileZ(rsp_path) catch |err|
src/main.zig+2-3
......@@ -4746,7 +4746,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
47464746 // the strategy is to choose a temporary file name ahead of time, and then
47474747 // read this file in the parent to obtain the results, in the case the child
47484748 // exits with code 3.
4749 const results_tmp_file_nonce = Package.Manifest.hex64(std.crypto.random.int(u64));
4749 const results_tmp_file_nonce = std.fmt.hex(std.crypto.random.int(u64));
47504750 try child_argv.append("-Z" ++ results_tmp_file_nonce);
47514751
47524752 var color: Color = .auto;
......@@ -7196,8 +7196,7 @@ fn createDependenciesModule(
71967196 // Atomically create the file in a directory named after the hash of its contents.
71977197 const basename = "dependencies.zig";
71987198 const rand_int = std.crypto.random.int(u64);
7199 const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++
7200 Package.Manifest.hex64(rand_int);
7199 const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++ std.fmt.hex(rand_int);
72017200 {
72027201 var tmp_dir = try local_cache_directory.handle.makeOpenPath(tmp_dir_sub_path, .{});
72037202 defer tmp_dir.close();