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

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

@ -76,7 +76,7 @@ fn new_scanner_file(file_path string) &Scanner {
fn new_scanner(text string) &Scanner { fn new_scanner(text string) &Scanner {
return &Scanner { return &Scanner {
text: text text: text
fmt_out: strings.new_builder(10000) fmt_out: strings.new_builder(1000)
should_print_line_on_error: true should_print_line_on_error: true
should_print_errors_in_color: true should_print_errors_in_color: true
should_print_relative_paths_on_error: true should_print_relative_paths_on_error: true

View File

@ -238,7 +238,6 @@ fn (p mut Parser) struct_init(typ string) string {
p.warn('type `$t.name` is private') p.warn('type `$t.name` is private')
} }
if p.gen_struct_init(typ, t) { return typ } if p.gen_struct_init(typ, t) { return typ }
p.scanner.fmt_out.cut(typ.len)
ptr := typ.contains('*') ptr := typ.contains('*')
mut did_gen_something := false mut did_gen_something := false
// Loop thru all struct init keys and assign values // Loop thru all struct init keys and assign values

View File

@ -13,6 +13,8 @@ fn (scanner mut Scanner) fgen(s_ string) {
if scanner.fmt_line_empty { if scanner.fmt_line_empty {
s = strings.repeat(`\t`, scanner.fmt_indent) + s s = strings.repeat(`\t`, scanner.fmt_indent) + s
} }
//scanner.fmt_out << s
scanner.fmt_out.write(s) scanner.fmt_out.write(s)
scanner.fmt_line_empty = false scanner.fmt_line_empty = false
} }
@ -23,6 +25,8 @@ fn (scanner mut Scanner) fgenln(s_ string) {
if scanner.fmt_line_empty { if scanner.fmt_line_empty {
s = strings.repeat(`\t`, scanner.fmt_indent) + s s = strings.repeat(`\t`, scanner.fmt_indent) + s
} }
//scanner.fmt_out << s
//scanner.fmt_out << '\n'
scanner.fmt_out.writeln(s) scanner.fmt_out.writeln(s)
scanner.fmt_line_empty = true scanner.fmt_line_empty = true
} }
@ -111,6 +115,7 @@ fn (p mut Parser) fnext() {
} }
} }
[if vfmt] [if vfmt]
fn (p mut Parser) gen_fmt() { fn (p mut Parser) gen_fmt() {
if p.pass != .main { if p.pass != .main {
@ -120,6 +125,7 @@ fn (p mut Parser) gen_fmt() {
return return
} }
s := p.scanner.fmt_out.str().trim_space() s := p.scanner.fmt_out.str().trim_space()
//s := p.scanner.fmt_out.join('').trim_space()
if s == '' { if s == '' {
return return
} }
@ -128,8 +134,7 @@ fn (p mut Parser) gen_fmt() {
verror('failed to create fmt.v') verror('failed to create fmt.v')
return return
} }
//println(p.scanner.fmt_out.str()) out.writeln(s)//p.scanner.fmt_out.str().trim_space())
out.writeln(p.scanner.fmt_out.str().trim_space())
out.close() out.close()
} }

View File

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

View File

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