aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/compress.zig10
-rw-r--r--lib/std/crypto/phc_encoding.zig9
-rw-r--r--lib/std/io/tty.zig4
-rw-r--r--lib/std/process.zig35
-rw-r--r--lib/std/zig.zig9
5 files changed, 52 insertions, 15 deletions
diff --git a/lib/std/compress.zig b/lib/std/compress.zig
index 200489c18a..7cc4a80d33 100644
--- a/lib/std/compress.zig
+++ b/lib/std/compress.zig
@@ -10,10 +10,7 @@ pub const lzma2 = @import("compress/lzma2.zig");
pub const xz = @import("compress/xz.zig");
pub const zstd = @import("compress/zstandard.zig");
-pub fn HashedReader(
- comptime ReaderType: anytype,
- comptime HasherType: anytype,
-) type {
+pub fn HashedReader(ReaderType: type, HasherType: type) type {
return struct {
child_reader: ReaderType,
hasher: HasherType,
@@ -40,10 +37,7 @@ pub fn hashedReader(
return .{ .child_reader = reader, .hasher = hasher };
}
-pub fn HashedWriter(
- comptime WriterType: anytype,
- comptime HasherType: anytype,
-) type {
+pub fn HashedWriter(WriterType: type, HasherType: type) type {
return struct {
child_writer: WriterType,
hasher: HasherType,
diff --git a/lib/std/crypto/phc_encoding.zig b/lib/std/crypto/phc_encoding.zig
index ee814861b3..507a02b983 100644
--- a/lib/std/crypto/phc_encoding.zig
+++ b/lib/std/crypto/phc_encoding.zig
@@ -75,6 +75,10 @@ pub fn BinValue(comptime max_len: usize) type {
///
/// Other fields will also be deserialized from the function parameters section.
pub fn deserialize(comptime HashResult: type, str: []const u8) Error!HashResult {
+ if (@hasField(HashResult, version_param_name)) {
+ @compileError("Field name '" ++ version_param_name ++ "'' is reserved for the algorithm version");
+ }
+
var out = mem.zeroes(HashResult);
var it = mem.splitScalar(u8, str, fields_delimiter_scalar);
var set_fields: usize = 0;
@@ -198,6 +202,11 @@ pub fn calcSize(params: anytype) usize {
fn serializeTo(params: anytype, out: anytype) !void {
const HashResult = @TypeOf(params);
+
+ if (@hasField(HashResult, version_param_name)) {
+ @compileError("Field name '" ++ version_param_name ++ "'' is reserved for the algorithm version");
+ }
+
try out.writeAll(fields_delimiter);
try out.writeAll(params.alg_id);
diff --git a/lib/std/io/tty.zig b/lib/std/io/tty.zig
index 013057649e..c220e87880 100644
--- a/lib/std/io/tty.zig
+++ b/lib/std/io/tty.zig
@@ -12,9 +12,9 @@ const native_os = builtin.os.tag;
pub fn detectConfig(file: File) Config {
const force_color: ?bool = if (builtin.os.tag == .wasi)
null // wasi does not support environment variables
- else if (process.hasEnvVarConstant("NO_COLOR"))
+ else if (process.hasNonEmptyEnvVarConstant("NO_COLOR"))
false
- else if (process.hasEnvVarConstant("CLICOLOR_FORCE"))
+ else if (process.hasNonEmptyEnvVarConstant("CLICOLOR_FORCE"))
true
else
null;
diff --git a/lib/std/process.zig b/lib/std/process.zig
index 34072a4d31..bc798e68e0 100644
--- a/lib/std/process.zig
+++ b/lib/std/process.zig
@@ -431,6 +431,20 @@ pub fn hasEnvVarConstant(comptime key: []const u8) bool {
}
}
+/// On Windows, `key` must be valid UTF-8.
+pub fn hasNonEmptyEnvVarConstant(comptime key: []const u8) bool {
+ if (native_os == .windows) {
+ const key_w = comptime unicode.utf8ToUtf16LeStringLiteral(key);
+ const value = getenvW(key_w) orelse return false;
+ return value.len != 0;
+ } else if (native_os == .wasi and !builtin.link_libc) {
+ @compileError("hasNonEmptyEnvVarConstant is not supported for WASI without libc");
+ } else {
+ const value = posix.getenv(key) orelse return false;
+ return value.len != 0;
+ }
+}
+
pub const ParseEnvVarIntError = std.fmt.ParseIntError || error{EnvironmentVariableNotFound};
/// Parses an environment variable as an integer.
@@ -477,6 +491,27 @@ pub fn hasEnvVar(allocator: Allocator, key: []const u8) HasEnvVarError!bool {
}
}
+/// On Windows, if `key` is not valid [WTF-8](https://simonsapin.github.io/wtf-8/),
+/// then `error.InvalidWtf8` is returned.
+pub fn hasNonEmptyEnvVar(allocator: Allocator, key: []const u8) HasEnvVarError!bool {
+ if (native_os == .windows) {
+ var stack_alloc = std.heap.stackFallback(256 * @sizeOf(u16), allocator);
+ const stack_allocator = stack_alloc.get();
+ const key_w = try unicode.wtf8ToWtf16LeAllocZ(stack_allocator, key);
+ defer stack_allocator.free(key_w);
+ const value = getenvW(key_w) orelse return false;
+ return value.len != 0;
+ } else if (native_os == .wasi and !builtin.link_libc) {
+ var envmap = getEnvMap(allocator) catch return error.OutOfMemory;
+ defer envmap.deinit();
+ const value = envmap.getPtr(key) orelse return false;
+ return value.len != 0;
+ } else {
+ const value = posix.getenv(key) orelse return false;
+ return value.len != 0;
+ }
+}
+
/// Windows-only. Get an environment variable with a null-terminated, WTF-16 encoded name.
///
/// This function performs a Unicode-aware case-insensitive lookup using RtlEqualUnicodeString.
diff --git a/lib/std/zig.zig b/lib/std/zig.zig
index d2521be1ba..46d164dade 100644
--- a/lib/std/zig.zig
+++ b/lib/std/zig.zig
@@ -697,6 +697,10 @@ pub const EnvVar = enum {
XDG_CACHE_HOME,
HOME,
+ pub fn isSet(comptime ev: EnvVar) bool {
+ return std.process.hasNonEmptyEnvVarConstant(@tagName(ev));
+ }
+
pub fn get(ev: EnvVar, arena: std.mem.Allocator) !?[]u8 {
if (std.process.getEnvVarOwned(arena, @tagName(ev))) |value| {
return value;
@@ -709,11 +713,6 @@ pub const EnvVar = enum {
pub fn getPosix(comptime ev: EnvVar) ?[:0]const u8 {
return std.posix.getenvZ(@tagName(ev));
}
-
- pub fn isSet(ev: EnvVar, arena: std.mem.Allocator) !bool {
- const value = try ev.get(arena) orelse return false;
- return value.len != 0;
- }
};
pub const SimpleComptimeReason = enum(u32) {