js: fix strings module (#11353)

pull/11355/head
playX 2021-08-31 17:10:19 +03:00 committed by GitHub
parent a208138283
commit f8aaf4bf67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 22 deletions

View File

@ -12,40 +12,49 @@ pub mut:
} }
pub fn new_builder(initial_size int) Builder { pub fn new_builder(initial_size int) Builder {
return Builder{ return Builder{[]byte{cap: initial_size}, 0, initial_size}
buf: make(0, initial_size, sizeof(byte))
initial_size: initial_size
}
} }
pub fn (mut b Builder) write_b(data byte) { pub fn (mut b Builder) write_b(data byte) {
b.buf << data b.buf << data
b.len++ }
pub fn (mut b Builder) write(data []byte) ?int {
if data.len == 0 {
return 0
}
b.buf << data
return data.len
}
pub fn (b &Builder) byte_at(n int) byte {
return b.buf[n]
} }
pub fn (mut b Builder) write_string(s string) { pub fn (mut b Builder) write_string(s string) {
b.buf.push_many(s.str, s.len) if s.len == 0 {
// b.buf << []byte(s) // TODO return
b.len += s.len }
for c in s {
b.buf << c
}
} }
pub fn (mut b Builder) writeln(s string) { pub fn (mut b Builder) writeln(s string) {
b.buf.push_many(s.str, s.len) if s.len > 0 {
// b.buf << []byte(s) // TODO b.write_string(s)
b.buf << `\n` }
b.len += s.len + 1
b.buf << 10
} }
pub fn (b Builder) str() string { pub fn (mut b Builder) str() string {
x := &byte(b.buf.data) b.buf << byte(0)
return unsafe { x.vstring_with_len(b.len) } s := ''
}
pub fn (mut b Builder) cut(n int) { #for (const c of b.val.buf.arr)
b.len -= n #s.str += String.fromCharCode(+c)
}
pub fn (mut b Builder) free() { return s
b.buf = make(0, b.initial_size, 1)
b.len = 0
} }

View File

@ -9,6 +9,7 @@ pub fn repeat(c byte, n int) string {
} }
pub fn repeat_string(s string, n int) string { pub fn repeat_string(s string, n int) string {
return ''
/* /*
// TODO: uncomment this. It is commented for now, so that `v doc strings` works // TODO: uncomment this. It is commented for now, so that `v doc strings` works
res := # s.repeat(n) res := # s.repeat(n)