js: os now compiles to the JS backend, more builtins & minor codegen fixes (#11302)

This commit is contained in:
playX 2021-08-25 14:40:53 +03:00 committed by GitHub
parent f257a23313
commit 109d5d5847
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 479 additions and 369 deletions

View file

@ -541,3 +541,168 @@ pub fn (s string) split_nth(delim string, nth int) []string {
}
}
}
struct RepIndex {
idx int
val_idx int
}
// replace_each replaces all occurences of the string pairs given in `vals`.
// Example: assert 'ABCD'.replace_each(['B','C/','C','D','D','C']) == 'AC/DC'
[direct_array_access]
pub fn (s string) replace_each(vals []string) string {
if s.len == 0 || vals.len == 0 {
return s.clone()
}
if vals.len % 2 != 0 {
eprintln('string.replace_each(): odd number of strings')
return s.clone()
}
// `rep` - string to replace
// `with_` - string to replace with_
// Remember positions of all rep strings, and calculate the length
// of the new string to do just one allocation.
mut idxs := []RepIndex{}
mut idx := 0
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 {
rep := vals[rep_i]
mut with_ := vals[rep_i + 1]
with_ = with_
for {
idx = s_.index_after(rep, idx)
if idx == -1 {
break
}
for i in 0 .. rep.len {
mut j_ := i
j_ = j_
#s_.str = setCharAt(s_.str,idx + i, String.fromCharCode(127))
}
idxs << RepIndex{
idx: idx
val_idx: rep_i
}
idx += rep.len
}
}
if idxs.len == 0 {
return s.clone()
}
idxs.sort(a.idx < b.idx)
mut b := ''
mut idx_pos := 0
mut cur_idx := idxs[idx_pos]
mut b_i := 0
for i := 0; i < s.len; i++ {
if i == cur_idx.idx {
rep := vals[cur_idx.val_idx]
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]
b_i++
}
i += rep.len - 1
idx_pos++
if idx_pos < idxs.len {
cur_idx = idxs[idx_pos]
}
} else {
#b.str = setCharAt(b.str,b_i,s.str[i]) //b.str[b_i] = s.str[i]
b_i++
}
}
return b
}
// last_index returns the position of the last occurence of the input string.
fn (s string) last_index_(p string) int {
if p.len > s.len || p.len == 0 {
return -1
}
mut i := s.len - p.len
for i >= 0 {
mut j := 0
for j < p.len && s[i + j] == p[j] {
j++
}
if j == p.len {
return i
}
i--
}
return -1
}
// last_index returns the position of the last occurence of the input string.
pub fn (s string) last_index(p string) ?int {
idx := s.last_index_(p)
if idx == -1 {
return none
}
return idx
}
pub fn (s string) trim_space() string {
res := ''
#res.str = s.str.trim()
return res
}
pub fn (s string) index_after(p string, start int) int {
if p.len > s.len {
return -1
}
mut strt := start
if start < 0 {
strt = 0
}
if start >= s.len {
return -1
}
mut i := strt
for i < s.len {
mut j := 0
mut ii := i
for j < p.len && s[ii] == p[j] {
j++
ii++
}
if j == p.len {
return i
}
i++
}
return -1
}
pub fn (s string) split_into_lines() []string {
mut res := []string{}
#let i = 0
#s.str.split('\n').forEach((str) => {
#res.arr[i] = new string(str);
#})
return res
}