aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/Build.zig15
-rw-r--r--lib/std/Build/Step/Options.zig2
-rw-r--r--lib/std/Build/Step/Run.zig2
-rw-r--r--lib/std/fmt.zig29
-rw-r--r--lib/std/process.zig6
-rw-r--r--lib/std/zig.zig6
-rw-r--r--lib/std/zig/Server.zig5
7 files changed, 47 insertions, 18 deletions
diff --git a/lib/std/Build.zig b/lib/std/Build.zig
index 06de7aa6f4..c68170b2c1 100644
--- a/lib/std/Build.zig
+++ b/lib/std/Build.zig
@@ -2522,7 +2522,7 @@ pub const InstallDir = union(enum) {
/// function.
pub fn makeTempPath(b: *Build) []const u8 {
const rand_int = std.crypto.random.int(u64);
- const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++ hex64(rand_int);
+ const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++ std.fmt.hex(rand_int);
const result_path = b.cache_root.join(b.allocator, &.{tmp_dir_sub_path}) catch @panic("OOM");
b.cache_root.handle.makePath(tmp_dir_sub_path) catch |err| {
std.debug.print("unable to make tmp path '{s}': {s}\n", .{
@@ -2532,18 +2532,9 @@ pub fn makeTempPath(b: *Build) []const u8 {
return result_path;
}
-/// There are a few copies of this function in miscellaneous places. Would be nice to find
-/// a home for them.
+/// Deprecated; use `std.fmt.hex` instead.
pub fn hex64(x: u64) [16]u8 {
- const hex_charset = "0123456789abcdef";
- var result: [16]u8 = undefined;
- var i: usize = 0;
- while (i < 8) : (i += 1) {
- const byte: u8 = @truncate(x >> @as(u6, @intCast(8 * i)));
- result[i * 2 + 0] = hex_charset[byte >> 4];
- result[i * 2 + 1] = hex_charset[byte & 15];
- }
- return result;
+ return std.fmt.hex(x);
}
/// A pair of target query and fully resolved target.
diff --git a/lib/std/Build/Step/Options.zig b/lib/std/Build/Step/Options.zig
index 7c872db170..6131912b1b 100644
--- a/lib/std/Build/Step/Options.zig
+++ b/lib/std/Build/Step/Options.zig
@@ -457,7 +457,7 @@ fn make(step: *Step, make_options: Step.MakeOptions) !void {
const rand_int = std.crypto.random.int(u64);
const tmp_sub_path = "tmp" ++ fs.path.sep_str ++
- std.Build.hex64(rand_int) ++ fs.path.sep_str ++
+ std.fmt.hex(rand_int) ++ fs.path.sep_str ++
basename;
const tmp_sub_path_dirname = fs.path.dirname(tmp_sub_path).?;
diff --git a/lib/std/Build/Step/Run.zig b/lib/std/Build/Step/Run.zig
index d79d4647b1..0712ce8083 100644
--- a/lib/std/Build/Step/Run.zig
+++ b/lib/std/Build/Step/Run.zig
@@ -743,7 +743,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
// We do not know the final output paths yet, use temp paths to run the command.
const rand_int = std.crypto.random.int(u64);
- const tmp_dir_path = "tmp" ++ fs.path.sep_str ++ std.Build.hex64(rand_int);
+ const tmp_dir_path = "tmp" ++ fs.path.sep_str ++ std.fmt.hex(rand_int);
for (output_placeholders.items) |placeholder| {
const output_components = .{ tmp_dir_path, placeholder.output.basename };
diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig
index a4407a8cd2..a7136d99bb 100644
--- a/lib/std/fmt.zig
+++ b/lib/std/fmt.zig
@@ -2718,3 +2718,32 @@ test "recursive format function" {
var r = R{ .Leaf = 1 };
try expectFmt("Leaf(1)\n", "{}\n", .{&r});
}
+
+pub const hex_charset = "0123456789abcdef";
+
+/// Converts an unsigned integer of any multiple of u8 to an array of lowercase
+/// hex bytes, little endian.
+pub fn hex(x: anytype) [@sizeOf(@TypeOf(x)) * 2]u8 {
+ comptime assert(@typeInfo(@TypeOf(x)).Int.signedness == .unsigned);
+ var result: [@sizeOf(@TypeOf(x)) * 2]u8 = undefined;
+ var i: usize = 0;
+ while (i < result.len / 2) : (i += 1) {
+ const byte: u8 = @truncate(x >> @intCast(8 * i));
+ result[i * 2 + 0] = hex_charset[byte >> 4];
+ result[i * 2 + 1] = hex_charset[byte & 15];
+ }
+ return result;
+}
+
+test hex {
+ {
+ const x = hex(@as(u32, 0xdeadbeef));
+ try std.testing.expect(x.len == 8);
+ try std.testing.expectEqualStrings("efbeadde", &x);
+ }
+ {
+ const s = "[" ++ hex(@as(u64, 0x12345678_abcdef00)) ++ "]";
+ try std.testing.expect(s.len == 18);
+ try std.testing.expectEqualStrings("[00efcdab78563412]", s);
+ }
+}
diff --git a/lib/std/process.zig b/lib/std/process.zig
index 86654e4b5a..eca3a26c29 100644
--- a/lib/std/process.zig
+++ b/lib/std/process.zig
@@ -2032,3 +2032,9 @@ pub fn createWindowsEnvBlock(allocator: mem.Allocator, env_map: *const EnvMap) !
i += 1;
return try allocator.realloc(result, i);
}
+
+/// Logs an error and then terminates the process with exit code 1.
+pub fn fatal(comptime format: []const u8, format_arguments: anytype) noreturn {
+ std.log.err(format, format_arguments);
+ exit(1);
+}
diff --git a/lib/std/zig.zig b/lib/std/zig.zig
index 230dc45ddc..01d3aafa75 100644
--- a/lib/std/zig.zig
+++ b/lib/std/zig.zig
@@ -667,10 +667,8 @@ pub fn parseTargetQueryOrReportFatalError(
};
}
-pub fn fatal(comptime format: []const u8, args: anytype) noreturn {
- std.log.err(format, args);
- std.process.exit(1);
-}
+/// Deprecated; see `std.process.fatal`.
+pub const fatal = std.process.fatal;
/// Collects all the environment variables that Zig could possibly inspect, so
/// that we can do reflection on this and print them with `zig env`.
diff --git a/lib/std/zig/Server.zig b/lib/std/zig/Server.zig
index 2d66ee628d..4046fe4014 100644
--- a/lib/std/zig/Server.zig
+++ b/lib/std/zig/Server.zig
@@ -109,6 +109,7 @@ pub fn deinit(s: *Server) void {
pub fn receiveMessage(s: *Server) !InMessage.Header {
const Header = InMessage.Header;
const fifo = &s.receive_fifo;
+ var last_amt_zero = false;
while (true) {
const buf = fifo.readableSlice(0);
@@ -136,6 +137,10 @@ pub fn receiveMessage(s: *Server) !InMessage.Header {
const write_buffer = try fifo.writableWithSize(256);
const amt = try s.in.read(write_buffer);
fifo.update(amt);
+ if (amt == 0) {
+ if (last_amt_zero) return error.BrokenPipe;
+ last_amt_zero = true;
+ }
}
}