mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
io: add a string_reader
submodule (#20893)
This commit is contained in:
parent
a9c10428d2
commit
d8c4a84f71
6 changed files with 454 additions and 11 deletions
|
@ -154,8 +154,9 @@ pub fn (mut b Builder) go_back(n int) {
|
|||
b.trim(b.len - n)
|
||||
}
|
||||
|
||||
// spart returns a part of the buffer as a string
|
||||
@[inline]
|
||||
fn (b &Builder) spart(start_pos int, n int) string {
|
||||
pub fn (b &Builder) spart(start_pos int, n int) string {
|
||||
unsafe {
|
||||
mut x := malloc_noscan(n + 1)
|
||||
vmemcpy(x, &u8(b.data) + start_pos, n)
|
||||
|
@ -253,6 +254,20 @@ pub fn (mut b Builder) ensure_cap(n int) {
|
|||
}
|
||||
}
|
||||
|
||||
// grow_len grows the length of the buffer by `n` bytes if necessary
|
||||
@[unsafe]
|
||||
pub fn (mut b Builder) grow_len(n int) {
|
||||
if n <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
new_len := b.len + n
|
||||
b.ensure_cap(new_len)
|
||||
unsafe {
|
||||
b.len = new_len
|
||||
}
|
||||
}
|
||||
|
||||
// free frees the memory block, used for the buffer.
|
||||
// Note: do not use the builder, after a call to free().
|
||||
@[unsafe]
|
||||
|
|
|
@ -166,3 +166,28 @@ fn test_write_decimal() {
|
|||
assert sb_i64_str(9223372036854775807) == '9223372036854775807'
|
||||
assert sb_i64_str(-9223372036854775807) == '-9223372036854775807'
|
||||
}
|
||||
|
||||
fn test_grow_len() {
|
||||
mut sb := strings.new_builder(10)
|
||||
assert sb.len == 0
|
||||
assert sb.cap == 10
|
||||
|
||||
sb.write_string('0123456789')
|
||||
assert sb.len == 10
|
||||
|
||||
unsafe { sb.grow_len(-5) }
|
||||
assert sb.len == 10
|
||||
assert sb.cap == 10
|
||||
|
||||
unsafe { sb.grow_len(10) }
|
||||
assert sb.len == 20
|
||||
assert sb.cap == 20
|
||||
|
||||
unsafe { sb.ensure_cap(35) }
|
||||
assert sb.len == 20
|
||||
assert sb.cap == 35
|
||||
|
||||
unsafe { sb.grow_len(5) }
|
||||
assert sb.len == 25
|
||||
assert sb.cap == 35
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue