From f8aaf4bf67b41b95ccaab7d33ddc98025d06e8e7 Mon Sep 17 00:00:00 2001 From: playX Date: Tue, 31 Aug 2021 17:10:19 +0300 Subject: [PATCH] js: fix strings module (#11353) --- vlib/strings/{builder.v => builder.c.v} | 0 vlib/strings/builder.js.v | 53 +++++++++++++++---------- vlib/strings/strings.js.v | 1 + 3 files changed, 32 insertions(+), 22 deletions(-) rename vlib/strings/{builder.v => builder.c.v} (100%) diff --git a/vlib/strings/builder.v b/vlib/strings/builder.c.v similarity index 100% rename from vlib/strings/builder.v rename to vlib/strings/builder.c.v diff --git a/vlib/strings/builder.js.v b/vlib/strings/builder.js.v index e445804f75..e5e11991fb 100644 --- a/vlib/strings/builder.js.v +++ b/vlib/strings/builder.js.v @@ -12,40 +12,49 @@ pub mut: } pub fn new_builder(initial_size int) Builder { - return Builder{ - buf: make(0, initial_size, sizeof(byte)) - initial_size: initial_size - } + return Builder{[]byte{cap: initial_size}, 0, initial_size} } pub fn (mut b Builder) write_b(data byte) { 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) { - b.buf.push_many(s.str, s.len) - // b.buf << []byte(s) // TODO - b.len += s.len + if s.len == 0 { + return + } + + for c in s { + b.buf << c + } } pub fn (mut b Builder) writeln(s string) { - b.buf.push_many(s.str, s.len) - // b.buf << []byte(s) // TODO - b.buf << `\n` - b.len += s.len + 1 + if s.len > 0 { + b.write_string(s) + } + + b.buf << 10 } -pub fn (b Builder) str() string { - x := &byte(b.buf.data) - return unsafe { x.vstring_with_len(b.len) } -} +pub fn (mut b Builder) str() string { + b.buf << byte(0) + s := '' -pub fn (mut b Builder) cut(n int) { - b.len -= n -} + #for (const c of b.val.buf.arr) + #s.str += String.fromCharCode(+c) -pub fn (mut b Builder) free() { - b.buf = make(0, b.initial_size, 1) - b.len = 0 + return s } diff --git a/vlib/strings/strings.js.v b/vlib/strings/strings.js.v index 2681b949f0..5bdff8cbba 100644 --- a/vlib/strings/strings.js.v +++ b/vlib/strings/strings.js.v @@ -9,6 +9,7 @@ pub fn repeat(c byte, 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 res := # s.repeat(n)