authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-12-13 17:13:10-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-12-13 17:13:10-05:00
logfff6e471259071d9ad8466bcdb0ce42c9d7a90d4
tree84c5b8ce41886b3740970e76506050c82a718183
parent457f03e37d368bcc470fb690778d880fd1878082
signature Commit is signed but in an unrecognized format.

fixups


2 files changed, 40 insertions(+), 47 deletions(-)

std/buf_map.zig+27-9
...@@ -16,7 +16,7 @@ pub const BufMap = struct {...@@ -16,7 +16,7 @@ pub const BufMap = struct {
16 return self;16 return self;
17 }17 }
1818
19 pub fn deinit(self: *const BufMap) void {19 pub fn deinit(self: *BufMap) void {
20 var it = self.hash_map.iterator();20 var it = self.hash_map.iterator();
21 while (true) {21 while (true) {
22 const entry = it.next() orelse break;22 const entry = it.next() orelse break;
...@@ -27,16 +27,34 @@ pub const BufMap = struct {...@@ -27,16 +27,34 @@ pub const BufMap = struct {
27 self.hash_map.deinit();27 self.hash_map.deinit();
28 }28 }
2929
30 /// Same as `set` but the key and value become owned by the BufMap rather
31 /// than being copied.
32 /// If `setMove` fails, the ownership of key and value does not transfer.
33 pub fn setMove(self: *BufMap, key: []u8, value: []u8) !void {
34 const get_or_put = try self.hash_map.getOrPut(key);
35 if (get_or_put.found_existing) {
36 self.free(get_or_put.kv.key);
37 get_or_put.kv.key = key;
38 }
39 get_or_put.kv.value = value;
40 }
41
42 /// `key` and `value` are copied into the BufMap.
30 pub fn set(self: *BufMap, key: []const u8, value: []const u8) !void {43 pub fn set(self: *BufMap, key: []const u8, value: []const u8) !void {
31 self.delete(key);
32 const key_copy = try self.copy(key);
33 errdefer self.free(key_copy);
34 const value_copy = try self.copy(value);44 const value_copy = try self.copy(value);
35 errdefer self.free(value_copy);45 errdefer self.free(value_copy);
36 _ = try self.hash_map.put(key_copy, value_copy);46 // Avoid copying key if it already exists
47 const get_or_put = try self.hash_map.getOrPut(key);
48 if (!get_or_put.found_existing) {
49 get_or_put.kv.key = self.copy(key) catch |err| {
50 _ = self.hash_map.remove(key);
51 return err;
52 };
53 }
54 get_or_put.kv.value = value_copy;
37 }55 }
3856
39 pub fn get(self: *const BufMap, key: []const u8) ?[]const u8 {57 pub fn get(self: BufMap, key: []const u8) ?[]const u8 {
40 const entry = self.hash_map.get(key) orelse return null;58 const entry = self.hash_map.get(key) orelse return null;
41 return entry.value;59 return entry.value;
42 }60 }
...@@ -47,7 +65,7 @@ pub const BufMap = struct {...@@ -47,7 +65,7 @@ pub const BufMap = struct {
47 self.free(entry.value);65 self.free(entry.value);
48 }66 }
4967
50 pub fn count(self: *const BufMap) usize {68 pub fn count(self: BufMap) usize {
51 return self.hash_map.count();69 return self.hash_map.count();
52 }70 }
5371
...@@ -55,11 +73,11 @@ pub const BufMap = struct {...@@ -55,11 +73,11 @@ pub const BufMap = struct {
55 return self.hash_map.iterator();73 return self.hash_map.iterator();
56 }74 }
5775
58 fn free(self: *const BufMap, value: []const u8) void {76 fn free(self: BufMap, value: []const u8) void {
59 self.hash_map.allocator.free(value);77 self.hash_map.allocator.free(value);
60 }78 }
6179
62 fn copy(self: *const BufMap, value: []const u8) ![]const u8 {80 fn copy(self: BufMap, value: []const u8) ![]u8 {
63 return mem.dupe(self.hash_map.allocator, u8, value);81 return mem.dupe(self.hash_map.allocator, u8, value);
64 }82 }
65};83};
std/os/index.zig+13-38
...@@ -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);
707707
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;
713711
714 const key_start = i;712 const key_start = i;
715 var fallocator = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator;
716713
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 }
729718
730 if (ptr[i] == '=') i += 1;719 if (ptr[i] == '=') i += 1;
731720
732 const value_start = i;721 const value_start = i;
733 while (ptr[i] != 0) : (i += 1) {}722 while (ptr[i] != 0) : (i += 1) {}
734723 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 }
745726
746 i += 1; // skip over null byte727 i += 1; // skip over null byte
747728
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 {
795pub const GetEnvVarOwnedError = error{769pub const GetEnvVarOwnedError = error{
796 OutOfMemory,770 OutOfMemory,
797 EnvironmentVariableNotFound,771 EnvironmentVariableNotFound,
798 DanglingSurrogateHalf,
799 ExpectedSecondSurrogateHalf,
800 UnexpectedSecondSurrogateHalf,
801772
802 /// See https://github.com/ziglang/zig/issues/1774773 /// 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 }
835806
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}
848824
849
850/// Caller must free the returned memory.825/// Caller must free the returned memory.
851pub fn getCwdAlloc(allocator: *Allocator) ![]u8 {826pub fn getCwdAlloc(allocator: *Allocator) ![]u8 {
852 var buf: [MAX_PATH_BYTES]u8 = undefined;827 var buf: [MAX_PATH_BYTES]u8 = undefined;