authorgravatar for suirad@users.noreply.github.comSuirad <suirad@users.noreply.github.com> 2018-11-29 03:24:36-06:00
committergravatar for suirad@users.noreply.github.comSuirad <suirad@users.noreply.github.com> 2018-11-30 02:08:34-06:00
log1fa2217c1008fefa7084ea34fedcf79ad214a02e
tree2a28bdd7007f04bc7dd2f8c9fb27e5fdeb95954b
parent24592d0216ce4830e9f0dd51a7d2542f1f8afa05

Simplify implementation


1 files changed, 12 insertions(+), 18 deletions(-)

std/os/index.zig+12-18
...@@ -15,7 +15,7 @@ test "std.os" {...@@ -15,7 +15,7 @@ test "std.os" {
15 _ = @import("get_user_id.zig");15 _ = @import("get_user_id.zig");
16 _ = @import("linux/index.zig");16 _ = @import("linux/index.zig");
17 _ = @import("path.zig");17 _ = @import("path.zig");
18 _ = @import("test.zig"); 18 _ = @import("test.zig");
19 _ = @import("time.zig");19 _ = @import("time.zig");
20 _ = @import("windows/index.zig");20 _ = @import("windows/index.zig");
21 _ = @import("get_app_data_dir.zig");21 _ = @import("get_app_data_dir.zig");
...@@ -705,28 +705,26 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {...@@ -705,28 +705,26 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
705 const ptr = windows.GetEnvironmentStringsW() orelse return error.OutOfMemory;705 const ptr = windows.GetEnvironmentStringsW() orelse return error.OutOfMemory;
706 defer assert(windows.FreeEnvironmentStringsW(ptr) != 0);706 defer assert(windows.FreeEnvironmentStringsW(ptr) != 0);
707707
708 var buf: [100]u8 = undefined;
709
708 var i: usize = 0;710 var i: usize = 0;
709 while (true) {711 while (true) {
710 if (ptr[i] == 0) return result;712 if (ptr[i] == 0) return result;
711713
712 const key_start = i;714 const key_start = i;
715 var fallocator = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator;
713716
714 while (ptr[i] != 0 and ptr[i] != '=') : (i += 1) {}717 while (ptr[i] != 0 and ptr[i] != '=') : (i += 1) {}
715 718
716 const stack_var_len = 50;
717 const key_slice = ptr[key_start..i];719 const key_slice = ptr[key_start..i];
718 var key: []u8 = undefined;720 var key: []u8 = undefined;
719 var heap_key = false;721 var heap_key = false;
720722
721 /// revisit needing the "-@sizeof(usize)*2" 723 key = std.unicode.utf16leToUtf8Alloc(fallocator, key_slice) catch undefined;
722 /// after https://github.com/ziglang/zig/issues/1774724
723 if (key_slice.len < stack_var_len-@sizeOf(usize)*2) {725 if (key.len == 0) {
724 var buf = []u8{0} ** stack_var_len;
725 var fallocator = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator;
726 key = try std.unicode.utf16leToUtf8Alloc(fallocator, key_slice);
727 } else {
728 key = try std.unicode.utf16leToUtf8Alloc(allocator, key_slice);726 key = try std.unicode.utf16leToUtf8Alloc(allocator, key_slice);
729 heap_key = true; // key needs to outlive this scope, so we cannot defer727 heap_key = true;
730 }728 }
731729
732 if (ptr[i] == '=') i += 1;730 if (ptr[i] == '=') i += 1;
...@@ -738,15 +736,11 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {...@@ -738,15 +736,11 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
738 var value: []u8 = undefined;736 var value: []u8 = undefined;
739 var heap_value = false;737 var heap_value = false;
740738
741 /// revisit needing the "-@sizeof(usize)*2"739 value = std.unicode.utf16leToUtf8Alloc(fallocator, value_slice) catch undefined;
742 /// after https://github.com/ziglang/zig/issues/1774740
743 if (value_slice.len < stack_var_len-@sizeOf(usize)*2) {741 if (value.len == 0) {
744 var buf = []u8{0} ** stack_var_len;
745 var fallocator = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator;
746 value = try std.unicode.utf16leToUtf8Alloc(fallocator, value_slice);
747 } else {
748 value = try std.unicode.utf16leToUtf8Alloc(allocator, value_slice);742 value = try std.unicode.utf16leToUtf8Alloc(allocator, value_slice);
749 heap_value = true; // value needs to outlive this scope, so we cannot defer743 heap_value = true;
750 }744 }
751745
752 i += 1; // skip over null byte746 i += 1; // skip over null byte