| author | |
| committer | |
| log | eb4028bf30bca7036c6bdc65feb321b163e84ecd |
| tree | 2dab5bba8b6a8e6c0e95b7db47618a1e4ff280a7 |
| parent | 9d00f69be58e7b807e26ba8a38050dd486d487f4 |
converts an unsigned integer into an array9 files changed, 55 insertions(+), 47 deletions(-)
lib/std/Build.zig+3-12| ... | ... | @@ -2522,7 +2522,7 @@ pub const InstallDir = union(enum) { |
| 2522 | 2522 | /// function. |
| 2523 | 2523 | pub fn makeTempPath(b: *Build) []const u8 { |
| 2524 | 2524 | 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); | |
| 2526 | 2526 | const result_path = b.cache_root.join(b.allocator, &.{tmp_dir_sub_path}) catch @panic("OOM"); |
| 2527 | 2527 | b.cache_root.handle.makePath(tmp_dir_sub_path) catch |err| { |
| 2528 | 2528 | std.debug.print("unable to make tmp path '{s}': {s}\n", .{ |
| ... | ... | @@ -2532,18 +2532,9 @@ pub fn makeTempPath(b: *Build) []const u8 { |
| 2532 | 2532 | return result_path; |
| 2533 | 2533 | } |
| 2534 | 2534 | |
| 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. | |
| 2537 | 2536 | pub 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); | |
| 2547 | 2538 | } |
| 2548 | 2539 | |
| 2549 | 2540 | /// 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 { |
| 457 | 457 | |
| 458 | 458 | const rand_int = std.crypto.random.int(u64); |
| 459 | 459 | 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 ++ | |
| 461 | 461 | basename; |
| 462 | 462 | const tmp_sub_path_dirname = fs.path.dirname(tmp_sub_path).?; |
| 463 | 463 |
lib/std/Build/Step/Run.zig+1-1| ... | ... | @@ -743,7 +743,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void { |
| 743 | 743 | |
| 744 | 744 | // We do not know the final output paths yet, use temp paths to run the command. |
| 745 | 745 | 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); | |
| 747 | 747 | |
| 748 | 748 | for (output_placeholders.items) |placeholder| { |
| 749 | 749 | const output_components = .{ tmp_dir_path, placeholder.output.basename }; |
lib/std/fmt.zig+35| ... | ... | @@ -2718,3 +2718,38 @@ test "recursive format function" { |
| 2718 | 2718 | var r = R{ .Leaf = 1 }; |
| 2719 | 2719 | try expectFmt("Leaf(1)\n", "{}\n", .{&r}); |
| 2720 | 2720 | } |
| 2721 | ||
| 2722 | pub const hex_charset = "0123456789abcdef"; | |
| 2723 | ||
| 2724 | /// Converts an unsigned integer of any multiple of u8 to an array of lowercase | |
| 2725 | /// hex bytes. | |
| 2726 | pub 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 | ||
| 2738 | test 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 { |
| 2106 | 2106 | const tmp_artifact_directory = d: { |
| 2107 | 2107 | const s = std.fs.path.sep_str; |
| 2108 | 2108 | 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); | |
| 2110 | 2110 | |
| 2111 | 2111 | const path = try comp.local_cache_directory.join(gpa, &.{tmp_dir_sub_path}); |
| 2112 | 2112 | errdefer gpa.free(path); |
| ... | ... | @@ -2298,7 +2298,7 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) !void { |
| 2298 | 2298 | } else unreachable; |
| 2299 | 2299 | |
| 2300 | 2300 | 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); | |
| 2302 | 2302 | const o_sub_path = "o" ++ s ++ digest; |
| 2303 | 2303 | |
| 2304 | 2304 | // Work around windows `AccessDenied` if any files within this |
src/Package/Fetch.zig+1-1| ... | ... | @@ -445,7 +445,7 @@ fn runResource( |
| 445 | 445 | const s = fs.path.sep_str; |
| 446 | 446 | const cache_root = f.job_queue.global_cache; |
| 447 | 447 | 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); | |
| 449 | 449 | |
| 450 | 450 | const package_sub_path = blk: { |
| 451 | 451 | const tmp_directory_path = try cache_root.join(arena, &.{tmp_dir_sub_path}); |
src/Package/Manifest.zig+9-26| ... | ... | @@ -1,3 +1,12 @@ |
| 1 | const Manifest = @This(); | |
| 2 | const std = @import("std"); | |
| 3 | const mem = std.mem; | |
| 4 | const Allocator = std.mem.Allocator; | |
| 5 | const assert = std.debug.assert; | |
| 6 | const Ast = std.zig.Ast; | |
| 7 | const testing = std.testing; | |
| 8 | const hex_charset = std.fmt.hex_charset; | |
| 9 | ||
| 1 | 10 | pub const max_bytes = 10 * 1024 * 1024; |
| 2 | 11 | pub const basename = "build.zig.zon"; |
| 3 | 12 | pub const Hash = std.crypto.hash.sha2.Sha256; |
| ... | ... | @@ -153,24 +162,6 @@ pub fn copyErrorsIntoBundle( |
| 153 | 162 | } |
| 154 | 163 | } |
| 155 | 164 | |
| 156 | const hex_charset = "0123456789abcdef"; | |
| 157 | ||
| 158 | pub 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 | ||
| 169 | test hex64 { | |
| 170 | const s = "[" ++ hex64(0x12345678_abcdef00) ++ "]"; | |
| 171 | try std.testing.expectEqualStrings("[00efcdab78563412]", s); | |
| 172 | } | |
| 173 | ||
| 174 | 165 | pub fn hexDigest(digest: Digest) MultiHashHexDigest { |
| 175 | 166 | var result: MultiHashHexDigest = undefined; |
| 176 | 167 | |
| ... | ... | @@ -590,14 +581,6 @@ const Parse = struct { |
| 590 | 581 | } |
| 591 | 582 | }; |
| 592 | 583 | |
| 593 | const Manifest = @This(); | |
| 594 | const std = @import("std"); | |
| 595 | const mem = std.mem; | |
| 596 | const Allocator = std.mem.Allocator; | |
| 597 | const assert = std.debug.assert; | |
| 598 | const Ast = std.zig.Ast; | |
| 599 | const testing = std.testing; | |
| 600 | ||
| 601 | 584 | test "basic" { |
| 602 | 585 | const gpa = testing.allocator; |
| 603 | 586 |
src/link.zig+1-1| ... | ... | @@ -1031,7 +1031,7 @@ pub fn spawnLld( |
| 1031 | 1031 | error.NameTooLong => err: { |
| 1032 | 1032 | const s = fs.path.sep_str; |
| 1033 | 1033 | 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"; | |
| 1035 | 1035 | |
| 1036 | 1036 | const rsp_file = try comp.local_cache_directory.handle.createFileZ(rsp_path, .{}); |
| 1037 | 1037 | 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 { |
| 4746 | 4746 | // the strategy is to choose a temporary file name ahead of time, and then |
| 4747 | 4747 | // read this file in the parent to obtain the results, in the case the child |
| 4748 | 4748 | // 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)); | |
| 4750 | 4750 | try child_argv.append("-Z" ++ results_tmp_file_nonce); |
| 4751 | 4751 | |
| 4752 | 4752 | var color: Color = .auto; |
| ... | ... | @@ -7196,8 +7196,7 @@ fn createDependenciesModule( |
| 7196 | 7196 | // Atomically create the file in a directory named after the hash of its contents. |
| 7197 | 7197 | const basename = "dependencies.zig"; |
| 7198 | 7198 | 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); | |
| 7201 | 7200 | { |
| 7202 | 7201 | var tmp_dir = try local_cache_directory.handle.makeOpenPath(tmp_dir_sub_path, .{}); |
| 7203 | 7202 | defer tmp_dir.close(); |