Commit c50ba2d101

Erik Arvstedt <erik.arvstedt@gmail.com>
2024-01-19 00:25:44
std.ArrayList.replaceRange: remove unneded overflow checks
The code asserted that the range to be replaced is within bounds of `self.items`. This is now reflected in the doc comment. The old, wrong doc comment was copied from the `insert*` fns. With this assertion holding true, `start + len` is always within the address space and `start + new_items.len` is, at this point, always strictly within bounds of `self.items`.
1 parent 0bb6967
Changed files (1)
lib/std/array_list.zig
@@ -242,9 +242,9 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
         /// Grows list if `len < new_items.len`.
         /// Shrinks list if `len > new_items.len`.
         /// Invalidates element pointers if this ArrayList is resized.
-        /// Asserts that the start index is in bounds or equal to the length.
+        /// Asserts that the range is in bounds.
         pub fn replaceRange(self: *Self, start: usize, len: usize, new_items: []const T) Allocator.Error!void {
-            const after_range = try addOrOom(start, len);
+            const after_range = start + len;
             const range = self.items[start..after_range];
 
             if (range.len == new_items.len)
@@ -257,7 +257,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
                 try self.insertSlice(after_range, rest);
             } else {
                 @memcpy(range[0..new_items.len], new_items);
-                const after_subrange = try addOrOom(start, new_items.len);
+                const after_subrange = start + new_items.len;
 
                 for (self.items[after_range..], 0..) |item, i| {
                     self.items[after_subrange..][i] = item;