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

v.gen.js: port fully the array test suite & add fixes (#11073)

This commit is contained in:
playX
2021-08-07 17:58:49 +03:00
committed by GitHub
parent c560d58f1e
commit 94c321c80d
8 changed files with 360 additions and 15 deletions

View File

@ -14,3 +14,13 @@ pub fn (mut m map) delete(key voidptr) {
pub fn (m &map) free() {}
#map.prototype[Symbol.iterator] = function () { return this.map[Symbol.iterator](); }
#map.prototype.toString = function () {
#function fmtKey(key) { return typeof key == 'string' ? '\'' + key + '\'' : key}
#let res = '{'
#for (const entry of this) {
#res += fmtKey(entry[0]) + ': ' + entry[0];
#}
#res += '}'
#return res;
#}

View File

@ -94,7 +94,10 @@ pub fn (s string) count(substr string) int {
}
pub fn (s string) ends_with(p string) bool {
return s.str.endsWith(p.str)
mut res := false
#res.val = s.str.endsWith(p.str)
return res
}
pub fn (s string) starts_with(p string) bool {
@ -133,7 +136,7 @@ pub fn (s string) fields() []string {
}
pub fn (s string) find_between(start string, end string) string {
return string(s.str.slice(s.str.indexOf(start.str), s.str.indexOf(end.str) + 1))
return string(s.str.slice(s.str.indexOf(start.str) + 1, s.str.indexOf(end.str)))
}
// unnecessary in the JS backend, implemented for api parity.
@ -464,3 +467,77 @@ pub fn (s string) strip_margin_custom(del byte) string {
return result
}
// split_nth splits the string based on the passed `delim` substring.
// It returns the first Nth parts. When N=0, return all the splits.
// The last returned element has the remainder of the string, even if
// the remainder contains more `delim` substrings.
[direct_array_access]
pub fn (s string) split_nth(delim string, nth int) []string {
mut res := []string{}
mut i := 0
match delim.len {
0 {
i = 1
for ch in s {
if nth > 0 && i >= nth {
res << s[i..]
break
}
res << ch.str()
i++
}
return res
}
1 {
mut start := 0
delim_byte := delim[0]
for i < s.len {
if s[i] == delim_byte {
was_last := nth > 0 && res.len == nth - 1
if was_last {
break
}
val := s[start..i] //.substr(start, i)
res << val
start = i + delim.len
i = start
} else {
i++
}
}
// Then the remaining right part of the string
if nth < 1 || res.len < nth {
res << s[start..]
}
return res
}
else {
mut start := 0
// Take the left part for each delimiter occurence
for i <= s.len {
is_delim := i + delim.len <= s.len && s[i..i + delim.len] == delim
if is_delim {
was_last := nth > 0 && res.len == nth - 1
if was_last {
break
}
val := s[start..i] // .substr(start, i)
res << val
start = i + delim.len
i = start
} else {
i++
}
}
// Then the remaining right part of the string
if nth < 1 || res.len < nth {
res << s[start..]
}
return res
}
}
}