# Adds initCapacity\(\) to buffer and arraylist · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/17abb7ef7f75705c1195022076e95cdde6ab2243)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 17abb7ef7f75705c1195022076e95cdde6ab2243

Requested commit: 17abb7ef7f75705c1195022076e95cdde6ab2243

Commit: 17abb7ef7f75705c1195022076e95cdde6ab2243

Tree: e2ac48ed25c54c6e3b13eaddb1876331cd020eec

Author: MCRusher

Committer: GitHub

## Message

```
Adds initCapacity() to buffer and arraylist

array_list.zig:
- adds ArrayList.initCapacity(*Allocator,usize) to allow preallocation of a block at initialization to reduce future allocations.
- adds a test for ArrayList.initCapacity() that ensures ArrayList.len() is unchanged and that at least the requested amount is allocated for.
buffer.zig:
- adds Buffer.initCapacity(*Allocator,usize), based off of ArrayList.initCapacity(), to preallocate a buffer before use.
note: contrary to Buffer.initSize(0) and then Buffer.list.ensureCapacity(200) (the presumed current method), this only allocates once instead of twice.
- adds Buffer.capacity to check usable allocated space, not including the null byte.
note: returns 0 when Buffer has only a null byte or when initNull() was used before without resize()/replaceContents().
- adds a test "Buffer.initCapacity" which ensures that Buffer.append()'s with [added size <= Buffer.capacity()-Buffer.len()] do not cause a reallocation to occur.
- adds a test "Buffer.initSize" which ensures that Buffer.initSize() behaves as expected, also combined with Buffer.append().
- adds a doc comment to Buffer.initSize() that makes its function and distinction from Buffer.initCapacity() clearer.
```

## Parents

- [d49e0a7b90dabbf486902fa9d4a4306aede55087](https://git.cafe/gitcafe/zig/commit/d49e0a7b90dabbf486902fa9d4a4306aede55087?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/17abb7ef7f75705c1195022076e95cdde6ab2243?format=markdown)
