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

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
14 changed files with 399 additions and 30 deletions

View File

@ -45,9 +45,9 @@ fn (mut a array_buffer) set(ix int, val voidptr) {
#array_buffer.prototype.set = function(ix,val) { array_buffer_set(this,ix,val); }
struct array {
mut:
pub mut:
arr array_buffer
pub:
len int
cap int
}
@ -67,6 +67,14 @@ fn v_sort(mut arr array, comparator fn (voidptr, voidptr) int) {
}
}
// trim trims the array length to "index" without modifying the allocated data. If "index" is greater
// than len nothing will be changed.
pub fn (mut a array) trim(index int) {
if index < a.len {
a.len = index
}
}
#function flatIntoArray(target, source, sourceLength, targetIndex, depth) {
#"use strict";
#
@ -368,7 +376,7 @@ pub fn (a &array) free() {
// todo: once (a []byte) will work rewrite this
pub fn (a array) bytestr() string {
res := ''
#a.arr.arr.forEach((item) => res.str += String.fromCharCode(+item))
#for (let i = 0;i < a.arr.len.valueOf();i++) res.str += String.fromCharCode(a.arr.get(new int(i)))
return res
}

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
}