aboutsummaryrefslogtreecommitdiff
path: root/lib/std/bounded_array.zig
diff options
context:
space:
mode:
authorArnav Singh <me@arnavion.dev>2021-12-15 11:10:34 -0800
committerGitHub <noreply@github.com>2021-12-15 20:10:34 +0100
commit09f70bdd91bf498644f7a42800f3e4e4df7040aa (patch)
tree6baacfaa15f3d330b0c8434d84a86376e1bc9c58 /lib/std/bounded_array.zig
parent87b843ef08426e1b14b2128987dfb52bb7de6788 (diff)
downloadzig-09f70bdd91bf498644f7a42800f3e4e4df7040aa.tar.gz
zig-09f70bdd91bf498644f7a42800f3e4e4df7040aa.zip
std.bounded_array: support inserting a new value at the end (#10340)
Since `BoundedArray.insert` internally reserves space for the element to be inserted, it can support inserting at the position that is the current length of the array. Change the check for the insertion position to allow this.
Diffstat (limited to 'lib/std/bounded_array.zig')
-rw-r--r--lib/std/bounded_array.zig6
1 files changed, 5 insertions, 1 deletions
diff --git a/lib/std/bounded_array.zig b/lib/std/bounded_array.zig
index 51a4488387..bc5f196087 100644
--- a/lib/std/bounded_array.zig
+++ b/lib/std/bounded_array.zig
@@ -121,7 +121,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
/// Insert `item` at index `i` by moving `slice[n .. slice.len]` to make room.
/// This operation is O(N).
pub fn insert(self: *Self, i: usize, item: T) !void {
- if (i >= self.len) {
+ if (i > self.len) {
return error.IndexOutOfBounds;
}
_ = try self.addOne();
@@ -289,6 +289,10 @@ test "BoundedArray" {
try testing.expectEqual(a.get(9), 3);
try testing.expectEqual(a.get(10), 4);
+ try a.insert(11, 0xbb);
+ try testing.expectEqual(a.len, 12);
+ try testing.expectEqual(a.pop(), 0xbb);
+
try a.appendSlice(&x);
try testing.expectEqual(a.len, 11 + x.len);