array: make .set() private + format

pull/1803/head
Alexander Medvednikov 2019-08-31 02:35:05 +03:00
parent 29b4114bc0
commit 72363ada84
2 changed files with 23 additions and 23 deletions

View File

@ -311,7 +311,7 @@ fn (v mut V) compile() {
d.writeln(v.prof_counters())
}
dd := d.str()
cgen.lines.set(defs_pos, dd)// TODO `def.str()` doesn't compile
cgen.lines[defs_pos] = dd// TODO `def.str()` doesn't compile
v.generate_main()

View File

@ -4,7 +4,7 @@
module builtin
import strings
import strings
struct array {
pub:
@ -28,10 +28,10 @@ fn new_array(mylen, cap, elm_size int) array {
}
// TODO
// TODO
pub fn _make(len, cap, elm_size int) array {
return new_array(len, cap, elm_size)
}
return new_array(len, cap, elm_size)
}
// Private function, used by V (`nums := [1, 2, 3]`)
fn new_array_from_c_array(len, cap, elm_size int, c_array voidptr) array {
@ -137,10 +137,10 @@ pub fn (s array) slice(start, _end int) array {
panic('invalid slice index: $start > $end')
}
if end > s.len {
panic('runtime error: slice bounds out of range ($end >= $s.len)')
panic('runtime error: slice bounds out of range ($end >= $s.len)')
}
if start < 0 {
panic('runtime error: slice bounds out of range ($start < 0)')
if start < 0 {
panic('runtime error: slice bounds out of range ($start < 0)')
}
l := end - start
res := array {
@ -148,12 +148,12 @@ pub fn (s array) slice(start, _end int) array {
data: s.data + start * s.element_size
len: l
cap: l
//is_slice: true
//is_slice: true
}
return res
}
pub fn (a mut array) set(idx int, val voidptr) {
fn (a mut array) set(idx int, val voidptr) {
if idx < 0 || idx >= a.len {
panic('array index out of range: $idx / $a.len')
}
@ -176,8 +176,8 @@ fn (arr mut array) _push(val voidptr) {
arr.len++
}
// `val` is array.data
// TODO make private, right now it's used by strings.Builder
// `val` is array.data
// TODO make private, right now it's used by strings.Builder
pub fn (arr mut array) _push_many(val voidptr, size int) {
if arr.len >= arr.cap - size {
cap := (arr.len + size) * 2
@ -214,31 +214,31 @@ pub fn (a array) clone() array {
element_size: a.element_size
data: malloc(a.cap * a.element_size)
}
C.memcpy(arr.data, a.data, a.cap * a.element_size)
return arr
}
C.memcpy(arr.data, a.data, a.cap * a.element_size)
return arr
}
//pub fn (a []int) free() {
pub fn (a array) free() {
//if a.is_slice {
//return
//}
//return
//}
C.free(a.data)
}
// "[ 'a', 'b', 'c' ]"
pub fn (a []string) str() string {
mut sb := strings.new_builder(a.len * 3)
sb.write('[')
mut sb := strings.new_builder(a.len * 3)
sb.write('[')
for i := 0; i < a.len; i++ {
val := a[i]
sb.write('"$val"')
sb.write('"$val"')
if i < a.len - 1 {
sb.write(', ')
sb.write(', ')
}
}
sb.write(']')
return sb.str()
sb.write(']')
return sb.str()
}
pub fn (b []byte) hex() string {