fix a bug in strings.Builder and wrap up vfmt

This commit is contained in:
Alexander Medvednikov 2019-11-11 08:04:37 +03:00
parent 1cda5c1bc8
commit d9b29bfb4e
5 changed files with 21 additions and 9 deletions

View file

@ -13,17 +13,23 @@ pub:
pub fn new_builder(initial_size int) Builder {
return Builder {
buf: make(0, initial_size, sizeof(byte))
buf: make(0, initial_size, 1)
}
}
pub fn (b mut Builder) write(s string) {
b.buf.push_many(s.str, s.len)
//for c in s {
//b.buf << c
//}
//b.buf << []byte(s) // TODO
b.len += s.len
}
pub fn (b mut Builder) writeln(s string) {
//for c in s {
//b.buf << c
//}
b.buf.push_many(s.str, s.len)
//b.buf << []byte(s) // TODO
b.buf << `\n`
@ -34,10 +40,6 @@ pub fn (b Builder) str() string {
return string(b.buf, b.len)
}
pub fn (b mut Builder) cut(n int) {
b.len -= n
}
pub fn (b mut Builder) free() {
//free(b.buf.data)
}

View file

@ -6,11 +6,13 @@ fn test_sb() {
sb.write('!')
sb.write('hello')
assert sb.str() == 'hi!hello'
assert sb.len == 8
sb = strings.new_builder(10)
sb.write('a')
sb.write('b')
println(sb.str())
assert sb.str() == 'ab'
assert sb.len == 2
}
const (
@ -19,8 +21,10 @@ const (
fn test_big_sb() {
mut sb := strings.new_builder(100)
mut sb2 := strings.new_builder(10000)
for i in 0..n {
sb.writeln(i.str())
sb2.write('+')
}
s := sb.str()
lines := s.split_into_lines()
@ -29,6 +33,8 @@ fn test_big_sb() {
assert lines[1] == '1'
assert lines[777] == '777'
assert lines[98765] == '98765'
println(sb2.len)
assert sb2.len == n
}