| ... | ... | @@ -705,54 +705,28 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap { |
| 705 | 705 | const ptr = windows.GetEnvironmentStringsW() orelse return error.OutOfMemory; |
| 706 | 706 | defer assert(windows.FreeEnvironmentStringsW(ptr) != 0); |
| 707 | 707 | |
| 708 | | var buf: [100]u8 = undefined; |
| 709 | | |
| 710 | 708 | var i: usize = 0; |
| 711 | 709 | while (true) { |
| 712 | 710 | if (ptr[i] == 0) return result; |
| 713 | 711 | |
| 714 | 712 | const key_start = i; |
| 715 | | var fallocator = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator; |
| 716 | 713 | |
| 717 | 714 | while (ptr[i] != 0 and ptr[i] != '=') : (i += 1) {} |
| 718 | | |
| 719 | | const key_slice = ptr[key_start..i]; |
| 720 | | var key: []u8 = undefined; |
| 721 | | var heap_key = false; |
| 722 | | |
| 723 | | key = std.unicode.utf16leToUtf8Alloc(fallocator, key_slice) catch undefined; |
| 724 | | |
| 725 | | if (key.len == 0) { |
| 726 | | key = try std.unicode.utf16leToUtf8Alloc(allocator, key_slice); |
| 727 | | heap_key = true; |
| 728 | | } |
| 715 | const key_w = ptr[key_start..i]; |
| 716 | const key = try std.unicode.utf16leToUtf8Alloc(allocator, key_w); |
| 717 | errdefer allocator.free(key); |
| 729 | 718 | |
| 730 | 719 | if (ptr[i] == '=') i += 1; |
| 731 | 720 | |
| 732 | 721 | const value_start = i; |
| 733 | 722 | while (ptr[i] != 0) : (i += 1) {} |
| 734 | | |
| 735 | | const value_slice = ptr[value_start..i]; |
| 736 | | var value: []u8 = undefined; |
| 737 | | var heap_value = false; |
| 738 | | |
| 739 | | value = std.unicode.utf16leToUtf8Alloc(fallocator, value_slice) catch undefined; |
| 740 | | |
| 741 | | if (value.len == 0) { |
| 742 | | value = try std.unicode.utf16leToUtf8Alloc(allocator, value_slice); |
| 743 | | heap_value = true; |
| 744 | | } |
| 723 | const value_w = ptr[value_start..i]; |
| 724 | const value = try std.unicode.utf16leToUtf8Alloc(allocator, value_w); |
| 725 | errdefer allocator.free(value); |
| 745 | 726 | |
| 746 | 727 | i += 1; // skip over null byte |
| 747 | 728 | |
| 748 | | try result.set(key, value); |
| 749 | | |
| 750 | | if (heap_key) { |
| 751 | | allocator.free(key); |
| 752 | | } |
| 753 | | if (heap_value) { |
| 754 | | allocator.free(value); |
| 755 | | } |
| 729 | try result.setMove(key, value); |
| 756 | 730 | } |
| 757 | 731 | } else { |
| 758 | 732 | for (posix_environ_raw) |ptr| { |
| ... | ... | @@ -795,9 +769,6 @@ pub fn getEnvPosix(key: []const u8) ?[]const u8 { |
| 795 | 769 | pub const GetEnvVarOwnedError = error{ |
| 796 | 770 | OutOfMemory, |
| 797 | 771 | EnvironmentVariableNotFound, |
| 798 | | DanglingSurrogateHalf, |
| 799 | | ExpectedSecondSurrogateHalf, |
| 800 | | UnexpectedSecondSurrogateHalf, |
| 801 | 772 | |
| 802 | 773 | /// See https://github.com/ziglang/zig/issues/1774 |
| 803 | 774 | InvalidUtf8, |
| ... | ... | @@ -833,7 +804,12 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned |
| 833 | 804 | continue; |
| 834 | 805 | } |
| 835 | 806 | |
| 836 | | return try std.unicode.utf16leToUtf8Alloc(allocator, buf); |
| 807 | return std.unicode.utf16leToUtf8Alloc(allocator, buf) catch |err| switch (err) { |
| 808 | error.DanglingSurrogateHalf => return error.InvalidUtf8, |
| 809 | error.ExpectedSecondSurrogateHalf => return error.InvalidUtf8, |
| 810 | error.UnexpectedSecondSurrogateHalf => return error.InvalidUtf8, |
| 811 | error.OutOfMemory => return error.OutOfMemory, |
| 812 | }; |
| 837 | 813 | } |
| 838 | 814 | } else { |
| 839 | 815 | const result = getEnvPosix(key) orelse return error.EnvironmentVariableNotFound; |
| ... | ... | @@ -846,7 +822,6 @@ test "os.getEnvVarOwned" { |
| 846 | 822 | debug.assertError(getEnvVarOwned(ga, "BADENV"), error.EnvironmentVariableNotFound); |
| 847 | 823 | } |
| 848 | 824 | |
| 849 | | |
| 850 | 825 | /// Caller must free the returned memory. |
| 851 | 826 | pub fn getCwdAlloc(allocator: *Allocator) ![]u8 { |
| 852 | 827 | var buf: [MAX_PATH_BYTES]u8 = undefined; |