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()) d.writeln(v.prof_counters())
} }
dd := d.str() 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() v.generate_main()

View File

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