diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 332fe76242..de5d1eef25 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,6 +27,29 @@ jobs: ./v fmt -verify vlib/v/gen/x64/gen.v ./v fmt -verify vlib/v/table/table.v ./v fmt -verify vlib/v/fmt/fmt.v + ./v fmt -verify vlib/builtin/array.v + ./v fmt -verify vlib/os/file.v + ./v fmt -verify vlib/v/util/errors.v + ./v fmt -verify vlib/v/util/suggestions.v + ./v fmt -verify vlib/v/util/util.v + ./v fmt -verify vlib/v/builder/builder.v + ./v fmt -verify vlib/v/builder/cc.v + ./v fmt -verify vlib/v/builder/compile.v + ./v fmt -verify vlib/v/builder/msvc.v + ./v fmt -verify vlib/math/bits/bits.v + ./v fmt -verify vlib/time/time.v + ./v fmt -verify vlib/term/colors.v + ./v fmt -verify vlib/term/term.v + ./v fmt -verify vlib/v/ast/scope.v + ./v fmt -verify vlib/v/checker/check_types.v + ./v fmt -verify vlib/v/table/atypes.v + ./v fmt -verify vlib/v/cflag/cflags.v + ./v fmt -verify vlib/v/table/cflags.v + ./v fmt -verify vlib/v/gen/auto_str_methods.v + ./v fmt -verify vlib/v/parser/parse_type.v + ./v fmt -verify vlib/v/gen/json.v + + - name: v test-fmt run: ./v -silent test-fmt diff --git a/doc/docs.md b/doc/docs.md index 81a73444b6..3ecc69bfc4 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -158,7 +158,7 @@ fn add(x int, y int) int { return x + y } -fn sub(x, y int) int { +fn sub(x int, y int) int { return x - y } ``` @@ -644,7 +644,7 @@ fn main() { println('Hello, $name!') } ``` -This program can use any public definitions from the `os` module, such +This program can use any public definitions from the `os` module, such as the `input` function. See the [standard library](https://modules.vlang.io/) documentation for a list of common modules and their public symbols. @@ -1466,7 +1466,7 @@ fn main() { ### `init` functions -If you want a module to automatically call some setup/initialization code when it is imported, +If you want a module to automatically call some setup/initialization code when it is imported, you can use a module `init` function: ```v @@ -1475,7 +1475,7 @@ fn init() { } ``` -The `init` function cannot be public - it will be called automatically. This feature is +The `init` function cannot be public - it will be called automatically. This feature is particularly useful for initializing a C library. ## Types 2 @@ -2044,15 +2044,15 @@ performance. ### Asserts -```v -mut v := 2 +```v +mut v := 2 foo(mut v) -assert v < 4 -``` -An `assert` statement checks that its expression evaluates to `true`. If an assert fails, +assert v < 4 +``` +An `assert` statement checks that its expression evaluates to `true`. If an assert fails, the program will abort. Asserts should only be used to detect programming errors. When an -assert fails it is reported to *stderr*, and the values on each side of a comparison operator -(such as `<`, `==`) will be printed when possible. This is useful to easily find an +assert fails it is reported to *stderr*, and the values on each side of a comparison operator +(such as `<`, `==`) will be printed when possible. This is useful to easily find an unexpected value. Assert statements can be used in any function. ### Test files @@ -2074,8 +2074,8 @@ fn test_hello() { assert hello() == 'Hello world' } ``` -To run the test above, use `v hello_test.v`. This will check that the function `hello` is -producing the correct output. V executes all test functions in the file. +To run the test above, use `v hello_test.v`. This will check that the function `hello` is +producing the correct output. V executes all test functions in the file. * All test functions have to be inside a test file whose name ends in `_test.v`. * Test function names must begin with `test_` to mark them for execution. @@ -2084,15 +2084,15 @@ producing the correct output. V executes all test functions in the file. * There are 2 kinds of tests: external and internal. * The internal tests, have to *declare* their module, just like all other .v files from the same module. Internal tests can call even private functions in -the same module. -* The external tests, have to *import* the modules which they test. They do not -have access to the private functions/types of the modules. They can test only +the same module. +* The external tests, have to *import* the modules which they test. They do not +have access to the private functions/types of the modules. They can test only the external/public API that a module provides. In the example above, `test_hello` is an internal test, that can call the private function `hello()` because `hello_test.v` has `module main`, -just like `hello.v`, i.e. both are part of the same module. Note also that -since `module main` is a regular module like the others, internal tests can +just like `hello.v`, i.e. both are part of the same module. Note also that +since `module main` is a regular module like the others, internal tests can be used to test private functions in your main program .v files too. You can also define special test functions in a test file: @@ -2103,8 +2103,8 @@ You can also define special test functions in a test file: To run test functions in an individual test file, use `v foo_test.v`. -To test an entire module, use `v test mymodule`. You can also use `v test .` to test -everything inside your current folder (and subfolders). You can pass the `-stats` +To test an entire module, use `v test mymodule`. You can also use `v test .` to test +everything inside your current folder (and subfolders). You can pass the `-stats` option to see more details about the individual tests run. ## Memory management diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 4b4e3bdabb..5f1adfeae9 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -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 } diff --git a/vlib/builtin/builtin.v b/vlib/builtin/builtin.v index 65f16476ba..46698c4e2d 100644 --- a/vlib/builtin/builtin.v +++ b/vlib/builtin/builtin.v @@ -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') } diff --git a/vlib/builtin/float.v b/vlib/builtin/float.v index d990c1bb4a..e53fa0a38b 100644 --- a/vlib/builtin/float.v +++ b/vlib/builtin/float.v @@ -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 { diff --git a/vlib/builtin/map.v b/vlib/builtin/map.v index 0c6c0f82bf..89416b0c68 100644 --- a/vlib/builtin/map.v +++ b/vlib/builtin/map.v @@ -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 { diff --git a/vlib/builtin/sorted_map.v b/vlib/builtin/sorted_map.v index 53d24b3197..a7bd62d700 100644 --- a/vlib/builtin/sorted_map.v +++ b/vlib/builtin/sorted_map.v @@ -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 { diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index cca6a772f9..d15247c292 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -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)') diff --git a/vlib/hash/wyhash.v b/vlib/hash/wyhash.v index 46b18b76b0..f61ff4f522 100644 --- a/vlib/hash/wyhash.v +++ b/vlib/hash/wyhash.v @@ -29,7 +29,7 @@ const ( ) [inline] -pub fn wyhash_c(key byteptr, len, seed u64) u64 { +pub fn wyhash_c(key byteptr, len u64, seed u64) u64 { return C.wyhash(key, len, seed) } @@ -44,7 +44,7 @@ pub fn sum64(key []byte, seed u64) u64 { } [inline] -fn wyhash64(key byteptr, len, seed_ u64) u64 { +fn wyhash64(key byteptr, len u64, seed_ u64) u64 { if len == 0 { return 0 } @@ -96,7 +96,7 @@ fn wyrotr(v u64, k u32) u64 { } [inline] -pub fn wymum(a, b u64) u64 { +pub fn wymum(a u64, b u64) u64 { /* mut r := u128(a) r = r*b diff --git a/vlib/math/bits/bits.v b/vlib/math/bits/bits.v index 7c5c96e40f..b61504a64f 100644 --- a/vlib/math/bits/bits.v +++ b/vlib/math/bits/bits.v @@ -4,17 +4,14 @@ module bits const ( -// See http://supertech.csail.mit.edu/papers/debruijn.pdf - de_bruijn32 = u32(0x077CB531) - de_bruijn32tab = [byte(0), 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, - 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9, - ] - de_bruijn64 = u64(0x03f79d71b4ca8b09) - de_bruijn64tab = [byte(0), 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4, - 62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5, - 63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11, - 54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6, - ] + // See http://supertech.csail.mit.edu/papers/debruijn.pdf + de_bruijn32 = u32(0x077CB531) + de_bruijn32tab = [byte(0), 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, + 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9] + de_bruijn64 = u64(0x03f79d71b4ca8b09) + de_bruijn64tab = [byte(0), 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4, 62, 47, + 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5, 63, 55, 48, 27, 60, 41, 37, 16, 46, 35, + 44, 21, 52, 32, 23, 11, 54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6] ) const ( @@ -64,7 +61,7 @@ pub fn trailing_zeros_16(x u16) int { return 16 } // see comment in trailing_zeros_64 - return int(de_bruijn32tab[u32(x & -x) * de_bruijn32>>(32 - 5)]) + return int(de_bruijn32tab[u32(x & -x) * de_bruijn32 >> (32 - 5)]) } // trailing_zeros_32 returns the number of trailing zero bits in x; the result is 32 for x == 0. @@ -73,7 +70,7 @@ pub fn trailing_zeros_32(x u32) int { return 32 } // see comment in trailing_zeros_64 - return int(de_bruijn32tab[(x & -x) * de_bruijn32>>(32 - 5)]) + return int(de_bruijn32tab[(x & -x) * de_bruijn32 >> (32 - 5)]) } // trailing_zeros_64 returns the number of trailing zero bits in x; the result is 64 for x == 0. @@ -92,11 +89,10 @@ pub fn trailing_zeros_64(x u64) int { // find by how many bits it was shifted by looking at which six bit // substring ended up at the top of the word. // (Knuth, volume 4, section 7.3.1) - return int(de_bruijn64tab[(x & -x) * de_bruijn64>>(64 - 6)]) + return int(de_bruijn64tab[(x & -x) * de_bruijn64 >> (64 - 6)]) } // --- OnesCount --- - // ones_count_8 returns the number of one bits ("population count") in x. pub fn ones_count_8(x byte) int { return int(pop_8_tab[x]) @@ -104,12 +100,13 @@ pub fn ones_count_8(x byte) int { // ones_count_16 returns the number of one bits ("population count") in x. pub fn ones_count_16(x u16) int { - return int(pop_8_tab[x>>8] + pop_8_tab[x & u16(0xff)]) + return int(pop_8_tab[x >> 8] + pop_8_tab[x & u16(0xff)]) } // ones_count_32 returns the number of one bits ("population count") in x. pub fn ones_count_32(x u32) int { - return int(pop_8_tab[x>>24] + pop_8_tab[x>>16 & 0xff] + pop_8_tab[x>>8 & 0xff] + pop_8_tab[x & u32(0xff)]) + return int(pop_8_tab[x >> 24] + pop_8_tab[x >> 16 & 0xff] + pop_8_tab[x >> 8 & 0xff] + pop_8_tab[x & + u32(0xff)]) } // ones_count_64 returns the number of one bits ("population count") in x. @@ -133,17 +130,16 @@ pub fn ones_count_64(x u64) int { // Per "Hacker's Delight", the first line can be simplified // more, but it saves at best one instruction, so we leave // it alone for clarity. - mut y := (x>>u64(1) & (m0 & max_u64)) + (x & (m0 & max_u64)) - y = (y>>u64(2) & (m1 & max_u64)) + (y & (m1 & max_u64)) - y = ((y>>4) + y) & (m2 & max_u64) - y += y>>8 - y += y>>16 - y += y>>32 - return int(y) & ((1<<7) - 1) + mut y := (x >> u64(1) & (m0 & max_u64)) + (x & (m0 & max_u64)) + y = (y >> u64(2) & (m1 & max_u64)) + (y & (m1 & max_u64)) + y = ((y >> 4) + y) & (m2 & max_u64) + y += y >> 8 + y += y >> 16 + y += y >> 32 + return int(y) & ((1 << 7) - 1) } // --- RotateLeft --- - // rotate_left_8 returns the value of x rotated left by (k mod 8) bits. // To rotate x right by k bits, call rotate_left_8(x, -k). // @@ -152,7 +148,7 @@ pub fn ones_count_64(x u64) int { pub fn rotate_left_8(x byte, k int) byte { n := byte(8) s := byte(k) & (n - byte(1)) - return ((x<>(n - s))) + return ((x << s) | (x >> (n - s))) } // rotate_left_16 returns the value of x rotated left by (k mod 16) bits. @@ -163,7 +159,7 @@ pub fn rotate_left_8(x byte, k int) byte { pub fn rotate_left_16(x u16, k int) u16 { n := u16(16) s := u16(k) & (n - u16(1)) - return ((x<>(n - s))) + return ((x << s) | (x >> (n - s))) } // rotate_left_32 returns the value of x rotated left by (k mod 32) bits. @@ -174,7 +170,7 @@ pub fn rotate_left_16(x u16, k int) u16 { pub fn rotate_left_32(x u32, k int) u32 { n := u32(32) s := u32(k) & (n - u32(1)) - return ((x<>(n - s))) + return ((x << s) | (x >> (n - s))) } // rotate_left_64 returns the value of x rotated left by (k mod 64) bits. @@ -185,11 +181,10 @@ pub fn rotate_left_32(x u32, k int) u32 { pub fn rotate_left_64(x u64, k int) u64 { n := u64(64) s := u64(k) & (n - u64(1)) - return ((x<>(n - s))) + return ((x << s) | (x >> (n - s))) } // --- Reverse --- - // reverse_8 returns the value of x with its bits in reversed order. [inline] pub fn reverse_8(x byte) byte { @@ -199,35 +194,34 @@ pub fn reverse_8(x byte) byte { // reverse_16 returns the value of x with its bits in reversed order. [inline] pub fn reverse_16(x u16) u16 { - return u16(rev_8_tab[x>>8]) | (u16(rev_8_tab[x & u16(0xff)])<<8) + return u16(rev_8_tab[x >> 8]) | (u16(rev_8_tab[x & u16(0xff)]) << 8) } // reverse_32 returns the value of x with its bits in reversed order. [inline] pub fn reverse_32(x u32) u32 { - mut y := ((x>>u32(1) & (m0 & max_u32)) | ((x & (m0 & max_u32))<<1)) - y = ((y>>u32(2) & (m1 & max_u32)) | ((y & (m1 & max_u32))<>u32(4) & (m2 & max_u32)) | ((y & (m2 & max_u32))<> u32(1) & (m0 & max_u32)) | ((x & (m0 & max_u32)) << 1)) + y = ((y >> u32(2) & (m1 & max_u32)) | ((y & (m1 & max_u32)) << u32(2))) + y = ((y >> u32(4) & (m2 & max_u32)) | ((y & (m2 & max_u32)) << u32(4))) return reverse_bytes_32(u32(y)) } // reverse_64 returns the value of x with its bits in reversed order. [inline] pub fn reverse_64(x u64) u64 { - mut y := ((x>>u64(1) & (m0 & max_u64)) | ((x & (m0 & max_u64))<<1)) - y = ((y>>u64(2) & (m1 & max_u64)) | ((y & (m1 & max_u64))<<2)) - y = ((y>>u64(4) & (m2 & max_u64)) | ((y & (m2 & max_u64))<<4)) + mut y := ((x >> u64(1) & (m0 & max_u64)) | ((x & (m0 & max_u64)) << 1)) + y = ((y >> u64(2) & (m1 & max_u64)) | ((y & (m1 & max_u64)) << 2)) + y = ((y >> u64(4) & (m2 & max_u64)) | ((y & (m2 & max_u64)) << 4)) return reverse_bytes_64(y) } // --- ReverseBytes --- - // reverse_bytes_16 returns the value of x with its bytes in reversed order. // // This function's execution time does not depend on the inputs. [inline] pub fn reverse_bytes_16(x u16) u16 { - return (x>>8) | (x<<8) + return (x >> 8) | (x << 8) } // reverse_bytes_32 returns the value of x with its bytes in reversed order. @@ -235,8 +229,8 @@ pub fn reverse_bytes_16(x u16) u16 { // This function's execution time does not depend on the inputs. [inline] pub fn reverse_bytes_32(x u32) u32 { - y := ((x>>u32(8) & (m3 & max_u32)) | ((x & (m3 & max_u32))<>16) | (y<<16)) + y := ((x >> u32(8) & (m3 & max_u32)) | ((x & (m3 & max_u32)) << u32(8))) + return u32((y >> 16) | (y << 16)) } // reverse_bytes_64 returns the value of x with its bytes in reversed order. @@ -244,13 +238,12 @@ pub fn reverse_bytes_32(x u32) u32 { // This function's execution time does not depend on the inputs. [inline] pub fn reverse_bytes_64(x u64) u64 { - mut y := ((x>>u64(8) & (m3 & max_u64)) | ((x & (m3 & max_u64))<>u64(16) & (m4 & max_u64)) | ((y & (m4 & max_u64))<>32) | (y<<32) + mut y := ((x >> u64(8) & (m3 & max_u64)) | ((x & (m3 & max_u64)) << u64(8))) + y = ((y >> u64(16) & (m4 & max_u64)) | ((y & (m4 & max_u64)) << u64(16))) + return (y >> 32) | (y << 32) } // --- Len --- - // len_8 returns the minimum number of bits required to represent x; the result is 0 for x == 0. pub fn len_8(x byte) int { return int(len_8_tab[x]) @@ -260,7 +253,7 @@ pub fn len_8(x byte) int { pub fn len_16(x u16) int { mut y := x mut n := 0 - if y >= 1<<8 { + if y >= 1 << 8 { y >>= 8 n = 8 } @@ -271,11 +264,11 @@ pub fn len_16(x u16) int { pub fn len_32(x u32) int { mut y := x mut n := 0 - if y >= (1<<16) { + if y >= (1 << 16) { y >>= 16 n = 16 } - if y >= (1<<8) { + if y >= (1 << 8) { y >>= 8 n += 8 } @@ -286,15 +279,15 @@ pub fn len_32(x u32) int { pub fn len_64(x u64) int { mut y := x mut n := 0 - if y >= u64(1)<= u64(1) << u64(32) { y >>= 32 n = 32 } - if y >= u64(1)<= u64(1) << u64(16) { y >>= 16 n += 16 } - if y >= u64(1)<= u64(1) << u64(8) { y >>= 8 n += 8 } @@ -302,12 +295,10 @@ pub fn len_64(x u64) int { } // --- Add with carry --- - // Add returns the sum with carry of x, y and carry: sum = x + y + carry. // The carry input must be 0 or 1; otherwise the behavior is undefined. // The carryOut output is guaranteed to be 0 or 1. // - // add_32 returns the sum with carry of x, y and carry: sum = x + y + carry. // The carry input must be 0 or 1; otherwise the behavior is undefined. // The carryOut output is guaranteed to be 0 or 1. @@ -316,7 +307,7 @@ pub fn len_64(x u64) int { pub fn add_32(x u32, y u32, carry u32) (u32, u32) { sum64 := u64(x) + u64(y) + u64(carry) sum := u32(sum64) - carry_out := u32(sum64>>32) + carry_out := u32(sum64 >> 32) return sum, carry_out } @@ -330,17 +321,15 @@ pub fn add_64(x u64, y u64, carry u64) (u64, u64) { // The sum will overflow if both top bits are set (x & y) or if one of them // is (x | y), and a carry from the lower place happened. If such a carry // happens, the top bit will be 1 + 0 + 1 = 0 (&^ sum). - carry_out := ((x & y) | ((x | y) & ~sum ))>>63 + carry_out := ((x & y) | ((x | y) & ~sum)) >> 63 return sum, carry_out } // --- Subtract with borrow --- - // Sub returns the difference of x, y and borrow: diff = x - y - borrow. // The borrow input must be 0 or 1; otherwise the behavior is undefined. // The borrowOut output is guaranteed to be 0 or 1. // - // sub_32 returns the difference of x, y and borrow, diff = x - y - borrow. // The borrow input must be 0 or 1; otherwise the behavior is undefined. // The borrowOut output is guaranteed to be 0 or 1. @@ -352,7 +341,7 @@ pub fn sub_32(x u32, y u32, borrow u32) (u32, u32) { // bit of y is set (^x & y) or if they are the same (^(x ^ y)) and a borrow // from the lower place happens. If that borrow happens, the result will be // 1 - 1 - 1 = 0 - 0 - 1 = 1 (& diff). - borrow_out := ((~x & y) | (~(x ^ y) & diff))>>31 + borrow_out := ((~x & y) | (~(x ^ y) & diff)) >> 31 return diff, borrow_out } @@ -364,17 +353,16 @@ pub fn sub_32(x u32, y u32, borrow u32) (u32, u32) { pub fn sub_64(x u64, y u64, borrow u64) (u64, u64) { diff := x - y - borrow // See Sub32 for the bit logic. - borrow_out := ((~x & y) | (~(x ^ y) & diff))>>63 + borrow_out := ((~x & y) | (~(x ^ y) & diff)) >> 63 return diff, borrow_out } // --- Full-width multiply --- - const ( - two32 = u64(0x1_0000_0000) - mask32 = two32 - 1 - overflow_error = "Overflow Error" - divide_error = "Divide Error" + two32 = u64(0x100000000) + mask32 = two32 - 1 + overflow_error = 'Overflow Error' + divide_error = 'Divide Error' ) // mul_32 returns the 64-bit product of x and y: (hi, lo) = x * y @@ -384,7 +372,7 @@ const ( // This function's execution time does not depend on the inputs. pub fn mul_32(x u32, y u32) (u32, u32) { tmp := u64(x) * u64(y) - hi := u32(tmp>>32) + hi := u32(tmp >> 32) lo := u32(tmp) return hi, lo } @@ -396,21 +384,20 @@ pub fn mul_32(x u32, y u32) (u32, u32) { // This function's execution time does not depend on the inputs. pub fn mul_64(x u64, y u64) (u64, u64) { x0 := x & mask32 - x1 := x>>32 + x1 := x >> 32 y0 := y & mask32 - y1 := y>>32 + y1 := y >> 32 w0 := x0 * y0 - t := x1*y0 + (w0>>32) + t := x1 * y0 + (w0 >> 32) mut w1 := t & mask32 - w2 := t>>32 + w2 := t >> 32 w1 += x0 * y1 - hi := x1*y1 + w2 + (w1>>32) + hi := x1 * y1 + w2 + (w1 >> 32) lo := x * y return hi, lo } // --- Full-width divide --- - // div_32 returns the quotient and remainder of (hi, lo) divided by y: // quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper // half in parameter hi and the lower half in parameter lo. @@ -419,9 +406,9 @@ pub fn div_32(hi u32, lo u32, y u32) (u32, u32) { if y != 0 && y <= hi { panic(overflow_error) } - z := (u64(hi)<<32) | u64(lo) - quo := u32(z/u64(y)) - rem := u32(z%u64(y)) + z := (u64(hi) << 32) | u64(lo) + quo := u32(z / u64(y)) + rem := u32(z % u64(y)) return quo, rem } @@ -437,59 +424,53 @@ pub fn div_64(hi u64, lo u64, y1 u64) (u64, u64) { if y <= hi { panic(overflow_error) } - s := u32(leading_zeros_64(y)) y <<= s - - yn1 := y>>32 + yn1 := y >> 32 yn0 := y & mask32 - un32 := (hi<>(64-s)) - un10 := lo<>32 + un32 := (hi << s) | (lo >> (64 - s)) + un10 := lo << s + un1 := un10 >> 32 un0 := un10 & mask32 mut q1 := un32 / yn1 - mut rhat := un32 - q1*yn1 - - for q1 >= two32 || q1*yn0 > two32*rhat+un1 { + mut rhat := un32 - q1 * yn1 + for q1 >= two32 || q1 * yn0 > two32 * rhat + un1 { q1-- rhat += yn1 if rhat >= two32 { break } } - - un21 := un32*two32 + un1 - q1*y + un21 := un32 * two32 + un1 - q1 * y mut q0 := un21 / yn1 - rhat = un21 - q0*yn1 - - for q0 >= two32 || q0*yn0 > two32*rhat+un0 { + rhat = un21 - q0 * yn1 + for q0 >= two32 || q0 * yn0 > two32 * rhat + un0 { q0-- rhat += yn1 if rhat >= two32 { break } } - - return q1*two32 + q0, (un21*two32 + un0 - q0*y)>>s + return q1 * two32 + q0, (un21 * two32 + un0 - q0 * y) >> s } // rem_32 returns the remainder of (hi, lo) divided by y. Rem32 panics // for y == 0 (division by zero) but, unlike Div32, it doesn't panic // on a quotient overflow. pub fn rem_32(hi u32, lo u32, y u32) u32 { - return u32(((u64(hi)<<32) | u64(lo)) % u64(y)) + return u32(((u64(hi) << 32) | u64(lo)) % u64(y)) } // rem_64 returns the remainder of (hi, lo) divided by y. Rem64 panics // for y == 0 (division by zero) but, unlike div_64, it doesn't panic // on a quotient overflow. -pub fn rem_64(hi, lo, y u64) u64 { +pub fn rem_64(hi u64, lo u64, y u64) u64 { // We scale down hi so that hi < y, then use div_64 to compute the // rem with the guarantee that it won't panic on quotient overflow. // Given that - // hi ≡ hi%y (mod y) + // hi ≡ hi%y (mod y) // we have - // hi<<64 + lo ≡ (hi%y)<<64 + lo (mod y) - _, rem := div_64(hi%y, lo, y) + // hi<<64 + lo ≡ (hi%y)<<64 + lo (mod y) + _, rem := div_64(hi % y, lo, y) return rem } diff --git a/vlib/os/file.v b/vlib/os/file.v index 45b824c95e..6ba940dcc0 100644 --- a/vlib/os/file.v +++ b/vlib/os/file.v @@ -70,7 +70,7 @@ pub fn (mut f File) write_bytes(data voidptr, size int) int { return C.fwrite(data, 1, size, f.cfile) } -pub fn (mut f File) write_bytes_at(data voidptr, size, pos int) int { +pub fn (mut f File) write_bytes_at(data voidptr, size int, pos int) int { C.fseek(f.cfile, pos, C.SEEK_SET) res := C.fwrite(data, 1, size, f.cfile) C.fseek(f.cfile, 0, C.SEEK_END) @@ -84,7 +84,7 @@ pub fn (f &File) read_bytes(size int) []byte { } // read_bytes_at reads bytes at the given position in the file -pub fn (f &File) read_bytes_at(size, pos int) []byte { +pub fn (f &File) read_bytes_at(size int, pos int) []byte { mut arr := []byte{len: size} nreadbytes := f.read_bytes_into(pos, mut arr) or { // return err diff --git a/vlib/os/os.v b/vlib/os/os.v index e1c875e1fc..14b6c0ccdf 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -57,7 +57,7 @@ pub fn read_file(path string) ?string { } } -/***************************** OS ops ************************/ +//***************************** OS ops ************************ // file_size returns the size of the file located in `path`. pub fn file_size(path string) int { mut s := C.stat{} @@ -76,7 +76,7 @@ pub fn file_size(path string) int { } // mv moves files or folders from `src` to `dst`. -pub fn mv(src, dst string) { +pub fn mv(src string, dst string) { mut rdst := dst if is_dir(rdst) { rdst = join_path(rdst.trim_right(path_separator),file_name(src.trim_right(path_separator))) @@ -91,7 +91,7 @@ pub fn mv(src, dst string) { } // cp copies files or folders from `src` to `dst`. -pub fn cp(src, dst string) ? { +pub fn cp(src string, dst string) ? { $if windows { w_src := src.replace('/', '\\') w_dst := dst.replace('/', '\\') @@ -135,14 +135,14 @@ pub fn cp(src, dst string) ? { } [deprecated] -pub fn cp_r(osource_path, odest_path string, overwrite bool) ? { +pub fn cp_r(osource_path string, odest_path string, overwrite bool) ? { eprintln('warning: `os.cp_r` has been deprecated, use `os.cp_all` instead') return cp_all(osource_path, odest_path, overwrite) } // cp_all will recursively copy `src` to `dst`, // optionally overwriting files or dirs in `dst`. -pub fn cp_all(src, dst string, overwrite bool) ? { +pub fn cp_all(src string, dst string, overwrite bool) ? { source_path := os.real_path(src) dest_path := os.real_path(dst) if !os.exists(source_path) { @@ -190,7 +190,7 @@ pub fn mv_by_cp(source string, target string) ? { // vfopen returns an opened C file, given its path and open mode. // NB: os.vfopen is useful for compatibility with C libraries, that expect `FILE *`. // If you write pure V code, os.create or os.open are more convenient. -pub fn vfopen(path, mode string) ?&C.FILE { +pub fn vfopen(path string, mode string) ?&C.FILE { if path.len == 0 { return error('vfopen called with ""') } @@ -858,7 +858,7 @@ pub fn home_dir() string { } // write_file writes `text` data to a file in `path`. -pub fn write_file(path, text string) ? { +pub fn write_file(path string, text string) ? { mut f := os.create(path)? f.write(text) f.close() @@ -1164,7 +1164,7 @@ pub fn join_path(base string, dirs ...string) string { } // walk_ext returns a recursive list of all files in `path` ending with `ext`. -pub fn walk_ext(path, ext string) []string { +pub fn walk_ext(path string, ext string) []string { if !os.is_dir(path) { return [] } diff --git a/vlib/os/os_nix.c.v b/vlib/os/os_nix.c.v index 0df1e574af..a46e53394d 100644 --- a/vlib/os/os_nix.c.v +++ b/vlib/os/os_nix.c.v @@ -159,7 +159,7 @@ pub fn exec(cmd string) ?Result { } } -pub fn symlink(origin, target string) ?bool { +pub fn symlink(origin string, target string) ?bool { res := C.symlink(charptr(origin.str), charptr(target.str)) if res == 0 { return true diff --git a/vlib/strconv/format.v b/vlib/strconv/format.v index 52292c5804..7af346a90b 100644 --- a/vlib/strconv/format.v +++ b/vlib/strconv/format.v @@ -846,7 +846,7 @@ pub fn v_sprintf(str string, pt ... voidptr) string{ } [inline] -fn v_sprintf_panic( idx, len int) { +fn v_sprintf_panic(idx int, len int) { if idx >= len { panic('${idx+1} % conversion specifiers, but given only ${len} args') } diff --git a/vlib/strings/similarity.v b/vlib/strings/similarity.v index d57e4fa22e..e546732564 100644 --- a/vlib/strings/similarity.v +++ b/vlib/strings/similarity.v @@ -2,7 +2,7 @@ module strings // #-js // use levenshtein distance algorithm to calculate // the distance between between two strings (lower is closer) -pub fn levenshtein_distance(a, b string) int { +pub fn levenshtein_distance(a string, b string) int { mut f := [0].repeat(b.len + 1) for j in 0..f.len { f[j] = j @@ -29,7 +29,7 @@ pub fn levenshtein_distance(a, b string) int { // use levenshtein distance algorithm to calculate // how similar two strings are as a percentage (higher is closer) -pub fn levenshtein_distance_percentage(a, b string) f32 { +pub fn levenshtein_distance_percentage(a string, b string) f32 { d := levenshtein_distance(a, b) l := if a.len >= b.len { a.len } else { b.len } return (1.00 - f32(d) / f32(l)) * 100.00 @@ -38,7 +38,7 @@ pub fn levenshtein_distance_percentage(a, b string) f32 { // implementation of Sørensen–Dice coefficient. // find the similarity between two strings. // returns coefficient between 0.0 (not similar) and 1.0 (exact match). -pub fn dice_coefficient(s1, s2 string) f32 { +pub fn dice_coefficient(s1 string, s2 string) f32 { if s1.len == 0 || s2.len == 0 { return 0.0 } diff --git a/vlib/term/colors.v b/vlib/term/colors.v index 87f4c4e502..6430577541 100644 --- a/vlib/term/colors.v +++ b/vlib/term/colors.v @@ -3,28 +3,29 @@ // that can be found in the LICENSE file. module term -pub fn format(msg, open, close string) string { +pub fn format(msg string, open string, close string) string { return '\x1b[' + open + 'm' + msg + '\x1b[' + close + 'm' } -pub fn format_rgb(r, g, b int, msg, open, close string) string { - return '\x1b[' + open + ';2;' + r.str() + ';' + g.str() + ';' + b.str() + 'm' + msg + '\x1b[' + close + 'm' +pub fn format_rgb(r int, g int, b int, msg string, open string, close string) string { + return '\x1b[' + open + ';2;' + r.str() + ';' + g.str() + ';' + b.str() + 'm' + msg + '\x1b[' + + close + 'm' } -pub fn rgb(r, g, b int, msg string) string { +pub fn rgb(r int, g int, b int, msg string) string { return format_rgb(r, g, b, msg, '38', '39') } -pub fn bg_rgb(r, g, b int, msg string) string { +pub fn bg_rgb(r int, g int, b int, msg string) string { return format_rgb(r, g, b, msg, '48', '49') } pub fn hex(hex int, msg string) string { - return format_rgb(hex>>16, hex>>8 & 0xFF, hex & 0xFF, msg, '38', '39') + return format_rgb(hex >> 16, hex >> 8 & 0xFF, hex & 0xFF, msg, '38', '39') } pub fn bg_hex(hex int, msg string) string { - return format_rgb(hex>>16, hex>>8 & 0xFF, hex & 0xFF, msg, '48', '49') + return format_rgb(hex >> 16, hex >> 8 & 0xFF, hex & 0xFF, msg, '48', '49') } pub fn bg_black(msg string) string { @@ -190,4 +191,3 @@ pub fn yellow(msg string) string { pub fn bright_yellow(msg string) string { return format(msg, '93', '39') } - diff --git a/vlib/term/term.v b/vlib/term/term.v index ed776a319a..86b5195aef 100644 --- a/vlib/term/term.v +++ b/vlib/term/term.v @@ -4,7 +4,7 @@ import os const ( default_columns_size = 80 - default_rows_size = 25 + default_rows_size = 25 ) // Coord - used by term.get_cursor_position and term.set_cursor_position @@ -29,51 +29,70 @@ pub fn can_show_color_on_stderr() bool { // ok_message returns a colored string with green color. // If colors are not allowed, returns a given string. pub fn ok_message(s string) string { - return if can_show_color_on_stdout() { green(s) } else { s } + return if can_show_color_on_stdout() { + green(s) + } else { + s + } } // fail_message returns a colored string with red color. // If colors are not allowed, returns a given string. pub fn fail_message(s string) string { - return if can_show_color_on_stdout() { bold(bg_red(white(s))) } else { s } + return if can_show_color_on_stdout() { + bold(bg_red(white(s))) + } else { + s + } } // warn_message returns a colored string with yellow color. // If colors are not allowed, returns a given string. pub fn warn_message(s string) string { - return if can_show_color_on_stdout() { bright_yellow(s) } else { s } + return if can_show_color_on_stdout() { + bright_yellow(s) + } else { + s + } } // h_divider returns a horizontal divider line with a dynamic width, // that depends on the current terminal settings. // If an empty string is passed in, print enough spaces to make a new line pub fn h_divider(divider string) string { - cols,_ := get_terminal_size() - result := if divider.len > 0 { divider.repeat(1 + (cols / divider.len)) } else { " ".repeat(1 + cols) } + cols, _ := get_terminal_size() + result := if divider.len > 0 { divider.repeat(1 + (cols / divider.len)) } else { ' '.repeat(1 + + cols) } return result[0..cols] } // header returns a horizontal divider line with a centered text in the middle. // e.g: term.header('TEXT', '=') // =============== TEXT =============== -pub fn header(text, divider string) string { +pub fn header(text string, divider string) string { if text.len == 0 { return h_divider(divider) } - xcols,_ := get_terminal_size() - cols := imax(1, xcols) - tlimit := imax(1, if cols > text.len + 2 + 2 * divider.len { text.len } else { cols - 3 - 2 * divider.len }) + xcols, _ := get_terminal_size() + cols := imax(1, xcols) + tlimit := imax(1, if cols > text.len + 2 + 2 * divider.len { text.len } else { cols - 3 - + 2 * divider.len }) tlimit_alligned := if (tlimit % 2) != (cols % 2) { tlimit + 1 } else { tlimit } tstart := imax(0, (cols - tlimit_alligned) / 2) - ln := if divider.len > 0 { divider.repeat(1 + cols / divider.len)[0..cols] } else { " ".repeat(1 + cols) } + ln := if divider.len > 0 { divider.repeat(1 + cols / divider.len)[0..cols] } else { ' '.repeat(1 + + cols) } if ln.len == 1 { return ln + ' ' + text[0..tlimit] + ' ' + ln } return ln[0..tstart] + ' ' + text[0..tlimit] + ' ' + ln[tstart + tlimit + 2..cols] } -fn imax(x,y int) int { - return if x > y { x } else { y } +fn imax(x int, y int) int { + return if x > y { + x + } else { + y + } } fn supports_escape_sequences(fd int) bool { diff --git a/vlib/time/time.v b/vlib/time/time.v index 7b19726e3f..07953d3c6a 100644 --- a/vlib/time/time.v +++ b/vlib/time/time.v @@ -4,23 +4,22 @@ module time #include - const ( - days_string = 'MonTueWedThuFriSatSun' - month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] - months_string = 'JanFebMarAprMayJunJulAugSepOctNovDec' + days_string = 'MonTueWedThuFriSatSun' + month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + months_string = 'JanFebMarAprMayJunJulAugSepOctNovDec' // The unsigned zero year for internal calculations. // Must be 1 mod 400, and times before it will not compute correctly, // but otherwise can be changed at will. - absolute_zero_year = i64(-292277022399 )//as i64 + absolute_zero_year = i64(-292277022399) // as i64 seconds_per_minute = 60 - seconds_per_hour = 60 * seconds_per_minute - seconds_per_day = 24 * seconds_per_hour - seconds_per_week = 7 * seconds_per_day + seconds_per_hour = 60 * seconds_per_minute + seconds_per_day = 24 * seconds_per_hour + seconds_per_week = 7 * seconds_per_day days_per_400_years = 365 * 400 + 97 days_per_100_years = 365 * 100 + 24 - days_per_4_years = 365 * 4 + 1 - days_before = [ + days_per_4_years = 365 * 4 + 1 + days_before = [ 0, 31, 31 + 28, @@ -35,19 +34,19 @@ const ( 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31, ] - long_days= ['Monday', 'Tuesday', 'Wednesday', 'Thusday', 'Friday', 'Saturday', 'Sunday'] + long_days = ['Monday', 'Tuesday', 'Wednesday', 'Thusday', 'Friday', 'Saturday', 'Sunday'] ) pub struct Time { pub: - year int - month int - day int - hour int - minute int - second int - microsecond int - unix u64 + year int + month int + day int + hour int + minute int + second int + microsecond int + unix u64 } pub enum FormatTime { @@ -86,8 +85,8 @@ pub struct C.timeval { } fn C.localtime(t &C.time_t) &C.tm -fn C.time(t &C.time_t) C.time_t +fn C.time(t &C.time_t) C.time_t // now returns current local time. pub fn now() Time { @@ -154,7 +153,10 @@ pub fn new_time(t Time) Time { tm_year: t.year - 1900 } utime := u64(make_unix_time(tt)) - return { t | unix: utime } + return { + t | + unix: utime + } } // unix_time returns Unix time. @@ -166,7 +168,7 @@ pub fn (t Time) unix_time() int { // unix_time_milli returns Unix time with millisecond resolution. [inline] pub fn (t Time) unix_time_milli() u64 { - return t.unix * 1000 + u64(t.microsecond/1000) + return t.unix * 1000 + u64(t.microsecond / 1000) } // add_seconds returns a new time struct with an added number of seconds. @@ -189,7 +191,7 @@ fn since(t Time) int { // relative returns a string representation of difference between time // and current time. pub fn (t Time) relative() string { - znow := time.now() + znow := now() secs := znow.unix - t.unix if secs <= 30 { // right now or in the future @@ -200,21 +202,21 @@ pub fn (t Time) relative() string { return '1m' } if secs < 3600 { - m := secs/60 + m := secs / 60 if m == 1 { return '1 minute ago' } return '$m minutes ago' } if secs < 3600 * 24 { - h := secs/3600 + h := secs / 3600 if h == 1 { return '1 hour ago' } return '$h hours ago' } if secs < 3600 * 24 * 5 { - d:=secs/3600/24 + d := secs / 3600 / 24 if d == 1 { return '1 day ago' } @@ -227,7 +229,7 @@ pub fn (t Time) relative() string { } pub fn (t Time) relative_short() string { - znow := time.now() + znow := now() secs := znow.unix - t.unix if secs <= 30 { // right now or in the future @@ -254,7 +256,7 @@ pub fn (t Time) relative_short() string { // day_of_week returns the current day of a given year, month, and day, // as an integer. -pub fn day_of_week(y, m, d int) int { +pub fn day_of_week(y int, m int, d int) int { // Sakomotho's algorithm is explained here: // https://stackoverflow.com/a/6385934 t := [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4] @@ -330,7 +332,7 @@ pub fn is_leap_year(year int) bool { } // days_in_month returns a number of days in a given month. -pub fn days_in_month(month, year int) ?int { +pub fn days_in_month(month int, year int) ?int { if month > 12 || month < 1 { return error('Invalid month: $month') } @@ -362,7 +364,7 @@ fn convert_ctime(t C.tm, microsecond int) Time { // A lot of these are taken from the Go library pub type Duration = i64 -pub const( +pub const ( nanosecond = Duration(1) microsecond = Duration(1000) * nanosecond millisecond = Duration(1000) * microsecond @@ -373,34 +375,39 @@ pub const( ) // nanoseconds returns the duration as an integer number of nanoseconds. -pub fn (d Duration) nanoseconds() i64 { return i64(d) } +pub fn (d Duration) nanoseconds() i64 { + return i64(d) +} // microseconds returns the duration as an integer number of microseconds. -pub fn (d Duration) microseconds() i64 { return i64(d) / 1000 } +pub fn (d Duration) microseconds() i64 { + return i64(d) / 1000 +} // milliseconds returns the duration as an integer number of milliseconds. -pub fn (d Duration) milliseconds() i64 { return i64(d) / 1_000_000 } +pub fn (d Duration) milliseconds() i64 { + return i64(d) / 1000000 +} // The following functions return floating point numbers because it's common to // consider all of them in sub-one intervals - // seconds returns the duration as a floating point number of seconds. pub fn (d Duration) seconds() f64 { sec := d / second nsec := d % second - return f64(sec) + f64(nsec)/1e9 + return f64(sec) + f64(nsec) / 1e9 } // minutes returns the duration as a floating point number of minutes. pub fn (d Duration) minutes() f64 { min := d / minute nsec := d % minute - return f64(min) + f64(nsec)/(60*1e9) + return f64(min) + f64(nsec) / (60 * 1e9) } // hours returns the duration as a floating point number of hours. pub fn (d Duration) hours() f64 { hr := d / hour nsec := d % hour - return f64(hr) + f64(nsec)/(60*60*1e9) + return f64(hr) + f64(nsec) / (60 * 60 * 1e9) } diff --git a/vlib/v/ast/scope.v b/vlib/v/ast/scope.v index 78dfecd5e8..52c1ab4b38 100644 --- a/vlib/v/ast/scope.v +++ b/vlib/v/ast/scope.v @@ -149,7 +149,7 @@ fn (s &Scope) contains(pos int) bool { return pos >= s.start_pos && pos <= s.end_pos } -pub fn (sc &Scope) show(depth, max_depth int) string { +pub fn (sc &Scope) show(depth int, max_depth int) string { mut out := '' mut indent := '' for _ in 0 .. depth * 4 { diff --git a/vlib/v/builder/builder.v b/vlib/v/builder/builder.v index b32890268d..bb23519573 100644 --- a/vlib/v/builder/builder.v +++ b/vlib/v/builder/builder.v @@ -228,7 +228,7 @@ fn module_path(mod string) string { return mod.replace('.', os.path_separator) } -pub fn (b &Builder) find_module_path(mod, fpath string) ?string { +pub fn (b &Builder) find_module_path(mod string, fpath string) ?string { // support @VROOT/v.mod relative paths: mut mcache := vmod.get_cache() vmod_file_location := mcache.get_by_file(fpath) diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v index 4d4e10b511..23c86ce4e0 100644 --- a/vlib/v/builder/cc.v +++ b/vlib/v/builder/cc.v @@ -792,7 +792,7 @@ fn missing_compiler_info() string { return '' } -fn error_context_lines(text, keyword string, before, after int) []string { +fn error_context_lines(text string, keyword string, before int, after int) []string { khighlight := if term.can_show_color_on_stdout() { term.red(keyword) } else { keyword } mut eline_idx := 0 mut lines := text.split_into_lines() diff --git a/vlib/v/builder/compile.v b/vlib/v/builder/compile.v index d933ebaa47..950229dcfd 100644 --- a/vlib/v/builder/compile.v +++ b/vlib/v/builder/compile.v @@ -21,7 +21,7 @@ fn get_vtmp_folder() string { return vtmp } -fn get_vtmp_filename(base_file_name, postfix string) string { +fn get_vtmp_filename(base_file_name string, postfix string) string { vtmp := get_vtmp_folder() return os.real_path(os.join_path(vtmp, os.file_name(os.real_path(base_file_name)) + postfix)) } diff --git a/vlib/v/builder/msvc.v b/vlib/v/builder/msvc.v index 3a65bd9070..de92ab8032 100644 --- a/vlib/v/builder/msvc.v +++ b/vlib/v/builder/msvc.v @@ -97,7 +97,7 @@ fn find_windows_kit_root(host_arch string) ?WindowsKit { return error('Unable to find a windows kit') } kit_lib := kit_root + 'Lib' - files := os.ls(kit_lib)? + files := os.ls(kit_lib) ? mut highest_path := '' mut highest_int := 0 for f in files { @@ -128,7 +128,7 @@ struct VsInstallation { exe_path string } -fn find_vs(vswhere_dir, host_arch string) ?VsInstallation { +fn find_vs(vswhere_dir string, host_arch string) ?VsInstallation { $if !windows { return error('Host OS does not support finding a Visual Studio installation') } @@ -136,7 +136,7 @@ fn find_vs(vswhere_dir, host_arch string) ?VsInstallation { // VSWhere is guaranteed to be installed at this location now // If its not there then end user needs to update their visual studio // installation! - res := os.exec('"$vswhere_dir\\Microsoft Visual Studio\\Installer\\vswhere.exe" -latest -prerelease -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath')? + res := os.exec('"$vswhere_dir\\Microsoft Visual Studio\\Installer\\vswhere.exe" -latest -prerelease -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath') ? res_output := res.output.trim_right('\r\n') // println('res: "$res"') version := os.read_file('$res_output\\VC\\Auxiliary\\Build\\Microsoft.VCToolsVersion.default.txt') or { diff --git a/vlib/v/cflag/cflags.v b/vlib/v/cflag/cflags.v index 99c0389591..54a6d8ca63 100644 --- a/vlib/v/cflag/cflags.v +++ b/vlib/v/cflag/cflags.v @@ -22,7 +22,7 @@ pub fn (c &CFlag) str() string { pub fn (cf &CFlag) format() string { mut value := cf.value if cf.name in ['-l', '-Wa', '-Wl', '-Wp'] && value.len > 0 { - return '${cf.name}${value}'.trim_space() + return '$cf.name$value'.trim_space() } // convert to absolute path if cf.name == '-I' || cf.name == '-L' || value.ends_with('.o') { diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 7bc6896f87..87a8c835a9 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -7,7 +7,7 @@ import v.table import v.token import v.ast -pub fn (mut c Checker) check_basic(got, expected table.Type) bool { +pub fn (mut c Checker) check_basic(got table.Type, expected table.Type) bool { if got == expected { return true } @@ -111,7 +111,7 @@ pub fn (mut c Checker) check_basic(got, expected table.Type) bool { return false } -pub fn (mut c Checker) check_matching_function_symbols(got_type_sym, exp_type_sym &table.TypeSymbol) bool { +pub fn (mut c Checker) check_matching_function_symbols(got_type_sym &table.TypeSymbol, exp_type_sym &table.TypeSymbol) bool { got_info := got_type_sym.info as table.FnType exp_info := exp_type_sym.info as table.FnType got_fn := got_info.func @@ -142,7 +142,7 @@ pub fn (mut c Checker) check_matching_function_symbols(got_type_sym, exp_type_sy } [inline] -fn (mut c Checker) check_shift(left_type, right_type table.Type, left_pos, right_pos token.Position) table.Type { +fn (mut c Checker) check_shift(left_type table.Type, right_type table.Type, left_pos token.Position, right_pos token.Position) table.Type { if !left_type.is_int() { // maybe it's an int alias? TODO move this to is_int() ? sym := c.table.get_type_symbol(left_type) @@ -163,7 +163,7 @@ fn (mut c Checker) check_shift(left_type, right_type table.Type, left_pos, right return left_type } -pub fn (c &Checker) promote(left_type, right_type table.Type) table.Type { +pub fn (c &Checker) promote(left_type table.Type, right_type table.Type) table.Type { if left_type.is_ptr() || left_type.is_pointer() { if right_type.is_int() { return left_type @@ -190,7 +190,7 @@ pub fn (c &Checker) promote(left_type, right_type table.Type) table.Type { } } -fn (c &Checker) promote_num(left_type, right_type table.Type) table.Type { +fn (c &Checker) promote_num(left_type table.Type, right_type table.Type) table.Type { // sort the operands to save time mut type_hi := left_type mut type_lo := right_type @@ -235,7 +235,7 @@ fn (c &Checker) promote_num(left_type, right_type table.Type) table.Type { } // TODO: promote(), check_types(), symmetric_check() and check() overlap - should be rearranged -pub fn (mut c Checker) check_types(got, expected table.Type) bool { +pub fn (mut c Checker) check_types(got table.Type, expected table.Type) bool { if got == expected { return true } @@ -278,7 +278,7 @@ pub fn (mut c Checker) check_types(got, expected table.Type) bool { return true } -pub fn (mut c Checker) symmetric_check(left, right table.Type) bool { +pub fn (mut c Checker) symmetric_check(left table.Type, right table.Type) bool { // allow direct int-literal assignment for pointers for now // maybe in the future optionals should be used for that if right.is_ptr() || right.is_pointer() { @@ -295,7 +295,7 @@ pub fn (mut c Checker) symmetric_check(left, right table.Type) bool { return c.check_basic(left, right) } -pub fn (c &Checker) get_default_fmt(ftyp, typ table.Type) byte { +pub fn (c &Checker) get_default_fmt(ftyp table.Type, typ table.Type) byte { if typ.is_float() { return `g` } else if typ.is_signed() || typ.is_any_int() { @@ -315,7 +315,8 @@ pub fn (c &Checker) get_default_fmt(ftyp, typ table.Type) byte { } } if ftyp in [table.string_type, table.bool_type] || - sym.kind in [.enum_, .array, .array_fixed, .struct_, .map, .multi_return, .sum_type] || ftyp.has_flag(.optional) || + sym.kind in + [.enum_, .array, .array_fixed, .struct_, .map, .multi_return, .sum_type] || ftyp.has_flag(.optional) || sym.has_method('str') { return `s` } else { @@ -369,7 +370,7 @@ pub fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) table.T return table.string_type } -pub fn (c &Checker) check_sumtype_compatibility(a, b table.Type) bool { +pub fn (c &Checker) check_sumtype_compatibility(a table.Type, b table.Type) bool { return c.table.sumtype_has_variant(a, b) || c.table.sumtype_has_variant(b, a) } diff --git a/vlib/v/gen/auto_str_methods.v b/vlib/v/gen/auto_str_methods.v index e7a16ab6d6..ab39696d9b 100644 --- a/vlib/v/gen/auto_str_methods.v +++ b/vlib/v/gen/auto_str_methods.v @@ -69,7 +69,7 @@ fn (mut g Gen) gen_str_for_type_with_styp(typ table.Type, styp string) string { return str_fn_name } -fn (mut g Gen) gen_str_for_array(info table.Array, styp, str_fn_name string) { +fn (mut g Gen) gen_str_for_array(info table.Array, styp string, str_fn_name string) { sym := g.table.get_type_symbol(info.elem_type) field_styp := g.typ(info.elem_type) is_elem_ptr := info.elem_type.is_ptr() @@ -133,7 +133,7 @@ fn (mut g Gen) gen_str_for_array(info table.Array, styp, str_fn_name string) { g.auto_str_funcs.writeln('}') } -fn (mut g Gen) gen_str_for_array_fixed(info table.ArrayFixed, styp, str_fn_name string) { +fn (mut g Gen) gen_str_for_array_fixed(info table.ArrayFixed, styp string, str_fn_name string) { sym := g.table.get_type_symbol(info.elem_type) field_styp := g.typ(info.elem_type) is_elem_ptr := info.elem_type.is_ptr() @@ -179,7 +179,7 @@ fn (mut g Gen) gen_str_for_array_fixed(info table.ArrayFixed, styp, str_fn_name g.auto_str_funcs.writeln('}') } -fn (mut g Gen) gen_str_for_map(info table.Map, styp, str_fn_name string) { +fn (mut g Gen) gen_str_for_map(info table.Map, styp string, str_fn_name string) { key_sym := g.table.get_type_symbol(info.key_type) key_styp := g.typ(info.key_type) if !key_sym.has_method('str') { @@ -224,7 +224,7 @@ fn (mut g Gen) gen_str_for_map(info table.Map, styp, str_fn_name string) { g.auto_str_funcs.writeln('}') } -fn (mut g Gen) gen_str_for_varg(styp, str_fn_name string, has_str_method bool) { +fn (mut g Gen) gen_str_for_varg(styp string, str_fn_name string, has_str_method bool) { g.definitions.writeln('string varg_${str_fn_name}(varg_$styp it); // auto') g.auto_str_funcs.writeln('string varg_${str_fn_name}(varg_$styp it) {') g.auto_str_funcs.writeln('\tstrings__Builder sb = strings__new_builder(it.len);') @@ -240,7 +240,7 @@ fn (mut g Gen) gen_str_for_varg(styp, str_fn_name string, has_str_method bool) { g.auto_str_funcs.writeln('}') } -fn (mut g Gen) gen_str_for_multi_return(info table.MultiReturn, styp, str_fn_name string) { +fn (mut g Gen) gen_str_for_multi_return(info table.MultiReturn, styp string, str_fn_name string) { for typ in info.types { sym := g.table.get_type_symbol(typ) if !sym.has_method('str') { @@ -288,7 +288,7 @@ fn (mut g Gen) gen_str_for_multi_return(info table.MultiReturn, styp, str_fn_nam g.auto_str_funcs.writeln('}') } -fn (mut g Gen) gen_str_for_struct(info table.Struct, styp, str_fn_name string) { +fn (mut g Gen) gen_str_for_struct(info table.Struct, styp string, str_fn_name string) { // TODO: short it if possible // generates all definitions of substructs mut fnames2strfunc := { @@ -337,7 +337,7 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp, str_fn_name string) { } g.auto_str_funcs.writeln('\t\t"%.*s\\000 $field.name: $fmt\\n"') } - g.auto_str_funcs.write('\t\t"%.*s\\000}", ${2 * (info.fields.len + 1)}') + g.auto_str_funcs.write('\t\t"%.*s\\000}", ${2*(info.fields.len+1)}') if info.fields.len > 0 { g.auto_str_funcs.write(',\n\t\t') for i, field in info.fields { @@ -348,7 +348,6 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp, str_fn_name string) { field_styp = field_styp.replace('*', '') } field_styp_fn_name := if has_custom_str { '${field_styp}_str' } else { fnames2strfunc[field_styp] } - g.auto_str_funcs.write('indents, ') func := struct_auto_str_func(sym, field.typ, field_styp_fn_name, field.name) if field.typ.is_ptr() { @@ -360,7 +359,6 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp, str_fn_name string) { } } g.auto_str_funcs.write(func) - if i < info.fields.len - 1 { g.auto_str_funcs.write(',\n\t\t') } @@ -372,7 +370,7 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp, str_fn_name string) { g.auto_str_funcs.writeln('}') } -fn struct_auto_str_func(sym table.TypeSymbol, field_type table.Type, fn_name, field_name string) string { +fn struct_auto_str_func(sym table.TypeSymbol, field_type table.Type, fn_name string, field_name string) string { has_custom_str := sym.has_method('str') if sym.kind == .enum_ { return '${fn_name}(it->${c_name(field_name)})' @@ -409,7 +407,7 @@ fn struct_auto_str_func(sym table.TypeSymbol, field_type table.Type, fn_name, fi } } -fn (mut g Gen) gen_str_for_enum(info table.Enum, styp, str_fn_name string) { +fn (mut g Gen) gen_str_for_enum(info table.Enum, styp string, str_fn_name string) { s := util.no_dots(styp) g.type_definitions.writeln('string ${str_fn_name}($styp it); // auto') g.auto_str_funcs.writeln('string ${str_fn_name}($styp it) { /* gen_str_for_enum */') @@ -429,8 +427,8 @@ fn (mut g Gen) gen_str_for_enum(info table.Enum, styp, str_fn_name string) { g.auto_str_funcs.writeln('}') } -fn (mut g Gen) gen_str_for_sum_type(info table.SumType, styp, str_fn_name string) { - mut gen_fn_names := map[string]string +fn (mut g Gen) gen_str_for_sum_type(info table.SumType, styp string, str_fn_name string) { + mut gen_fn_names := map[string]string{} for typ in info.variants { sym := g.table.get_type_symbol(typ) if !sym.has_method('str') { @@ -453,14 +451,11 @@ fn (mut g Gen) gen_str_for_sum_type(info table.SumType, styp, str_fn_name string for typ in info.variants { mut value_fmt := '%.*s\\000' if typ == table.string_type { - value_fmt = '\'$value_fmt\'' + value_fmt = "\'$value_fmt\'" } typ_str := g.typ(typ) - mut func_name := if typ_str in gen_fn_names { - gen_fn_names[typ_str] - } else { - g.gen_str_for_type_with_styp(typ, typ_str) - } + mut func_name := if typ_str in gen_fn_names { gen_fn_names[typ_str] } else { g.gen_str_for_type_with_styp(typ, + typ_str) } sym := g.table.get_type_symbol(typ) if sym.kind == .struct_ { func_name = 'indent_$func_name' diff --git a/vlib/v/gen/json.v b/vlib/v/gen/json.v index bb6e931d39..d88c9fe6bd 100644 --- a/vlib/v/gen/json.v +++ b/vlib/v/gen/json.v @@ -74,7 +74,7 @@ $enc_fn_dec { // enc += g.encode_array(t) } else if sym.kind == .map { // Handle maps - m := sym.info as table.Map + m := sym.info as table.Map g.gen_json_for_type(m.key_type) g.gen_json_for_type(m.value_type) dec.writeln(g.decode_map(m.key_type, m.value_type)) @@ -156,7 +156,6 @@ fn is_js_prim(typ string) bool { fn (mut g Gen) decode_array(value_type table.Type) string { styp := g.typ(value_type) fn_name := js_dec_name(styp) - mut s := '' if is_js_prim(styp) { s = '$styp val = ${fn_name}(jsval); ' @@ -165,7 +164,7 @@ fn (mut g Gen) decode_array(value_type table.Type) string { Option_$styp val2 = $fn_name (jsval); if(!val2.ok) { array_free(&res); - return *(Option_array_${styp}*)&val2; + return *(Option_array_$styp*)&val2; } $styp val = *($styp*)val2.data; ' @@ -173,7 +172,7 @@ fn (mut g Gen) decode_array(value_type table.Type) string { return ' if(!cJSON_IsArray(root)) { Option err = v_error( string_add(tos_lit("Json element is not an array: "), tos2(cJSON_PrintUnformatted(root))) ); - return *(Option_array_${styp} *)&err; + return *(Option_array_$styp *)&err; } res = __new_array(0, 0, sizeof($styp)); const cJSON *jsval = NULL; @@ -196,13 +195,10 @@ fn (mut g Gen) encode_array(value_type table.Type) string { ' } -fn (mut g Gen) decode_map(key_type, value_type table.Type) string { - +fn (mut g Gen) decode_map(key_type table.Type, value_type table.Type) string { styp := g.typ(key_type) - styp_v := g.typ(value_type) fn_name_v := js_dec_name(styp_v) - mut s := '' if is_js_prim(styp_v) { s = '$styp_v val = $fn_name_v (js_get(root, jsval->string));' @@ -211,16 +207,15 @@ fn (mut g Gen) decode_map(key_type, value_type table.Type) string { Option_$styp_v val2 = $fn_name_v (js_get(root, jsval->string)); if(!val2.ok) { map_free(&res); - return *(Option_map_${styp}_${styp_v}*)&val2; + return *(Option_map_${styp}_$styp_v*)&val2; } $styp_v val = *($styp_v*)val2.data; ' } - return ' if(!cJSON_IsObject(root)) { Option err = v_error( string_add(tos_lit("Json element is not an object: "), tos2(cJSON_PrintUnformatted(root))) ); - return *(Option_map_${styp}_${styp_v} *)&err; + return *(Option_map_${styp}_$styp_v *)&err; } res = new_map_1(sizeof($styp_v)); cJSON *jsval = NULL; @@ -232,16 +227,12 @@ fn (mut g Gen) decode_map(key_type, value_type table.Type) string { ' } -fn (mut g Gen) encode_map(key_type, value_type table.Type) string { +fn (mut g Gen) encode_map(key_type table.Type, value_type table.Type) string { styp := g.typ(key_type) - styp_v := g.typ(value_type) fn_name_v := js_enc_name(styp_v) - zero := g.type_default(value_type) - keys_tmp := g.new_tmp_var() - mut key := 'string key = ' if key_type.is_string() { key += '(($styp*)${keys_tmp}.data)[i];' @@ -250,7 +241,6 @@ fn (mut g Gen) encode_map(key_type, value_type table.Type) string { // key += '${styp}_str((($styp*)${keys_tmp}.data)[i]);' verror('json: encode only maps with string keys') } - return ' o = cJSON_CreateObject(); array_$styp $keys_tmp = map_keys(&val); diff --git a/vlib/v/gen/sql.v b/vlib/v/gen/sql.v index 1c8568d0c0..eb1558c53d 100644 --- a/vlib/v/gen/sql.v +++ b/vlib/v/gen/sql.v @@ -38,7 +38,7 @@ fn (mut g Gen) sql_stmt(node ast.SqlStmt) { if field.name == 'id' { continue } - g.write('`${field.name}`') + g.write('`$field.name`') if i < node.fields.len - 1 { g.write(', ') } @@ -114,7 +114,7 @@ fn (mut g Gen) sql_select_expr(node ast.SqlExpr) { } else { // `select id, name, country from User` for i, field in node.fields { - sql_query += '`${field.name}`' + sql_query += '`$field.name`' if i < node.fields.len - 1 { sql_query += ', ' } @@ -188,7 +188,7 @@ fn (mut g Gen) sql_select_expr(node ast.SqlExpr) { info := sym.info as table.Struct for i, field in info.fields { g.zero_struct_field(field) - if i != info.fields.len-1 { + if i != info.fields.len - 1 { g.write(', ') } } @@ -203,7 +203,7 @@ fn (mut g Gen) sql_select_expr(node ast.SqlExpr) { info := sym.info as table.Struct for i, field in info.fields { g.zero_struct_field(field) - if i != info.fields.len-1 { + if i != info.fields.len - 1 { g.write(', ') } } @@ -246,7 +246,7 @@ fn (mut g Gen) sql_bind_int(val string) { g.sql_buf.writeln('sqlite3_bind_int($g.sql_stmt_name, $g.sql_i, $val);') } -fn (mut g Gen) sql_bind_string(val, len string) { +fn (mut g Gen) sql_bind_string(val string, len string) { g.sql_buf.writeln('sqlite3_bind_text($g.sql_stmt_name, $g.sql_i, $val, $len, 0);') } diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 0785e976fd..bff633c368 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -500,6 +500,10 @@ fn (mut p Parser) fn_args() ([]table.Param, bool, bool) { mut arg_names := [p.check_name()] // `a, b, c int` for p.tok.kind == .comma { + if !p.pref.is_fmt { + p.warn('`fn f(x, y Type)` syntax has been deprecated and will soon be removed. ' + + 'Use `fn f(x Type, y Type)` instead. You can run `v fmt -w file.v` to automatically fix your code.') + } p.next() arg_pos << p.tok.position() arg_names << p.check_name() diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index 54e882c008..c7a6525ee5 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -189,7 +189,7 @@ If you need to modify an array in a function, use a mutable argument instead: `f return typ } -pub fn (mut p Parser) parse_any_type(language table.Language, is_ptr, check_dot bool) table.Type { +pub fn (mut p Parser) parse_any_type(language table.Language, is_ptr bool, check_dot bool) table.Type { mut name := p.tok.lit if language == .c { name = 'C.$name' diff --git a/vlib/v/table/atypes.v b/vlib/v/table/atypes.v index 6433863995..40352238dc 100644 --- a/vlib/v/table/atypes.v +++ b/vlib/v/table/atypes.v @@ -82,7 +82,7 @@ pub fn (t Type) atomic_typename() string { } } -pub fn sharetype_from_flags(is_shared, is_atomic bool) ShareType { +pub fn sharetype_from_flags(is_shared bool, is_atomic bool) ShareType { return ShareType((int(is_atomic) << 1) | int(is_shared)) } @@ -187,7 +187,7 @@ pub fn new_type(idx int) Type { // return new type with TypeSymbol idx set to `idx` & nr_muls set to `nr_muls` [inline] -pub fn new_type_ptr(idx, nr_muls int) Type { +pub fn new_type_ptr(idx int, nr_muls int) Type { if idx < 1 || idx > 65535 { panic('new_type_ptr: idx must be between 1 & 65535') } diff --git a/vlib/v/table/cflags.v b/vlib/v/table/cflags.v index 713a9759b7..19ac0c49e0 100644 --- a/vlib/v/table/cflags.v +++ b/vlib/v/table/cflags.v @@ -17,7 +17,7 @@ fn (table &Table) has_cflag(flag cflag.CFlag) bool { // parse the flags to (table.cflags) []CFlag // Note: clean up big time (joe-c) -pub fn (mut table Table) parse_cflag(cflg, mod string, ctimedefines []string) ?bool { +pub fn (mut table Table) parse_cflag(cflg string, mod string, ctimedefines []string) ?bool { allowed_flags := ['framework', 'library', 'Wa', 'Wl', 'Wp', 'I', 'l', 'L'] flag_orig := cflg.trim_space() mut flag := flag_orig diff --git a/vlib/v/util/diff.v b/vlib/v/util/diff.v index a78dea228f..e8bfd0a4e9 100644 --- a/vlib/v/util/diff.v +++ b/vlib/v/util/diff.v @@ -12,10 +12,8 @@ pub fn find_working_diff_command() ?string { if env_difftool.len > 0 { known_diff_tools << env_difftool } - known_diff_tools << [ - 'colordiff', 'gdiff', 'diff', 'colordiff.exe', - 'diff.exe', 'opendiff', 'code', 'code.cmd' - ] + known_diff_tools << + ['colordiff', 'gdiff', 'diff', 'colordiff.exe', 'diff.exe', 'opendiff', 'code', 'code.cmd'] // NOTE: code.cmd is the Windows variant of the `code` cli tool for diffcmd in known_diff_tools { if diffcmd == 'opendiff' { // opendiff has no `--version` option @@ -56,10 +54,9 @@ fn opendiff_exists() bool { return false } -pub fn color_compare_files(diff_cmd, file1, file2 string) string { +pub fn color_compare_files(diff_cmd string, file1 string, file2 string) string { if diff_cmd != '' { - full_cmd := '$diff_cmd --minimal --text --unified=2 ' + - ' --show-function-line="fn " "$file1" "$file2" ' + full_cmd := '$diff_cmd --minimal --text --unified=2 ' + ' --show-function-line="fn " "$file1" "$file2" ' x := os.exec(full_cmd) or { return 'comparison command: `$full_cmd` failed' } @@ -68,7 +65,7 @@ pub fn color_compare_files(diff_cmd, file1, file2 string) string { return '' } -pub fn color_compare_strings(diff_cmd, expected, found string) string { +pub fn color_compare_strings(diff_cmd string, expected string, found string) string { cdir := os.cache_dir() ctime := time.sys_mono_now() e_file := os.join_path(cdir, '${ctime}.expected.txt') diff --git a/vlib/v/util/errors.v b/vlib/v/util/errors.v index dd66460d7c..359ea8ed0c 100644 --- a/vlib/v/util/errors.v +++ b/vlib/v/util/errors.v @@ -53,7 +53,7 @@ pub fn bold(msg string) string { return term.bold(msg) } -fn color(kind, msg string) string { +fn color(kind string, msg string) string { if !emanager.support_color { return msg } @@ -65,7 +65,7 @@ fn color(kind, msg string) string { } // formatted_error - `kind` may be 'error' or 'warn' -pub fn formatted_error(kind, omsg, filepath string, pos token.Position) string { +pub fn formatted_error(kind string, omsg string, filepath string, pos token.Position) string { emsg := omsg.replace('main.', '') mut path := filepath verror_paths_override := os.getenv('VERROR_PATHS') @@ -101,7 +101,7 @@ pub fn formatted_error(kind, omsg, filepath string, pos token.Position) string { return '$final_position $final_kind $final_msg$final_context'.trim_space() } -pub fn source_context(kind, source string, column int, pos token.Position) []string { +pub fn source_context(kind string, source string, column int, pos token.Position) []string { mut clines := []string{} if source.len == 0 { return clines @@ -136,7 +136,7 @@ pub fn source_context(kind, source string, column int, pos token.Position) []str return clines } -pub fn verror(kind, s string) { +pub fn verror(kind string, s string) { final_kind := bold(color(kind, kind)) eprintln('$final_kind: $s') exit(1) diff --git a/vlib/v/util/suggestions.v b/vlib/v/util/suggestions.v index e34989d6c1..2175b3d506 100644 --- a/vlib/v/util/suggestions.v +++ b/vlib/v/util/suggestions.v @@ -9,7 +9,7 @@ mut: similarity f32 } -fn compare_by_similarity(a, b &Possibility) int { +fn compare_by_similarity(a &Possibility, b &Possibility) int { if a.similarity < b.similarity { return -1 } diff --git a/vlib/v/util/util.v b/vlib/v/util/util.v index a58e74a013..2c7c9caba7 100644 --- a/vlib/v/util/util.v +++ b/vlib/v/util/util.v @@ -26,9 +26,7 @@ pub const ( pub fn vhash() string { mut buf := [50]byte{} buf[0] = 0 - unsafe { - C.snprintf(charptr(buf), 50, '%s', C.V_COMMIT_HASH) - } + unsafe {C.snprintf(charptr(buf), 50, '%s', C.V_COMMIT_HASH)} return tos_clone(buf) } @@ -98,9 +96,7 @@ pub fn githash(should_get_from_filesystem bool) string { } mut buf := [50]byte{} buf[0] = 0 - unsafe { - C.snprintf(charptr(buf), 50, '%s', C.V_CURRENT_COMMIT_HASH) - } + unsafe {C.snprintf(charptr(buf), 50, '%s', C.V_CURRENT_COMMIT_HASH)} return tos_clone(buf) } @@ -114,7 +110,7 @@ pub fn set_vroot_folder(vroot_path string) { os.setenv('VCHILD', 'true', true) } -pub fn resolve_vroot(str, dir string) ?string { +pub fn resolve_vroot(str string, dir string) ?string { mut mcache := vmod.get_cache() vmod_file_location := mcache.get_by_folder(dir) if vmod_file_location.vmod_file.len == 0 { @@ -236,7 +232,7 @@ pub fn read_file(file_path string) ?string { } [inline] -pub fn imin(a, b int) int { +pub fn imin(a int, b int) int { return if a < b { a } else { @@ -245,7 +241,7 @@ pub fn imin(a, b int) int { } [inline] -pub fn imax(a, b int) int { +pub fn imax(a int, b int) int { return if a > b { a } else { @@ -369,7 +365,7 @@ const ( // but *only* when it is at the start, i.e.: // no_cur_mod('vproto.Abdcdef', 'proto') == 'vproto.Abdcdef' // even though proto. is a substring -pub fn no_cur_mod(typename, cur_mod string) string { +pub fn no_cur_mod(typename string, cur_mod string) string { mut res := typename mod_prefix := cur_mod + '.' has_map_prefix := res.starts_with(map_prefix) diff --git a/vlib/vweb/tmpl/tmpl.v b/vlib/vweb/tmpl/tmpl.v index fcfda8846f..935d198cfd 100644 --- a/vlib/vweb/tmpl/tmpl.v +++ b/vlib/vweb/tmpl/tmpl.v @@ -12,7 +12,7 @@ const ( ) // compile_file compiles the content of a file by the given path as a template -pub fn compile_file(path, fn_name string) string { +pub fn compile_file(path string, fn_name string) string { html := os.read_file(path) or { panic('html failed') } @@ -26,7 +26,7 @@ enum State { //span // span.{ } -pub fn compile_template(html_, fn_name string) string { +pub fn compile_template(html_ string, fn_name string) string { // lines := os.read_lines(path) mut html := html_.trim_space() mut header := ''