1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

strings: simplify Builder.drain_builder; add test (#17846)

This commit is contained in:
Delyan Angelov 2023-04-02 00:03:00 +03:00 committed by GitHub
parent 58dd9ee6a2
commit 6aec8244f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -87,7 +87,9 @@ pub fn (mut b Builder) write(data []u8) !int {
// `other`, so that the `other` strings builder is ready to receive new content.
[manualfree]
pub fn (mut b Builder) drain_builder(mut other Builder, other_new_cap int) {
b.write(other) or { panic(err) }
if other.len > 0 {
b << *other
}
unsafe { other.free() }
other = new_builder(other_new_cap)
}

View File

@ -129,3 +129,18 @@ fn test_ensure_cap() {
sb.ensure_cap(-1)
assert sb.cap == 15
}
fn test_drain_builder() {
mut sb := strings.new_builder(0)
mut target_sb := strings.new_builder(0)
assert sb.cap == 0
assert target_sb.cap == 0
sb.write_string('abc')
assert sb.len == 3
target_sb.drain_builder(mut sb, 0)
assert sb.len == 0
assert target_sb.len == 3
assert target_sb.str() == 'abc'
}