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

all: switch to the new fn arg syntax everywhere; add lots of vfmt -verify tests

This commit is contained in:
Alexander Medvednikov
2020-10-15 12:32:28 +02:00
parent 982056894e
commit 7da1afa140
37 changed files with 382 additions and 404 deletions

View File

@ -9,8 +9,8 @@ pub struct array {
pub:
element_size int
pub mut:
data voidptr// Using a void pointer allows to implement arrays without generics and without generating
// extra code for every type.
data voidptr // Using a void pointer allows to implement arrays without generics and without generating
// extra code for every type.
len int
cap int
}
@ -36,10 +36,8 @@ fn __new_array_with_default(mylen int, cap int, elm_size int, val voidptr) array
cap: cap_
}
if val != 0 {
for i in 0..arr.len {
unsafe {
arr.set_unsafe(i, val)
}
for i in 0 .. arr.len {
unsafe {arr.set_unsafe(i, val)}
}
}
return arr
@ -53,19 +51,16 @@ fn __new_array_with_array_default(mylen int, cap int, elm_size int, val array) a
len: mylen
cap: cap_
}
for i in 0..arr.len {
for i in 0 .. arr.len {
val_clone := val.clone()
unsafe {
arr.set_unsafe(i, &val_clone)
}
unsafe {arr.set_unsafe(i, &val_clone)}
}
return arr
}
// 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 int, cap int, elm_size int, c_array voidptr) array {
cap_ := if cap < len { len } else { cap }
arr := array{
element_size: elm_size
data: vcalloc(cap_ * elm_size)
@ -73,14 +68,12 @@ fn new_array_from_c_array(len, cap, elm_size int, c_array voidptr) array {
cap: cap_
}
// TODO Write all memory functions (like memcpy) in V
unsafe {
C.memcpy(arr.data, c_array, len * elm_size)
}
unsafe {C.memcpy(arr.data, c_array, len * elm_size)}
return arr
}
// Private function, used by V (`nums := [1, 2, 3] !`)
fn new_array_from_c_array_no_alloc(len, cap, elm_size int, c_array voidptr) array {
fn new_array_from_c_array_no_alloc(len int, cap int, elm_size int, c_array voidptr) array {
arr := array{
element_size: elm_size
data: c_array
@ -102,8 +95,7 @@ fn (mut a array) ensure_cap(required int) {
}
if a.cap == 0 {
a.data = vcalloc(cap * a.element_size)
}
else {
} else {
a.data = v_realloc(a.data, u32(cap * a.element_size))
}
a.cap = cap
@ -124,20 +116,14 @@ pub fn (a array) repeat(count int) array {
len: count * a.len
cap: count * a.len
}
for i in 0..count {
for i in 0 .. count {
if a.len > 0 && a.element_size == sizeof(array) {
ary := array{}
unsafe {
C.memcpy(&ary, a.data, sizeof(array))
}
unsafe {C.memcpy(&ary, a.data, sizeof(array))}
ary_clone := ary.clone()
unsafe {
C.memcpy(arr.get_unsafe(i * a.len), &ary_clone, a.len * a.element_size)
}
unsafe {C.memcpy(arr.get_unsafe(i * a.len), &ary_clone, a.len * a.element_size)}
} else {
unsafe {
C.memcpy(arr.get_unsafe(i * a.len), byteptr(a.data), a.len * a.element_size)
}
unsafe {C.memcpy(arr.get_unsafe(i * a.len), byteptr(a.data), a.len * a.element_size)}
}
}
return arr
@ -150,7 +136,7 @@ pub fn (mut a array) sort_with_compare(compare voidptr) {
// array.insert
pub fn (mut a array) insert(i int, val voidptr) {
$if !no_bounds_checking? {
$if !no_bounds_checking ? {
if i < 0 || i > a.len {
panic('array.insert: index out of range (i == $i, a.len == $a.len)')
}
@ -165,7 +151,7 @@ pub fn (mut a array) insert(i int, val voidptr) {
// array.insert_many
pub fn (mut a array) insert_many(i int, val voidptr, size int) {
$if !no_bounds_checking? {
$if !no_bounds_checking ? {
if i < 0 || i > a.len {
panic('array.insert_many: index out of range (i == $i, a.len == $a.len)')
}
@ -192,16 +178,14 @@ pub fn (mut a array) prepend_many(val voidptr, size int) {
// array.delete deletes array element at the given index
pub fn (mut a array) delete(i int) {
$if !no_bounds_checking? {
$if !no_bounds_checking ? {
if i < 0 || i >= a.len {
panic('array.delete: index out of range (i == $i, a.len == $a.len)')
}
}
// NB: if a is [12,34], a.len = 2, a.delete(0)
// should move (2-0-1) elements = 1 element (the 34) forward
unsafe {
C.memmove(a.get_unsafe(i), a.get_unsafe(i + 1), (a.len - i - 1) * a.element_size)
}
unsafe {C.memmove(a.get_unsafe(i), a.get_unsafe(i + 1), (a.len - i - 1) * a.element_size)}
a.len--
}
@ -219,7 +203,8 @@ pub fn (mut a array) trim(index int) {
}
// we manually inline this for single operations for performance without -prod
[inline] [unsafe]
[inline]
[unsafe]
fn (a array) get_unsafe(i int) voidptr {
unsafe {
return byteptr(a.data) + i * a.element_size
@ -228,7 +213,7 @@ fn (a array) get_unsafe(i int) voidptr {
// Private function. Used to implement array[] operator
fn (a array) get(i int) voidptr {
$if !no_bounds_checking? {
$if !no_bounds_checking ? {
if i < 0 || i >= a.len {
panic('array.get: index out of range (i == $i, a.len == $a.len)')
}
@ -240,7 +225,7 @@ fn (a array) get(i int) voidptr {
// array.first returns the first element of the array
pub fn (a array) first() voidptr {
$if !no_bounds_checking? {
$if !no_bounds_checking ? {
if a.len == 0 {
panic('array.first: array is empty')
}
@ -250,7 +235,7 @@ pub fn (a array) first() voidptr {
// array.last returns the last element of the array
pub fn (a array) last() voidptr {
$if !no_bounds_checking? {
$if !no_bounds_checking ? {
if a.len == 0 {
panic('array.last: array is empty')
}
@ -263,13 +248,13 @@ pub fn (a array) last() voidptr {
// array.pop returns the last element of the array, and removes it
pub fn (mut a array) pop() voidptr {
// in a sense, this is the opposite of `a << x`
$if !no_bounds_checking? {
$if !no_bounds_checking ? {
if a.len == 0 {
panic('array.pop: array is empty')
}
}
new_len := a.len - 1
last_elem := unsafe { byteptr(a.data) + (new_len) * a.element_size }
last_elem := unsafe {byteptr(a.data) + (new_len) * a.element_size}
a.len = new_len
// NB: a.cap is not changed here *on purpose*, so that
// further << ops on that array will be more efficient.
@ -280,9 +265,9 @@ pub fn (mut a array) pop() voidptr {
// but starting from the `start` element and ending with the element before
// the `end` element of the original array with the length and capacity
// set to the number of the elements in the slice.
fn (a array) slice(start, _end int) array {
fn (a array) slice(start int, _end int) array {
mut end := _end
$if !no_bounds_checking? {
$if !no_bounds_checking ? {
if start > end {
panic('array.slice: invalid slice index ($start > $end)')
}
@ -308,7 +293,7 @@ fn (a array) slice(start, _end int) array {
}
// used internally for [2..4]
fn (a array) slice2(start, _end int, end_max bool) array {
fn (a array) slice2(start int, _end int, end_max bool) array {
end := if end_max { a.len } else { _end }
return a.slice(start, end)
}
@ -333,27 +318,21 @@ pub fn (a &array) clone() array {
}
// Recursively clone-generated elements if array element is array type
if a.element_size == sizeof(array) {
for i in 0..a.len {
for i in 0 .. a.len {
ar := array{}
unsafe {
C.memcpy(&ar, a.get_unsafe(i), sizeof(array))
}
unsafe {C.memcpy(&ar, a.get_unsafe(i), sizeof(array))}
ar_clone := ar.clone()
unsafe {
arr.set_unsafe(i, &ar_clone)
}
unsafe {arr.set_unsafe(i, &ar_clone)}
}
} else {
unsafe {
C.memcpy(byteptr(arr.data), a.data, a.cap * a.element_size)
}
unsafe {C.memcpy(byteptr(arr.data), a.data, a.cap * a.element_size)}
}
return arr
}
fn (a &array) slice_clone(start, _end int) array {
fn (a &array) slice_clone(start int, _end int) array {
mut end := _end
$if !no_bounds_checking? {
$if !no_bounds_checking ? {
if start > end {
panic('array.slice: invalid slice index ($start > $end)')
}
@ -379,30 +358,25 @@ fn (a &array) slice_clone(start, _end int) array {
}
// we manually inline this for single operations for performance without -prod
[inline] [unsafe]
[inline]
[unsafe]
fn (mut a array) set_unsafe(i int, val voidptr) {
unsafe {
C.memcpy(byteptr(a.data) + a.element_size * i, val, a.element_size)
}
unsafe {C.memcpy(byteptr(a.data) + a.element_size * i, val, a.element_size)}
}
// Private function. Used to implement assigment to the array element.
fn (mut a array) set(i int, val voidptr) {
$if !no_bounds_checking? {
$if !no_bounds_checking ? {
if i < 0 || i >= a.len {
panic('array.set: index out of range (i == $i, a.len == $a.len)')
}
}
unsafe {
C.memcpy(byteptr(a.data) + a.element_size * i, val, a.element_size)
}
unsafe {C.memcpy(byteptr(a.data) + a.element_size * i, val, a.element_size)}
}
fn (mut a array) push(val voidptr) {
a.ensure_cap(a.len + 1)
unsafe {
C.memcpy(byteptr(a.data) + a.element_size * a.len, val, a.element_size)
}
unsafe {C.memcpy(byteptr(a.data) + a.element_size * a.len, val, a.element_size)}
a.len++
}
@ -414,14 +388,12 @@ pub fn (mut a3 array) push_many(val voidptr, size int) {
copy := a3.clone()
a3.ensure_cap(a3.len + size)
unsafe {
//C.memcpy(a.data, copy.data, copy.element_size * copy.len)
// C.memcpy(a.data, copy.data, copy.element_size * copy.len)
C.memcpy(a3.get_unsafe(a3.len), copy.data, a3.element_size * size)
}
} else {
a3.ensure_cap(a3.len + size)
unsafe {
C.memcpy(a3.get_unsafe(a3.len), val, a3.element_size * size)
}
unsafe {C.memcpy(a3.get_unsafe(a3.len), val, a3.element_size * size)}
}
a3.len += size
}
@ -432,10 +404,11 @@ pub fn (mut a array) reverse_in_place() {
}
unsafe {
mut tmp_value := malloc(a.element_size)
for i in 0..a.len/2 {
for i in 0 .. a.len / 2 {
C.memcpy(tmp_value, byteptr(a.data) + i * a.element_size, a.element_size)
C.memcpy(byteptr(a.data) + i * a.element_size, byteptr(a.data) + (a.len-1-i) * a.element_size, a.element_size)
C.memcpy(byteptr(a.data) + (a.len-1-i) * a.element_size, tmp_value, a.element_size)
C.memcpy(byteptr(a.data) + i * a.element_size, byteptr(a.data) + (a.len - 1 - i) *
a.element_size, a.element_size)
C.memcpy(byteptr(a.data) + (a.len - 1 - i) * a.element_size, tmp_value, a.element_size)
}
free(tmp_value)
}
@ -453,10 +426,8 @@ pub fn (a array) reverse() array {
len: a.len
cap: a.cap
}
for i in 0..a.len {
unsafe {
arr.set_unsafe(i, a.get_unsafe(a.len - 1 - i))
}
for i in 0 .. a.len {
unsafe {arr.set_unsafe(i, a.get_unsafe(a.len - 1 - i))}
}
return arr
}
@ -478,7 +449,7 @@ pub fn (a &array) free() {
pub fn (a []string) str() string {
mut sb := strings.new_builder(a.len * 3)
sb.write('[')
for i in 0..a.len {
for i in 0 .. a.len {
val := a[i]
sb.write("\'")
sb.write(val)
@ -508,7 +479,7 @@ pub fn (b []byte) hex() string {
}
unsafe {
hex[dst_i] = `\0`
return tos(hex,dst_i)
return tos(hex, dst_i)
}
}
@ -516,20 +487,18 @@ pub fn (b []byte) hex() string {
// The number of the elements copied is the minimum of the length of both arrays.
// Returns the number of elements copied.
// TODO: implement for all types
pub fn copy(dst, src []byte) int {
pub fn copy(dst []byte, src []byte) int {
if dst.len > 0 && src.len > 0 {
mut min := 0
min = if dst.len < src.len { dst.len } else { src.len }
unsafe {
C.memcpy(byteptr(dst.data), src[..min].data, dst.element_size * min)
}
unsafe {C.memcpy(byteptr(dst.data), src[..min].data, dst.element_size * min)}
return min
}
return 0
}
// Private function. Comparator for int type.
fn compare_ints(a, b &int) int {
fn compare_ints(a &int, b &int) int {
if *a < *b {
return -1
}
@ -539,7 +508,7 @@ fn compare_ints(a, b &int) int {
return 0
}
fn compare_ints_reverse(a, b &int) int {
fn compare_ints_reverse(a &int, b &int) int {
if *a > *b {
return -1
}
@ -549,7 +518,7 @@ fn compare_ints_reverse(a, b &int) int {
return 0
}
fn compare_floats(a, b &f64) int {
fn compare_floats(a &f64, b &f64) int {
if *a < *b {
return -1
}
@ -559,7 +528,7 @@ fn compare_floats(a, b &f64) int {
return 0
}
fn compare_floats_reverse(a, b &f64) int {
fn compare_floats_reverse(a &f64, b &f64) int {
if *a > *b {
return -1
}
@ -577,7 +546,7 @@ pub fn (mut a []int) sort() {
// []string.index returns the index of the first element equal to the given value,
// or -1 if the value is not found in the array.
pub fn (a []string) index(v string) int {
for i in 0..a.len {
for i in 0 .. a.len {
if a[i] == v {
return i
}
@ -588,7 +557,7 @@ pub fn (a []string) index(v string) int {
// []int.index returns the index of the first element equal to the given value,
// or -1 if the value is not found in the array.
pub fn (a []int) index(v int) int {
for i in 0..a.len {
for i in 0 .. a.len {
if a[i] == v {
return i
}
@ -599,7 +568,7 @@ pub fn (a []int) index(v int) int {
// []byte.index returns the index of the first element equal to the given value,
// or -1 if the value is not found in the array.
pub fn (a []byte) index(v byte) int {
for i in 0..a.len {
for i in 0 .. a.len {
if a[i] == v {
return i
}
@ -608,7 +577,7 @@ pub fn (a []byte) index(v byte) int {
}
pub fn (a []rune) index(v rune) int {
for i in 0..a.len {
for i in 0 .. a.len {
if a[i] == v {
return i
}
@ -620,7 +589,7 @@ pub fn (a []rune) index(v rune) int {
// or -1 if the value is not found in the array.
// TODO is `char` type yet in the language?
pub fn (a []char) index(v char) int {
for i in 0..a.len {
for i in 0 .. a.len {
if a[i] == v {
return i
}
@ -630,12 +599,11 @@ pub fn (a []char) index(v char) int {
// []int.reduce executes a given reducer function on each element of the array,
// resulting in a single output value.
pub fn (a []int) reduce(iter fn(accum, curr int)int, accum_start int) int {
pub fn (a []int) reduce(iter fn (int, int) int, accum_start int) int {
mut accum_ := accum_start
for i in a {
accum_ = iter(accum_, i)
}
return accum_
}
@ -671,13 +639,12 @@ pub fn (a []f32) eq(a2 []f32) bool {
return array_eq(a, a2)
}
*/
pub fn (a1 []string) eq(a2 []string) bool {
//return array_eq(a, a2)
// return array_eq(a, a2)
if a1.len != a2.len {
return false
}
for i in 0..a1.len {
for i in 0 .. a1.len {
if a1[i] != a2[i] {
return false
}
@ -693,7 +660,7 @@ pub fn (a1 []string) eq(a2 []string) bool {
// println(x) // Sorted i64 Array
// output:
// [10, 28, 70, 92, 100]
pub fn compare_i64(a, b &i64) int {
pub fn compare_i64(a &i64, b &i64) int {
if *a < *b {
return -1
}
@ -705,7 +672,7 @@ pub fn compare_i64(a, b &i64) int {
// compare_f64 for []f64 sort_with_compare()
// ref. compare_i64(...)
pub fn compare_f64(a, b &f64) int {
pub fn compare_f64(a &f64, b &f64) int {
if *a < *b {
return -1
}
@ -717,7 +684,7 @@ pub fn compare_f64(a, b &f64) int {
// compare_f32 for []f32 sort_with_compare()
// ref. compare_i64(...)
pub fn compare_f32(a, b &f32) int {
pub fn compare_f32(a &f32, b &f32) int {
if *a < *b {
return -1
}
@ -731,10 +698,8 @@ pub fn compare_f32(a, b &f32) int {
// is the address of the corresponding element in a.
pub fn (a array) pointers() []voidptr {
mut res := []voidptr{}
for i in 0..a.len {
unsafe {
res << a.get_unsafe(i)
}
for i in 0 .. a.len {
unsafe {res << a.get_unsafe(i)}
}
return res
}

View File

@ -36,7 +36,7 @@ pub fn print_backtrace() {
}
// replaces panic when -debug arg is passed
fn panic_debug(line_no int, file, mod, fn_name, s string) {
fn panic_debug(line_no int, file string, mod string, fn_name string, s string) {
// NB: the order here is important for a stabler test output
// module is less likely to change than function, etc...
// During edits, the line number will change most frequently,
@ -239,7 +239,7 @@ pub fn is_atty(fd int) int {
}
}
fn __as_cast(obj voidptr, obj_type, expected_type int) voidptr {
fn __as_cast(obj voidptr, obj_type int, expected_type int) voidptr {
if obj_type != expected_type {
panic('as cast: cannot cast $obj_type to $expected_type')
}

View File

@ -91,7 +91,7 @@ fn f64_abs(a f64) f64 {
[inline]
pub fn f32_max(a, b f32) f32 {
pub fn f32_max(a f32, b f32) f32 {
return if a > b {
a
} else {
@ -100,7 +100,7 @@ pub fn f32_max(a, b f32) f32 {
}
[inline]
pub fn f32_min(a, b f32) f32 {
pub fn f32_min(a f32, b f32) f32 {
return if a < b {
a
} else {
@ -109,7 +109,7 @@ pub fn f32_min(a, b f32) f32 {
}
[inline]
pub fn f64_max(a, b f64) f64 {
pub fn f64_max(a f64, b f64) f64 {
return if a > b {
a
} else {
@ -118,7 +118,7 @@ pub fn f64_max(a, b f64) f64 {
}
[inline]
fn f64_min(a, b f64) f64 {
fn f64_min(a f64, b f64) f64 {
return if a < b {
a
} else {

View File

@ -83,7 +83,7 @@ const (
// the strings are very likely to be equal
// TODO: add branch prediction hints
[inline]
fn fast_string_eq(a, b string) bool {
fn fast_string_eq(a string, b string) bool {
if a.len != b.len {
return false
}
@ -219,7 +219,7 @@ fn new_map_1(value_bytes int) map {
}
}
fn new_map_init(n, value_bytes int, keys &string, values voidptr) map {
fn new_map_init(n int, value_bytes int, keys &string, values voidptr) map {
mut out := new_map_1(value_bytes)
for i in 0 .. n {
unsafe {

View File

@ -37,7 +37,7 @@ mut:
values [11]voidptr // TODO: Should use `max_len`
}
fn new_sorted_map(n, value_bytes int) SortedMap { // TODO: Remove `n`
fn new_sorted_map(n int, value_bytes int) SortedMap { // TODO: Remove `n`
return SortedMap {
value_bytes: value_bytes
root: new_node()
@ -45,7 +45,7 @@ fn new_sorted_map(n, value_bytes int) SortedMap { // TODO: Remove `n`
}
}
fn new_sorted_map_init(n, value_bytes int, keys &string, values voidptr) SortedMap {
fn new_sorted_map_init(n int, value_bytes int, keys &string, values voidptr) SortedMap {
mut out := new_sorted_map(n, value_bytes)
for i in 0 .. n {
unsafe {

View File

@ -169,14 +169,14 @@ pub fn cstring_to_vstring(cstr byteptr) string {
return tos_clone(cstr)
}
pub fn (s string) replace_once(rep, with string) string {
pub fn (s string) replace_once(rep string, with string) string {
index := s.index(rep) or {
return s.clone()
}
return s.substr(0, index) + with + s.substr(index + rep.len, s.len)
}
pub fn (s string) replace(rep, with string) string {
pub fn (s string) replace(rep string, with string) string {
if s.len == 0 || rep.len == 0 {
return s.clone()
}
@ -239,7 +239,7 @@ struct RepIndex {
val_idx int
}
fn compare_rep_index(a, b &RepIndex) int {
fn compare_rep_index(a &RepIndex, b &RepIndex) int {
if a.idx < b.idx {
return -1
}
@ -566,12 +566,12 @@ fn (s string) right(n int) string {
}
// used internally for [2..4]
fn (s string) substr2(start, _end int, end_max bool) string {
fn (s string) substr2(start int, _end int, end_max bool) string {
end := if end_max { s.len } else { _end }
return s.substr(start, end)
}
pub fn (s string) substr(start, end int) string {
pub fn (s string) substr(start int, end int) string {
$if !no_bounds_checking? {
if start > end || start > s.len || end > s.len || start < 0 || end < 0 {
panic('substr($start, $end) out of bounds (len=$s.len)')
@ -898,7 +898,7 @@ pub fn (s string) is_title() bool {
// 'hey [man] how you doin'
// find_between('[', ']') == 'man'
pub fn (s string) find_between(start, end string) string {
pub fn (s string) find_between(start string, end string) string {
start_pos := s.index(start) or {
return ''
}
@ -1004,7 +1004,7 @@ pub fn (s string) trim_suffix(str string) string {
return s
}
pub fn compare_strings(a, b &string) int {
pub fn compare_strings(a &string, b &string) int {
if a.lt(b) {
return -1
}
@ -1014,7 +1014,7 @@ pub fn compare_strings(a, b &string) int {
return 0
}
fn compare_strings_reverse(a, b &string) int {
fn compare_strings_reverse(a &string, b &string) int {
if a.lt(b) {
return 1
}
@ -1024,7 +1024,7 @@ fn compare_strings_reverse(a, b &string) int {
return 0
}
fn compare_strings_by_len(a, b &string) int {
fn compare_strings_by_len(a &string, b &string) int {
if a.len < b.len {
return -1
}
@ -1034,7 +1034,7 @@ fn compare_strings_by_len(a, b &string) int {
return 0
}
fn compare_lower_strings(a, b &string) int {
fn compare_lower_strings(a &string, b &string) int {
aa := a.to_lower()
bb := b.to_lower()
return compare_strings(aa, bb)
@ -1200,7 +1200,7 @@ pub fn (u ustring) count(substr ustring) int {
return 0 // TODO can never get here - v doesn't know that
}
pub fn (u ustring) substr(_start, _end int) string {
pub fn (u ustring) substr(_start int, _end int) string {
$if !no_bounds_checking? {
if _start > _end || _start > u.len || _end > u.len || _start < 0 || _end < 0 {
panic('substr($_start, $_end) out of bounds (len=$u.len)')