Skip to content

Commit eac1e25

Browse files
authored
strings: simplify Builder (#10263)
1 parent 8990114 commit eac1e25

File tree

14 files changed

+65
-76
lines changed

14 files changed

+65
-76
lines changed

cmd/tools/modules/vgit/vgit.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub mut:
102102
vexename string // v or v.exe
103103
vexepath string // the full absolute path to the prepared v/v.exe
104104
vvlocation string // v.v or compiler/ or cmd/v, depending on v version
105+
make_fresh_tcc bool // whether to do 'make fresh_tcc' before compiling an old V.
105106
}
106107

107108
pub fn (mut vgit_context VGitContext) compile_oldv_if_needed() {
@@ -141,6 +142,9 @@ pub fn (mut vgit_context VGitContext) compile_oldv_if_needed() {
141142
}
142143
// Recompilation is needed. Just to be sure, clean up everything first.
143144
scripting.run('git clean -xf')
145+
if vgit_context.make_fresh_tcc {
146+
scripting.run('make fresh_tcc')
147+
}
144148
scripting.run(command_for_building_v_from_c_source)
145149
build_cmd := command_for_selfbuilding.replace('{SOURCE}', vgit_context.vvlocation)
146150
scripting.run(build_cmd)

cmd/tools/oldv.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ mut:
3939
cc string = 'cc' // the C compiler to use for bootstrapping.
4040
cleanup bool // should the tool run a cleanup first
4141
use_cache bool // use local cached copies for --vrepo and --vcrepo in
42+
fresh_tcc bool // do use `make fresh_tcc`
4243
}
4344

