authorgravatar for 66840021+Mcrtin@users.noreply.github.comMartin <66840021+Mcrtin@users.noreply.github.com> 2026-10-17 10:41:53+02:00
committergravatar for 66840021+Mcrtin@users.noreply.github.comMartin <66840021+Mcrtin@users.noreply.github.com> 2026-10-17 10:41:53+02:00
logf79dad493810e8c9338ccf3343468c001329e30f
tree1c45ee5e5554bf0a0d017c4903d4fdf7837f5734
parentf471212724bb917c57fc89a1cf41b5d365aa3a2d

std.json: update usages of `array_list.Managed` to `ArrayList`


4 files changed, 48 insertions(+), 54 deletions(-)

lib/std/json/Scanner.zig+32-26
...@@ -135,11 +135,11 @@ pub fn nextAllocMax(self: *@This(), allocator: Allocator, when: AllocWhen, max_v...@@ -135,11 +135,11 @@ pub fn nextAllocMax(self: *@This(), allocator: Allocator, when: AllocWhen, max_v
135 };135 };
136 switch (token_type) {136 switch (token_type) {
137 .number, .string => {137 .number, .string => {
138 var value_list = std.array_list.Managed(u8).init(allocator);138 var value_list: std.ArrayList(u8) = .empty;
139 errdefer {139 errdefer {
140 value_list.deinit();140 value_list.deinit(allocator);
141 }141 }
142 if (self.allocNextIntoArrayListMax(&value_list, when, max_value_len) catch |e| switch (e) {142 if (self.allocNextIntoArrayListMax(allocator, &value_list, when, max_value_len) catch |e| switch (e) {
143 error.BufferUnderrun => unreachable,143 error.BufferUnderrun => unreachable,
144 else => |err| return err,144 else => |err| return err,
145 }) |slice| {145 }) |slice| {
...@@ -149,9 +149,9 @@ pub fn nextAllocMax(self: *@This(), allocator: Allocator, when: AllocWhen, max_v...@@ -149,9 +149,9 @@ pub fn nextAllocMax(self: *@This(), allocator: Allocator, when: AllocWhen, max_v
149 Token{ .string = slice };149 Token{ .string = slice };
150 } else {150 } else {
151 return if (token_type == .number)151 return if (token_type == .number)
152 Token{ .allocated_number = try value_list.toOwnedSlice() }152 Token{ .allocated_number = try value_list.toOwnedSlice(allocator) }
153 else153 else
154 Token{ .allocated_string = try value_list.toOwnedSlice() };154 Token{ .allocated_string = try value_list.toOwnedSlice(allocator) };
155 }155 }
156 },156 },
157157
...@@ -172,8 +172,8 @@ pub fn nextAllocMax(self: *@This(), allocator: Allocator, when: AllocWhen, max_v...@@ -172,8 +172,8 @@ pub fn nextAllocMax(self: *@This(), allocator: Allocator, when: AllocWhen, max_v
172}172}
173173
174/// Equivalent to `allocNextIntoArrayListMax(value_list, when, default_max_value_len);`174/// Equivalent to `allocNextIntoArrayListMax(value_list, when, default_max_value_len);`
175pub fn allocNextIntoArrayList(self: *@This(), value_list: *std.array_list.Managed(u8), when: AllocWhen) AllocIntoArrayListError!?[]const u8 {175pub fn allocNextIntoArrayList(self: *@This(), allocator: Allocator, value_list: *std.ArrayList(u8), when: AllocWhen) AllocIntoArrayListError!?[]const u8 {
176 return self.allocNextIntoArrayListMax(value_list, when, default_max_value_len);176 return self.allocNextIntoArrayListMax(allocator, value_list, when, default_max_value_len);
177}177}
178/// The next token type must be either `.number` or `.string`. See `peekNextTokenType()`.178/// The next token type must be either `.number` or `.string`. See `peekNextTokenType()`.
179/// When allocation is not necessary with `.alloc_if_needed`,179/// When allocation is not necessary with `.alloc_if_needed`,
...@@ -185,25 +185,25 @@ pub fn allocNextIntoArrayList(self: *@This(), value_list: *std.array_list.Manage...@@ -185,25 +185,25 @@ pub fn allocNextIntoArrayList(self: *@This(), value_list: *std.array_list.Manage
185/// can be resumed by passing the same array list in again.185/// can be resumed by passing the same array list in again.
186/// This method does not indicate whether the token content being returned is for a `.number` or `.string` token type;186/// This method does not indicate whether the token content being returned is for a `.number` or `.string` token type;
187/// the caller of this method is expected to know which type of token is being processed.187/// the caller of this method is expected to know which type of token is being processed.
188pub fn allocNextIntoArrayListMax(self: *@This(), value_list: *std.array_list.Managed(u8), when: AllocWhen, max_value_len: usize) AllocIntoArrayListError!?[]const u8 {188pub fn allocNextIntoArrayListMax(self: *@This(), allocator: Allocator, value_list: *std.ArrayList(u8), when: AllocWhen, max_value_len: usize) AllocIntoArrayListError!?[]const u8 {
189 while (true) {189 while (true) {
190 const token = try self.next();190 const token = try self.next();
191 switch (token) {191 switch (token) {
192 // Accumulate partial values.192 // Accumulate partial values.
193 .partial_number, .partial_string => |slice| {193 .partial_number, .partial_string => |slice| {
194 try appendSlice(value_list, slice, max_value_len);194 try appendSlice(allocator, value_list, slice, max_value_len);
195 },195 },
196 .partial_string_escaped_1 => |buf| {196 .partial_string_escaped_1 => |buf| {
197 try appendSlice(value_list, buf[0..], max_value_len);197 try appendSlice(allocator, value_list, buf[0..], max_value_len);
198 },198 },
199 .partial_string_escaped_2 => |buf| {199 .partial_string_escaped_2 => |buf| {
200 try appendSlice(value_list, buf[0..], max_value_len);200 try appendSlice(allocator, value_list, buf[0..], max_value_len);
201 },201 },
202 .partial_string_escaped_3 => |buf| {202 .partial_string_escaped_3 => |buf| {
203 try appendSlice(value_list, buf[0..], max_value_len);203 try appendSlice(allocator, value_list, buf[0..], max_value_len);
204 },204 },
205 .partial_string_escaped_4 => |buf| {205 .partial_string_escaped_4 => |buf| {
206 try appendSlice(value_list, buf[0..], max_value_len);206 try appendSlice(allocator, value_list, buf[0..], max_value_len);
207 },207 },
208208
209 // Return complete values.209 // Return complete values.
...@@ -212,7 +212,7 @@ pub fn allocNextIntoArrayListMax(self: *@This(), value_list: *std.array_list.Man...@@ -212,7 +212,7 @@ pub fn allocNextIntoArrayListMax(self: *@This(), value_list: *std.array_list.Man
212 // No alloc necessary.212 // No alloc necessary.
213 return slice;213 return slice;
214 }214 }
215 try appendSlice(value_list, slice, max_value_len);215 try appendSlice(allocator, value_list, slice, max_value_len);
216 // The token is complete.216 // The token is complete.
217 return null;217 return null;
218 },218 },
...@@ -221,7 +221,7 @@ pub fn allocNextIntoArrayListMax(self: *@This(), value_list: *std.array_list.Man...@@ -221,7 +221,7 @@ pub fn allocNextIntoArrayListMax(self: *@This(), value_list: *std.array_list.Man
221 // No alloc necessary.221 // No alloc necessary.
222 return slice;222 return slice;
223 }223 }
224 try appendSlice(value_list, slice, max_value_len);224 try appendSlice(allocator, value_list, slice, max_value_len);
225 // The token is complete.225 // The token is complete.
226 return null;226 return null;
227 },227 },
...@@ -1607,20 +1607,20 @@ pub const Reader = struct {...@@ -1607,20 +1607,20 @@ pub const Reader = struct {
1607 const token_type = try self.peekNextTokenType();1607 const token_type = try self.peekNextTokenType();
1608 switch (token_type) {1608 switch (token_type) {
1609 .number, .string => {1609 .number, .string => {
1610 var value_list = std.array_list.Managed(u8).init(allocator);1610 var value_list: std.ArrayList(u8) = .empty;
1611 errdefer {1611 errdefer {
1612 value_list.deinit();1612 value_list.deinit(allocator);
1613 }1613 }
1614 if (try self.allocNextIntoArrayListMax(&value_list, when, max_value_len)) |slice| {1614 if (try self.allocNextIntoArrayListMax(allocator, &value_list, when, max_value_len)) |slice| {
1615 return if (token_type == .number)1615 return if (token_type == .number)
1616 Token{ .number = slice }1616 Token{ .number = slice }
1617 else1617 else
1618 Token{ .string = slice };1618 Token{ .string = slice };
1619 } else {1619 } else {
1620 return if (token_type == .number)1620 return if (token_type == .number)
1621 Token{ .allocated_number = try value_list.toOwnedSlice() }1621 Token{ .allocated_number = try value_list.toOwnedSlice(allocator) }
1622 else1622 else
1623 Token{ .allocated_string = try value_list.toOwnedSlice() };1623 Token{ .allocated_string = try value_list.toOwnedSlice(allocator) };
1624 }1624 }
1625 },1625 },
16261626
...@@ -1638,13 +1638,19 @@ pub const Reader = struct {...@@ -1638,13 +1638,19 @@ pub const Reader = struct {
1638 }1638 }
16391639
1640 /// Equivalent to `allocNextIntoArrayListMax(value_list, when, default_max_value_len);`1640 /// Equivalent to `allocNextIntoArrayListMax(value_list, when, default_max_value_len);`
1641 pub fn allocNextIntoArrayList(self: *@This(), value_list: *std.array_list.Managed(u8), when: AllocWhen) Reader.AllocError!?[]const u8 {1641 pub fn allocNextIntoArrayList(self: *@This(), allocator: Allocator, value_list: *std.ArrayList(u8), when: AllocWhen) Reader.AllocError!?[]const u8 {
1642 return self.allocNextIntoArrayListMax(value_list, when, default_max_value_len);1642 return self.allocNextIntoArrayListMax(allocator, value_list, when, default_max_value_len);
1643 }1643 }
1644 /// Calls `std.json.Scanner.allocNextIntoArrayListMax` and handles `error.BufferUnderrun`.1644 /// Calls `std.json.Scanner.allocNextIntoArrayListMax` and handles `error.BufferUnderrun`.
1645 pub fn allocNextIntoArrayListMax(self: *@This(), value_list: *std.array_list.Managed(u8), when: AllocWhen, max_value_len: usize) Reader.AllocError!?[]const u8 {1645 pub fn allocNextIntoArrayListMax(
1646 self: *@This(),
1647 allocator: Allocator,
1648 value_list: *std.ArrayList(u8),
1649 when: AllocWhen,
1650 max_value_len: usize,
1651 ) Reader.AllocError!?[]const u8 {
1646 while (true) {1652 while (true) {
1647 return self.scanner.allocNextIntoArrayListMax(value_list, when, max_value_len) catch |err| switch (err) {1653 return self.scanner.allocNextIntoArrayListMax(allocator, value_list, when, max_value_len) catch |err| switch (err) {
1648 error.BufferUnderrun => {1654 error.BufferUnderrun => {
1649 try self.refillBuffer();1655 try self.refillBuffer();
1650 continue;1656 continue;
...@@ -1745,10 +1751,10 @@ pub const Reader = struct {...@@ -1745,10 +1751,10 @@ pub const Reader = struct {
1745const OBJECT_MODE = 0;1751const OBJECT_MODE = 0;
1746const ARRAY_MODE = 1;1752const ARRAY_MODE = 1;
17471753
1748fn appendSlice(list: *std.array_list.Managed(u8), buf: []const u8, max_value_len: usize) !void {1754fn appendSlice(allocator: Allocator, list: *std.ArrayList(u8), buf: []const u8, max_value_len: usize) !void {
1749 const new_len = std.math.add(usize, list.items.len, buf.len) catch return error.ValueTooLong;1755 const new_len = std.math.add(usize, list.items.len, buf.len) catch return error.ValueTooLong;
1750 if (new_len > max_value_len) return error.ValueTooLong;1756 if (new_len > max_value_len) return error.ValueTooLong;
1751 try list.appendSlice(buf);1757 try list.appendSlice(allocator, buf);
1752}1758}
17531759
1754/// For the slice you get from a `Token.number` or `Token.allocated_number`,1760/// For the slice you get from a `Token.number` or `Token.allocated_number`,
lib/std/json/dynamic.zig+7-7
...@@ -11,7 +11,7 @@ const ParseError = @import("./static.zig").ParseError;...@@ -11,7 +11,7 @@ const ParseError = @import("./static.zig").ParseError;
11const isNumberFormattedLikeAnInteger = @import("Scanner.zig").isNumberFormattedLikeAnInteger;11const isNumberFormattedLikeAnInteger = @import("Scanner.zig").isNumberFormattedLikeAnInteger;
1212
13pub const ObjectMap = StringArrayHashMap(Value);13pub const ObjectMap = StringArrayHashMap(Value);
14pub const Array = std.array_list.Managed(Value);14pub const Array = std.ArrayList(Value);
1515
16/// Represents any JSON value, potentially containing other JSON values.16/// Represents any JSON value, potentially containing other JSON values.
17/// A .float value may be an approximation of the original value.17/// A .float value may be an approximation of the original value.
...@@ -76,8 +76,8 @@ pub const Value = union(enum) {...@@ -76,8 +76,8 @@ pub const Value = union(enum) {
76 pub fn jsonParse(allocator: Allocator, source: anytype, options: ParseOptions) ParseError(@TypeOf(source.*))!@This() {76 pub fn jsonParse(allocator: Allocator, source: anytype, options: ParseOptions) ParseError(@TypeOf(source.*))!@This() {
77 // The grammar of the stack is:77 // The grammar of the stack is:
78 // (.array | .object .string)*78 // (.array | .object .string)*
79 var stack = Array.init(allocator);79 var stack: Array = .empty;
80 defer stack.deinit();80 defer stack.deinit(allocator);
8181
82 while (true) {82 while (true) {
83 // Assert the stack grammar at the top of the stack.83 // Assert the stack grammar at the top of the stack.
...@@ -105,7 +105,7 @@ pub const Value = union(enum) {...@@ -105,7 +105,7 @@ pub const Value = union(enum) {
105 switch (try source.nextAllocMax(allocator, .alloc_always, options.max_value_len.?)) {105 switch (try source.nextAllocMax(allocator, .alloc_always, options.max_value_len.?)) {
106 .object_end => return try handleCompleteValue(&stack, allocator, source, Value{ .object = .empty }, options) orelse continue,106 .object_end => return try handleCompleteValue(&stack, allocator, source, Value{ .object = .empty }, options) orelse continue,
107 .allocated_string => |key| {107 .allocated_string => |key| {
108 try stack.appendSlice(&[_]Value{108 try stack.appendSlice(allocator, &[_]Value{
109 Value{ .object = .empty },109 Value{ .object = .empty },
110 Value{ .string = key },110 Value{ .string = key },
111 });111 });
...@@ -114,7 +114,7 @@ pub const Value = union(enum) {...@@ -114,7 +114,7 @@ pub const Value = union(enum) {
114 }114 }
115 },115 },
116 .array_begin => {116 .array_begin => {
117 try stack.append(Value{ .array = Array.init(allocator) });117 try stack.append(allocator, Value{ .array = .empty });
118 },118 },
119 .array_end => return try handleCompleteValue(&stack, allocator, source, stack.pop().?, options) orelse continue,119 .array_end => return try handleCompleteValue(&stack, allocator, source, stack.pop().?, options) orelse continue,
120120
...@@ -170,7 +170,7 @@ fn handleCompleteValue(stack: *Array, allocator: Allocator, source: anytype, val...@@ -170,7 +170,7 @@ fn handleCompleteValue(stack: *Array, allocator: Allocator, source: anytype, val
170 },170 },
171 .allocated_string => |next_key| {171 .allocated_string => |next_key| {
172 // We've got another key.172 // We've got another key.
173 try stack.append(Value{ .string = next_key });173 try stack.append(allocator, Value{ .string = next_key });
174 // stack: [..., .object, .string]174 // stack: [..., .object, .string]
175 return null;175 return null;
176 },176 },
...@@ -179,7 +179,7 @@ fn handleCompleteValue(stack: *Array, allocator: Allocator, source: anytype, val...@@ -179,7 +179,7 @@ fn handleCompleteValue(stack: *Array, allocator: Allocator, source: anytype, val
179 },179 },
180 .array => |*array| {180 .array => |*array| {
181 // stack: [..., .array]181 // stack: [..., .array]
182 try array.append(value);182 try array.append(allocator, value);
183 return null;183 return null;
184 },184 },
185 else => unreachable,185 else => unreachable,
lib/std/json/dynamic_test.zig+1-13
...@@ -122,18 +122,6 @@ test "parsing empty string gives appropriate error" {...@@ -122,18 +122,6 @@ test "parsing empty string gives appropriate error" {
122 try testing.expectError(error.UnexpectedEndOfInput, testParse(arena_allocator.allocator(), ""));122 try testing.expectError(error.UnexpectedEndOfInput, testParse(arena_allocator.allocator(), ""));
123}123}
124124
125test "Value.array allocator should still be usable after parsing" {
126 var parsed = try parseFromSlice(Value, std.testing.allocator, "[]", .{});
127 defer parsed.deinit();
128
129 // Allocation should succeed
130 var i: usize = 0;
131 while (i < 100) : (i += 1) {
132 try parsed.value.array.append(Value{ .integer = 100 });
133 }
134 try testing.expectEqual(parsed.value.array.items.len, 100);
135}
136
137test "integer after float has proper type" {125test "integer after float has proper type" {
138 var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);126 var arena_allocator = std.heap.ArenaAllocator.init(std.testing.allocator);
139 defer arena_allocator.deinit();127 defer arena_allocator.deinit();
...@@ -236,7 +224,7 @@ test "Value.jsonStringify" {...@@ -236,7 +224,7 @@ test "Value.jsonStringify" {
236 .{ .number_string = "43" },224 .{ .number_string = "43" },
237 .{ .float = 42 },225 .{ .float = 42 },
238 .{ .string = "weeee" },226 .{ .string = "weeee" },
239 .{ .array = Array.fromOwnedSlice(undefined, &vals) },227 .{ .array = .fromOwnedSlice(&vals) },
240 .{ .object = obj },228 .{ .object = obj },
241 };229 };
242 var buffer: [0x1000]u8 = undefined;230 var buffer: [0x1000]u8 = undefined;
lib/std/json/static.zig+8-8
...@@ -2,7 +2,7 @@ const std = @import("std");...@@ -2,7 +2,7 @@ const std = @import("std");
2const assert = std.debug.assert;2const assert = std.debug.assert;
3const Allocator = std.mem.Allocator;3const Allocator = std.mem.Allocator;
4const ArenaAllocator = std.heap.ArenaAllocator;4const ArenaAllocator = std.heap.ArenaAllocator;
5const ArrayList = std.array_list.Managed;5const ArrayList = std.ArrayList;
66
7const Scanner = @import("Scanner.zig");7const Scanner = @import("Scanner.zig");
8const Token = Scanner.Token;8const Token = Scanner.Token;
...@@ -469,7 +469,7 @@ pub fn innerParse(...@@ -469,7 +469,7 @@ pub fn innerParse(
469 _ = try source.next();469 _ = try source.next();
470470
471 // Typical array.471 // Typical array.
472 var arraylist = ArrayList(ptrInfo.child).init(allocator);472 var arraylist: ArrayList(ptrInfo.child) = .empty;
473 while (true) {473 while (true) {
474 switch (try source.peekNextTokenType()) {474 switch (try source.peekNextTokenType()) {
475 .array_end => {475 .array_end => {
...@@ -479,15 +479,15 @@ pub fn innerParse(...@@ -479,15 +479,15 @@ pub fn innerParse(
479 else => {},479 else => {},
480 }480 }
481481
482 try arraylist.ensureUnusedCapacity(1);482 try arraylist.ensureUnusedCapacity(allocator, 1);
483 arraylist.appendAssumeCapacity(try innerParse(ptrInfo.child, allocator, source, options));483 arraylist.appendAssumeCapacity(try innerParse(ptrInfo.child, allocator, source, options));
484 }484 }
485485
486 if (ptrInfo.sentinel()) |s| {486 if (ptrInfo.sentinel()) |s| {
487 return try arraylist.toOwnedSliceSentinel(s);487 return try arraylist.toOwnedSliceSentinel(allocator, s);
488 }488 }
489489
490 return try arraylist.toOwnedSlice();490 return try arraylist.toOwnedSlice(allocator);
491 },491 },
492 .string => {492 .string => {
493 if (ptrInfo.child != u8) return error.UnexpectedToken;493 if (ptrInfo.child != u8) return error.UnexpectedToken;
...@@ -495,9 +495,9 @@ pub fn innerParse(...@@ -495,9 +495,9 @@ pub fn innerParse(
495 // Dynamic length string.495 // Dynamic length string.
496 if (ptrInfo.sentinel()) |s| {496 if (ptrInfo.sentinel()) |s| {
497 // Use our own array list so we can append the sentinel.497 // Use our own array list so we can append the sentinel.
498 var value_list = ArrayList(u8).init(allocator);498 var value_list: ArrayList(u8) = .empty;
499 _ = try source.allocNextIntoArrayListMax(&value_list, .alloc_always, options.max_value_len.?);499 _ = try source.allocNextIntoArrayListMax(allocator, &value_list, .alloc_always, options.max_value_len.?);
500 return try value_list.toOwnedSliceSentinel(s);500 return try value_list.toOwnedSliceSentinel(allocator, s);
501 }501 }
502 if (ptrInfo.attrs.@"const") {502 if (ptrInfo.attrs.@"const") {
503 switch (try source.nextAllocMax(allocator, options.allocate.?, options.max_value_len.?)) {503 switch (try source.nextAllocMax(allocator, options.allocate.?, options.max_value_len.?)) {