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

cgen: treat the main module like any other v module

This commit is contained in:
Delyan Angelov
2020-07-01 01:53:53 +03:00
committed by GitHub
parent 81e4d3fd09
commit 78e1127d99
53 changed files with 720 additions and 535 deletions

View File

@@ -49,21 +49,26 @@ pub fn (mut b Builder) go_back(n int) {
b.len -= n
}
fn bytes2string(b []byte) string {
mut copy := b.clone()
copy << `\0`
res := tos(copy.data, copy.len-1)
return res
}
pub fn (mut b Builder) cut_last(n int) string {
buf := b.buf[b.len-n..]
s := string(buf.clone())
res := bytes2string( b.buf[b.len-n..] )
b.buf.trim(b.buf.len-n)
b.len -= n
return s
return res
}
/*
pub fn (mut b Builder) cut_to(pos int) string {
buf := b.buf[pos..]
s := string(buf.clone())
res := bytes2string( b.buf[pos..] )
b.buf.trim(pos)
b.len = pos
return s
return res
}
*/
@@ -88,8 +93,7 @@ pub fn (b &Builder) last_n(n int) string {
if n > b.len {
return ''
}
buf := b.buf[b.len-n..]
return string(buf.clone())
return bytes2string( b.buf[b.len-n..] )
}
// buf == 'hello world'
@@ -98,10 +102,7 @@ pub fn (b &Builder) after(n int) string {
if n >= b.len {
return ''
}
buf := b.buf[n..]
mut copy := buf.clone()
copy << `\0`
return string(copy)
return bytes2string( b.buf[n..] )
}
// NB: in order to avoid memleaks and additional memory copies, after a call to b.str(),

View File

@@ -6,7 +6,8 @@ fn test_sb() {
sb.write('!')
sb.write('hello')
assert sb.len == 8
assert sb.str() == 'hi!hello'
sb_end := sb.str()
assert sb_end == 'hi!hello'
assert sb.len == 0
///
sb = strings.new_builder(10)
@@ -19,39 +20,33 @@ fn test_sb() {
// TODO msvc bug
sb = strings.new_builder(10)
sb.write('123456')
assert sb.cut_last(2) == '56'
assert sb.str() == '1234'
last_2 := sb.cut_last(2)
assert last_2 == '56'
final_sb := sb.str()
assert final_sb == '1234'
}
///
/*
sb = strings.new_builder(10)
sb.write('123456')
x := sb.cut_to(2)
assert x == '456'
assert sb.str() == '123'
*/
}
const (
n = 100000
maxn = 100000
)
fn test_big_sb() {
mut sb := strings.new_builder(100)
mut sb2 := strings.new_builder(10000)
for i in 0..n {
for i in 0..maxn {
sb.writeln(i.str())
sb2.write('+')
}
s := sb.str()
lines := s.split_into_lines()
assert lines.len == n
assert lines.len == maxn
assert lines[0] == '0'
assert lines[1] == '1'
assert lines[777] == '777'
assert lines[98765] == '98765'
println(sb2.len)
assert sb2.len == n
assert sb2.len == maxn
}
@@ -64,5 +59,6 @@ fn test_byte_write() {
count++
assert count == sb.len
}
assert sb.str() == temp_str
sb_final := sb.str()
assert sb_final == temp_str
}