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

js: add basic support for running tests, fix string.replace_each (#11314)

This commit is contained in:
playX
2021-08-26 15:20:54 +03:00
committed by GitHub
parent 403da9f36e
commit bdf11d969a
8 changed files with 178 additions and 28 deletions

View File

@@ -36,3 +36,11 @@ pub fn eprint(s any) {
panic('Cannot `eprint` in a browser, use `println` instead')
}
}
// Exits the process in node, and halts execution in the browser
// because `process.exit` is undefined. Workaround for not having
// a 'real' way to exit in the browser.
pub fn exit(c int) {
JS.process.exit(c)
js_throw('exit($c)')
}

View File

@@ -6,14 +6,6 @@ module builtin
fn (a any) toString()
// Exits the process in node, and halts execution in the browser
// because `process.exit` is undefined. Workaround for not having
// a 'real' way to exit in the browser.
pub fn exit(c int) {
JS.process.exit(c)
js_throw('exit($c)')
}
pub fn unwrap(opt any) any {
o := &Option(opt)
if o.state != 0 {

View File

@@ -567,16 +567,19 @@ pub fn (s string) replace_each(vals []string) string {
mut idxs := []RepIndex{}
mut idx := 0
mut new_len := s.len
s_ := s.clone()
#function setCharAt(str,index,chr) {
#if(index > str.length-1) return str;
#return str.substring(0,index) + chr + str.substring(index+1);
#}
for rep_i := 0; rep_i < vals.len; rep_i += 2 {
for rep_i := 0; rep_i < vals.len; rep_i = rep_i + 2 {
rep := vals[rep_i]
mut with_ := vals[rep_i + 1]
with_ = with_
for {
idx = s_.index_after(rep, idx)
if idx == -1 {
@@ -589,11 +592,16 @@ pub fn (s string) replace_each(vals []string) string {
#s_.str = setCharAt(s_.str,idx + i, String.fromCharCode(127))
}
idxs << RepIndex{
idx: idx
val_idx: rep_i
rep_idx := RepIndex{
idx: 0
val_idx: 0
}
// todo: primitives should always be copied
#rep_idx.idx = idx.val
#rep_idx.val_idx = new int(rep_i.val)
idxs << rep_idx
idx += rep.len
new_len += with_.len - rep.len
}
}
@@ -604,6 +612,9 @@ pub fn (s string) replace_each(vals []string) string {
idxs.sort(a.idx < b.idx)
mut b := ''
#for (let i = 0; i < new_len.val;i++) b.str += String.fromCharCode(127)
new_len = new_len
mut idx_pos := 0
mut cur_idx := idxs[idx_pos]
mut b_i := 0
@@ -613,6 +624,7 @@ pub fn (s string) replace_each(vals []string) string {
with_ := vals[cur_idx.val_idx + 1]
for j in 0 .. with_.len {
mut j_ := j
j_ = j_
#b.str = setCharAt(b.str,b_i, with_.str[j])
//#b.str[b_i] = with_.str[j]