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:
parent
58dd9ee6a2
commit
6aec8244f0
@ -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)
|
||||
}
|
||||
|
@ -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'
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user