4445
fn (mut c Context) compile_oldv_if_needed() {
@@ -50,6 +51,7 @@ fn (mut c Context) compile_oldv_if_needed() {
5051
commit_v: c.commit_v
5152
path_v: c.path_v
5253
path_vc: c.path_vc
54+
make_fresh_tcc: c.fresh_tcc
5355
}
5456
c.vgcontext.compile_oldv_if_needed()
5557
c.commit_v_hash = c.vgcontext.commit_v__hash
@@ -125,6 +127,7 @@ fn main() {
125127
}
126128
////
127129
context.cleanup = fp.bool('clean', 0, false, 'Clean before running (slower).')
130+
context.fresh_tcc = fp.bool('fresh_tcc', 0, true, 'Do `make fresh_tcc` when preparing a V compiler.')
128131
context.cmd_to_run = fp.string('command', `c`, '', 'Command to run in the old V repo.\n')
129132
commits := vgit.add_common_tool_options(mut context.vgo, mut fp)
130133
if should_sync {

examples/web_crawler/web_crawler.v

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ import net.http
22
import net.html
33

44
fn main() {
5-
/*
6-
user_agent = 'v.http'
7-
resp := http.get('https://tuicool.com') or {
8-
println('failed to fetch data from the server')
9-
return
10-
}
11-
*/
125
// http.fetch() sends an HTTP request to the URL with the given method and configurations.
136
config := http.FetchConfig{
147
user_agent: 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0'

vlib/net/html/parser.v

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ mut:
1313
is_attribute bool
1414
opened_code_type string
1515
line_count int
16-
lexeme_builder strings.Builder = strings.Builder{}
16+
lexeme_builder strings.Builder = strings.new_builder(100)
1717
code_tags map[string]bool = map{
18-
'script': true
19-
'style': true
20-
}
18+
'script': true
19+
'style': true
20+
}
2121
}
2222

2323
// Parser is responsible for reading the HTML strings and converting them into a `DocumentObjectModel`.

vlib/strings/builder.v

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,76 +7,59 @@ module strings
77
// dynamically growing buffer, then use the resulting large string. Using
88
// a string builder is much better for performance/memory usage than doing
99
// constantly string concatenation.
10-
pub struct Builder {
11-
pub mut:
12-
buf []byte
13-
len int
14-
initial_size int = 1
15-
}
10+
pub type Builder = []byte
1611

1712
// new_builder returns a new string builder, with an initial capacity of `initial_size`
1813
pub fn new_builder(initial_size int) Builder {
19-
return Builder{
20-
// buf: make(0, initial_size)
21-
buf: []byte{cap: initial_size}
22-
len: 0
23-
initial_size: initial_size
24-
}
25-
}
26-
27-
// write_bytes appends `bytes` to the accumulated buffer
28-
[deprecated: 'use Builder.write_ptr() instead']
29-
[deprecated_after: '2021-04-18']
30-
[unsafe]
31-
pub fn (mut b Builder) write_bytes(bytes &byte, len int) {
32-
unsafe { b.write_ptr(bytes, len) }
14+
return Builder([]byte{cap: initial_size})
3315
}
3416

3517
// write_ptr writes `len` bytes provided byteptr to the accumulated buffer
3618
[unsafe]
3719
pub fn (mut b Builder) write_ptr(ptr &byte, len int) {
38-
unsafe { b.buf.push_many(ptr, len) }
39-
b.len += len
20+
unsafe { b.push_many(ptr, len) }
4021
}
4122

4223
// write_b appends a single `data` byte to the accumulated buffer
4324
pub fn (mut b Builder) write_b(data byte) {
44-
b.buf << data
45-
b.len++
25+
b << data
4626
}
4727

4828
// write implements the Writer interface
4929
pub fn (mut b Builder) write(data []byte) ?int {
50-
b.buf << data
51-
b.len += data.len
30+
b << data
5231
return data.len
5332
}
5433

34+
[inline]
35+
pub fn (b &Builder) byte_at(n int) byte {
36+
return unsafe { (&[]byte(b))[n] }
37+
}
38+
5539
// write appends the string `s` to the buffer
5640
[inline]
5741
pub fn (mut b Builder) write_string(s string) {
5842
if s == '' {
5943
return
6044
}
61-
unsafe { b.buf.push_many(s.str, s.len) }
45+
unsafe { b.push_many(s.str, s.len) }
6246
// for c in s {
6347
// b.buf << c
6448
// }
6549
// b.buf << []byte(s) // TODO
66-
b.len += s.len
6750
}
6851

6952
// go_back discards the last `n` bytes from the buffer
7053
pub fn (mut b Builder) go_back(n int) {
71-
b.buf.trim(b.buf.len - n)
72-
b.len -= n
54+
b.trim(b.len - n)
7355
}
7456

7557
// cut_last cuts the last `n` bytes from the buffer and returns them
7658
pub fn (mut b Builder) cut_last(n int) string {
77-
res := b.buf[b.len - n..].bytestr()
78-
b.buf.trim(b.buf.len - n)
79-
b.len -= n
59+
cut_pos := b.len - n
60+
x := unsafe { (&[]byte(b))[cut_pos..] }
61+
res := x.bytestr()
62+
b.trim(cut_pos)
8063
return res
8164
}
8265

@@ -87,14 +70,13 @@ pub fn (mut b Builder) cut_to(pos int) string {
8770
if pos > b.len {
8871
return ''
8972
}
90-
return b.cut_last(b.buf.len - pos)
73+
return b.cut_last(b.len - pos)
9174
}
9275

9376
// go_back_to resets the buffer to the given position `pos`
9477
// NB: pos should be < than the existing buffer length.
9578
pub fn (mut b Builder) go_back_to(pos int) {
96-
b.buf.trim(pos)
97-
b.len = pos
79+
b.trim(pos)
9880
}
9981

10082
// writeln appends the string `s`, and then a newline character.
@@ -103,10 +85,9 @@ pub fn (mut b Builder) writeln(s string) {
10385
// for c in s {
10486
// b.buf << c
10587
// }
106-
unsafe { b.buf.push_many(s.str, s.len) }
88+
unsafe { b.push_many(s.str, s.len) }
10789
// b.buf << []byte(s) // TODO
108-
b.buf << byte(`\n`)
109-
b.len += s.len + 1
90+
b << byte(`\n`)
11091
}
11192

11293
// buf == 'hello world'
@@ -115,7 +96,8 @@ pub fn (b &Builder) last_n(n int) string {
11596
if n > b.len {
11697
return ''
11798
}
118-
return b.buf[b.len - n..].bytestr()
99+
x := unsafe { (&[]byte(b))[b.len - n..] }
100+
return x.bytestr()
119101
}
120102

121103
// buf == 'hello world'
@@ -124,7 +106,8 @@ pub fn (b &Builder) after(n int) string {
124106
if n >= b.len {
125107
return ''
126108
}
127-
return b.buf[n..].bytestr()
109+
x := unsafe { (&[]byte(b))[n..] }
110+
return x.bytestr()
128111
}
129112

130113
// str returns a copy of all of the accumulated buffer content.
@@ -135,17 +118,15 @@ pub fn (b &Builder) after(n int) string {
135118
// accumulated data that was in the string builder, before the
136119
// .str() call.
137120
pub fn (mut b Builder) str() string {
138-
b.buf << byte(0)
139-
bcopy := unsafe { &byte(memdup(b.buf.data, b.buf.len)) }
140-
s := unsafe { bcopy.vstring_with_len(b.len) }
141-
b.len = 0
142-
b.buf.trim(0)
121+
b << byte(0)
122+
bcopy := unsafe { &byte(memdup(b.data, b.len)) }
123+
s := unsafe { bcopy.vstring_with_len(b.len - 1) }
124+
b.trim(0)
143125
return s
144126
}
145127

146128
// free - manually free the contents of the buffer
147129
[unsafe]
148130
pub fn (mut b Builder) free() {
149-
unsafe { free(b.buf.data) }
150-
b.len = 0
131+
unsafe { free(b.data) }
151132
}

vlib/strings/builder_test.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import strings
33
type MyInt = int
44

55
fn test_sb() {
6-
mut sb := strings.Builder{}
6+
mut sb := strings.new_builder(100)
77
sb.write_string('hi')
88
sb.write_string('!')
99
sb.write_string('hello')

vlib/v/checker/checker.v

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,10 +766,11 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) ast.Type {
766766
c.error('unknown struct: $type_sym.name', struct_init.pos)
767767
return ast.void_type
768768
}
769-
if sym.kind != .struct_ {
769+
if sym.kind == .struct_ {
770+
info = sym.info as ast.Struct
771+
} else {
770772
c.error('alias type name: $sym.name is not struct type', struct_init.pos)
771773
}
772-
info = sym.info as ast.Struct
773774
} else {
774775
info = type_sym.info as ast.Struct
775776
}

vlib/v/fmt/fmt.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ pub fn (mut f Fmt) wrap_long_line(penalty_idx int, add_indent bool) bool {
126126
if penalty_idx > 0 && f.line_len <= fmt.max_len[penalty_idx] {
127127
return false
128128
}
129-
if f.out.buf[f.out.buf.len - 1] == ` ` {
129+
if f.out[f.out.len - 1] == ` ` {
130130
f.out.go_back(1)
131131
}
132132
f.write('\n')
@@ -149,7 +149,7 @@ pub fn (mut f Fmt) remove_new_line(cfg RemoveNewLineConfig) {
149149
mut buffer := if cfg.imports_buffer { unsafe { &f.out_imports } } else { unsafe { &f.out } }
150150
mut i := 0
151151
for i = buffer.len - 1; i >= 0; i-- {
152-
if !buffer.buf[i].is_space() { // != `\n` {
152+
if !buffer.byte_at(i).is_space() { // != `\n` {
153153
break
154154
}
155155
}

vlib/v/fmt/struct.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ pub fn (mut f Fmt) struct_init(node ast.StructInit) {
306306
fields_start := f.out.len
307307
fields_loop: for {
308308
if !single_line_fields {
309-
if use_short_args && f.out.buf[f.out.buf.len - 1] == ` ` {
309+
if use_short_args && f.out[f.out.len - 1] == ` ` {
310310
// v Remove space at tail of line
311311
// f(a, b, c, \n
312312
// f1: 0\n

vlib/v/gen/c/cgen.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5837,9 +5837,9 @@ fn (mut g Gen) insert_before_stmt(s string) {
58375837
}
58385838

58395839
fn (mut g Gen) write_expr_to_string(expr ast.Expr) string {
5840-
pos := g.out.buf.len
5840+
pos := g.out.len
58415841
g.expr(expr)
5842-
return g.out.cut_last(g.out.buf.len - pos)
5842+
return g.out.cut_last(g.out.len - pos)
58435843
}
58445844

58455845
// fn (mut g Gen) start_tmp() {

0 commit comments

Comments
 (0)