authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2025-03-17 17:24:00-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2025-03-17 17:53:38-07:00
logb2cc408a3e5ae0f8c2550c8828f6d388c6969b5a
treee2a0e1f2c3703250c95eafda8de0395f88104598
parent423761bb6de68bec9482dbc3b3c21b2b5c74a2b9

std.process: Allow WTF-8 in env var functions with comptime-known keys


1 files changed, 6 insertions(+), 6 deletions(-)

lib/std/process.zig+6-6
...@@ -419,10 +419,10 @@ pub fn getEnvVarOwned(allocator: Allocator, key: []const u8) GetEnvVarOwnedError...@@ -419,10 +419,10 @@ pub fn getEnvVarOwned(allocator: Allocator, key: []const u8) GetEnvVarOwnedError
419 }419 }
420}420}
421421
422/// On Windows, `key` must be valid UTF-8.422/// On Windows, `key` must be valid WTF-8.
423pub fn hasEnvVarConstant(comptime key: []const u8) bool {423pub fn hasEnvVarConstant(comptime key: []const u8) bool {
424 if (native_os == .windows) {424 if (native_os == .windows) {
425 const key_w = comptime unicode.utf8ToUtf16LeStringLiteral(key);425 const key_w = comptime unicode.wtf8ToWtf16LeStringLiteral(key);
426 return getenvW(key_w) != null;426 return getenvW(key_w) != null;
427 } else if (native_os == .wasi and !builtin.link_libc) {427 } else if (native_os == .wasi and !builtin.link_libc) {
428 @compileError("hasEnvVarConstant is not supported for WASI without libc");428 @compileError("hasEnvVarConstant is not supported for WASI without libc");
...@@ -431,10 +431,10 @@ pub fn hasEnvVarConstant(comptime key: []const u8) bool {...@@ -431,10 +431,10 @@ pub fn hasEnvVarConstant(comptime key: []const u8) bool {
431 }431 }
432}432}
433433
434/// On Windows, `key` must be valid UTF-8.434/// On Windows, `key` must be valid WTF-8.
435pub fn hasNonEmptyEnvVarConstant(comptime key: []const u8) bool {435pub fn hasNonEmptyEnvVarConstant(comptime key: []const u8) bool {
436 if (native_os == .windows) {436 if (native_os == .windows) {
437 const key_w = comptime unicode.utf8ToUtf16LeStringLiteral(key);437 const key_w = comptime unicode.wtf8ToWtf16LeStringLiteral(key);
438 const value = getenvW(key_w) orelse return false;438 const value = getenvW(key_w) orelse return false;
439 return value.len != 0;439 return value.len != 0;
440 } else if (native_os == .wasi and !builtin.link_libc) {440 } else if (native_os == .wasi and !builtin.link_libc) {
...@@ -451,10 +451,10 @@ pub const ParseEnvVarIntError = std.fmt.ParseIntError || error{EnvironmentVariab...@@ -451,10 +451,10 @@ pub const ParseEnvVarIntError = std.fmt.ParseIntError || error{EnvironmentVariab
451///451///
452/// Since the key is comptime-known, no allocation is needed.452/// Since the key is comptime-known, no allocation is needed.
453///453///
454/// On Windows, `key` must be valid UTF-8.454/// On Windows, `key` must be valid WTF-8.
455pub fn parseEnvVarInt(comptime key: []const u8, comptime I: type, base: u8) ParseEnvVarIntError!I {455pub fn parseEnvVarInt(comptime key: []const u8, comptime I: type, base: u8) ParseEnvVarIntError!I {
456 if (native_os == .windows) {456 if (native_os == .windows) {
457 const key_w = comptime std.unicode.utf8ToUtf16LeStringLiteral(key);457 const key_w = comptime std.unicode.wtf8ToWtf16LeStringLiteral(key);
458 const text = getenvW(key_w) orelse return error.EnvironmentVariableNotFound;458 const text = getenvW(key_w) orelse return error.EnvironmentVariableNotFound;
459 return std.fmt.parseIntWithGenericCharacter(I, u16, text, base);459 return std.fmt.parseIntWithGenericCharacter(I, u16, text, base);
460 } else if (native_os == .wasi and !builtin.link_libc) {460 } else if (native_os == .wasi and !builtin.link_libc) {