authorgravatar for ilmari.autio@gmail.comIlmari Autio <ilmari.autio@gmail.com> 2020-03-02 23:15:36+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-02 13:01:32-04:00
logba1a8b64c44b9b99e9f0002555b9b4a35092e8f6
treed2b4343ab06caef96b9800f1a3b71f6b7da551b1
parent503c4207972b1357ac11c6a6fb2e49958b68c2dc
signaturelock-open Commit is signed but in an unrecognized format.

make std.os.getenvW case insensitive

partially addresses #4603 Fixing std.process.getEnvMap is NOT included in this commit.

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

lib/std/os.zig+15-1
...@@ -21,6 +21,7 @@ const assert = std.debug.assert;...@@ -21,6 +21,7 @@ const assert = std.debug.assert;
21const math = std.math;21const math = std.math;
22const mem = std.mem;22const mem = std.mem;
23const elf = std.elf;23const elf = std.elf;
24const unicode = std.unicode;
24const dl = @import("dynamic_library.zig");25const dl = @import("dynamic_library.zig");
25const MAX_PATH_BYTES = std.fs.MAX_PATH_BYTES;26const MAX_PATH_BYTES = std.fs.MAX_PATH_BYTES;
2627
...@@ -1206,6 +1207,19 @@ pub fn getenvZ(key: [*:0]const u8) ?[]const u8 {...@@ -1206,6 +1207,19 @@ pub fn getenvZ(key: [*:0]const u8) ?[]const u8 {
1206 return getenv(mem.spanZ(key));1207 return getenv(mem.spanZ(key));
1207}1208}
12081209
1210fn utf16LeAsciiEqlIgnoreCase(a: []const u16, b: []const u16) bool {
1211 if (a.len != b.len) return false;
1212 var a_it = unicode.Utf16LeIterator.init(a);
1213 var b_it = unicode.Utf16LeIterator.init(b);
1214 while (a_it.nextCodepoint() catch return false) |a_codepoint| {
1215 const b_codepoint = (b_it.nextCodepoint() catch return false) orelse return false;
1216 const upper_a = if (a_codepoint >= 97 and a_codepoint <= 122) a_codepoint & 0b11011111 else a_codepoint;
1217 const upper_b = if (b_codepoint >= 97 and b_codepoint <= 122) b_codepoint & 0b11011111 else b_codepoint;
1218 if (upper_a != upper_b) return false;
1219 }
1220 return true;
1221}
1222
1209/// Windows-only. Get an environment variable with a null-terminated, WTF-16 encoded name.1223/// Windows-only. Get an environment variable with a null-terminated, WTF-16 encoded name.
1210/// See also `getenv`.1224/// See also `getenv`.
1211pub fn getenvW(key: [*:0]const u16) ?[:0]const u16 {1225pub fn getenvW(key: [*:0]const u16) ?[:0]const u16 {
...@@ -1227,7 +1241,7 @@ pub fn getenvW(key: [*:0]const u16) ?[:0]const u16 {...@@ -1227,7 +1241,7 @@ pub fn getenvW(key: [*:0]const u16) ?[:0]const u16 {
1227 while (ptr[i] != 0) : (i += 1) {}1241 while (ptr[i] != 0) : (i += 1) {}
1228 const this_value = ptr[value_start..i :0];1242 const this_value = ptr[value_start..i :0];
12291243
1230 if (mem.eql(u16, key_slice, this_key)) return this_value;1244 if (utf16LeAsciiEqlIgnoreCase(key_slice, this_key)) return this_value;
12311245
1232 i += 1; // skip over null byte1246 i += 1; // skip over null byte
1233 }1247 }