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