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

JavaSript backend (early stage)

This commit is contained in:
Alexander Medvednikov
2019-09-14 23:48:30 +03:00
parent 982a162fbf
commit 5cc81b91cb
31 changed files with 1818 additions and 584 deletions

View File

@ -78,10 +78,9 @@ pub fn (s string) replace(rep, with string) string {
if s.len == 0 || rep.len == 0 {
return s
}
// println('"$s" replace "$rep" with "$with" rep.len=$rep.len')
// TODO PERF Allocating ints is expensive. Should be a stack array
// Get locations of all reps within this string
mut idxs := []int{}
mut idxs := []int
mut rem := s
mut rstart := 0
for {
@ -352,37 +351,58 @@ pub fn (s string) substr(start, end int) string {
return res
}
// KMP search
pub fn (s string) index(p string) int {
pub fn (s string) index_old(p string) int {
if p.len > s.len {
return -1
}
mut prefix := [0; p.len]
mut j := 0
for i := 1; i < p.len; i++ {
for p[j] != p[i] && j > 0 {
j = prefix[j - 1]
}
if p[j] == p[i] {
j++
}
prefix[i] = j
}
j = 0
for i := 0; i < s.len; i++ {
for p[j] != s[i] && j > 0 {
j = prefix[j - 1]
}
if p[j] == s[i] {
mut i := 0
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 - p.len + 1
}
i++
}
return -1
}
// KMP search
pub fn (s string) index(p string) int {
if p.len > s.len {
return -1
}
mut prefix := [0].repeat2(p.len)
mut j := 0
for i := 1; i < p.len; i++ {
for p[j] != p[i] && j > 0 {
j = prefix[j - 1]
}
if p[j] == p[i] {
j++
}
prefix[i] = j
}
j = 0
for i := 0; i < s.len; i++ {
for p[j] != s[i] && j > 0 {
j = prefix[j - 1]
}
if p[j] == s[i] {
j++
}
if j == p.len {
return i - p.len + 1
}
}
return -1
}
pub fn (s string) index_any(chars string) int {
for c in chars {
index := s.index(c.str())
@ -874,7 +894,7 @@ pub fn (s string) bytes() []byte {
if s.len == 0 {
return []byte
}
mut buf := [byte(0); s.len]
mut buf := [byte(0)].repeat2(s.len)
C.memcpy(buf.data, s.str, s.len)
return buf
}