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

all: add strings.Builder.write_string and use write_string instead of write (#8892)

This commit is contained in:
zakuro
2021-02-22 20:18:11 +09:00
committed by GitHub
parent 36a6bc270c
commit f54c1a5cc2
34 changed files with 402 additions and 397 deletions

View File

@@ -23,7 +23,7 @@ pub fn (mut b Builder) write_b(data byte) {
b.len++
}
pub fn (mut b Builder) write(s string) {
pub fn (mut b Builder) write_string(s string) {
b.buf.push_many(s.str, s.len)
// b.buf << []byte(s) // TODO
b.len += s.len

View File

@@ -37,9 +37,14 @@ pub fn (mut b Builder) write_b(data byte) {
b.len++
}
[deprecated: 'write(string) will be changed to write([]byte)']
pub fn (mut b Builder) write(s string) {
b.write_string(s)
}
// write appends the string `s` to the buffer
[inline]
pub fn (mut b Builder) write(s string) {
pub fn (mut b Builder) write_string(s string) {
if s == '' {
return
}

View File

@@ -4,17 +4,17 @@ type MyInt = int
fn test_sb() {
mut sb := strings.Builder{}
sb.write('hi')
sb.write('!')
sb.write('hello')
sb.write_string('hi')
sb.write_string('!')
sb.write_string('hello')
assert sb.len == 8
sb_end := sb.str()
assert sb_end == 'hi!hello'
assert sb.len == 0
///
sb = strings.new_builder(10)
sb.write('a')
sb.write('b')
sb.write_string('a')
sb.write_string('b')
assert sb.len == 2
assert sb.str() == 'ab'
// Test interpolation optimization
@@ -28,12 +28,12 @@ fn test_sb() {
assert res.trim_space() == 'x = 10 y = 20'
//
sb = strings.new_builder(10)
sb.write('x = $x y = $y')
sb.write_string('x = $x y = $y')
assert sb.str() == 'x = 10 y = 20'
$if !windows {
// TODO msvc bug
sb = strings.new_builder(10)
sb.write('123456')
sb.write_string('123456')
last_2 := sb.cut_last(2)
assert last_2 == '56'
final_sb := sb.str()
@@ -50,7 +50,7 @@ fn test_big_sb() {
mut sb2 := strings.new_builder(10000)
for i in 0 .. maxn {
sb.writeln(i.str())
sb2.write('+')
sb2.write_string('+')
}
s := sb.str()
lines := s.split_into_lines()
@@ -78,8 +78,8 @@ fn test_byte_write() {
fn test_strings_builder_reuse() {
mut sb := strings.new_builder(256)
sb.write('world')
sb.write_string('world')
assert sb.str() == 'world'
sb.write('hello')
sb.write_string('hello')
assert sb.str() == 'hello'
}