strings: add `Builder.grow()` method

Signed-off-by: Hunam <hunam@disroot.org>
pull/13755/head
Hunam 2022-03-16 19:22:13 +01:00
parent 7f62346213
commit c4dd6c8d42
No known key found for this signature in database
GPG Key ID: 1B5A281D937261EA
2 changed files with 35 additions and 0 deletions

View File

@ -176,6 +176,30 @@ pub fn (mut b Builder) str() string {
return s
}
// grow ensures that the buffer has enough space for at least `n` bytes by growing the buffer if necessary
pub fn (mut b Builder) grow(n int) {
// code adapted from vlib/builtin/array.v
if n < 0 || b.cap - b.len >= n {
return
}
cap := 2 * b.cap + n
new_size := cap * b.element_size
new_data := vcalloc(new_size)
if b.data != voidptr(0) {
unsafe { vmemcpy(new_data, b.data, b.len * b.element_size) }
// TODO: the old data may be leaked when no GC is used (ref-counting?)
if b.flags.has(.noslices) {
unsafe { free(b.data) }
}
}
unsafe {
b.data = new_data
b.offset = 0
b.cap = cap
}
}
// free is for manually freeing the contents of the buffer
[unsafe]
pub fn (mut b Builder) free() {

View File

@ -112,3 +112,14 @@ fn test_write_runes() {
x := sb.str()
assert x == 'hello world'
}
fn test_grow() {
mut sb := strings.new_builder(0)
assert sb.cap == 0
sb.grow(10)
assert sb.cap == 10
sb.grow(10)
assert sb.cap == 10
sb.grow(15)
assert sb.cap == 35
}