js: fix string.bytes codegen, readline, add tests for strings (#12060)

This commit is contained in:
playX 2021-10-04 18:28:30 +03:00 committed by GitHub
parent e94e08475d
commit 8d1ba52d0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 399 additions and 30 deletions

View file

@ -71,7 +71,7 @@ pub fn (s string) split(dot string) []string {
pub fn (s string) bytes() []byte {
sep := ''
mut arr := s.str.split(sep.str).map(it.charCodeAt(0))
#arr = new array(arr)
#arr = new array(new array_buffer({arr,index_start: new int(0),len: new int(arr.length)}))
return arr
}
@ -735,11 +735,21 @@ pub fn (s string) index_after(p string, start int) int {
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);
#})
if s.len == 0 {
return res
}
mut start := 0
mut end := 0
for i := 0; i < s.len; i++ {
if s[i] == 10 {
end = if i > 0 && s[i - 1] == 13 { i - 1 } else { i }
res << if start == end { '' } else { s[start..end] }
start = i + 1
}
}
if start < s.len {
res << s[start..]
}
return res
}