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

builtin: remove methods that can be autogenerated (#11109)

This commit is contained in:
Enzo 2021-08-09 14:42:31 +02:00 committed by GitHub
parent 1a555ab898
commit 506c30a291
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 95 deletions

View File

@ -592,44 +592,6 @@ pub fn copy(dst []byte, src []byte) int {
return min return min
} }
// Private function. Comparator for int type.
fn compare_ints(a &int, b &int) int {
if *a < *b {
return -1
}
if *a > *b {
return 1
}
return 0
}
fn compare_ints_reverse(a &int, b &int) int {
if *a > *b {
return -1
}
if *a < *b {
return 1
}
return 0
}
// sort sorts an array of int in place in ascending order.
pub fn (mut a []int) sort() {
a.sort_with_compare(compare_ints)
}
// index returns the first index at which a given element can be found in the array
// or -1 if the value is not found.
[direct_array_access]
pub fn (a []string) index(v string) int {
for i in 0 .. a.len {
if a[i] == v {
return i
}
}
return -1
}
// reduce executes a given reducer function on each element of the array, // reduce executes a given reducer function on each element of the array,
// resulting in a single output value. // resulting in a single output value.
pub fn (a []int) reduce(iter fn (int, int) int, accum_start int) int { pub fn (a []int) reduce(iter fn (int, int) int, accum_start int) int {
@ -652,25 +614,6 @@ pub fn (mut a array) grow_len(amount int) {
a.len += amount a.len += amount
} }
// eq checks if the arrays have the same elements or not.
// TODO: make it work with all types.
pub fn (a1 []string) eq(a2 []string) bool {
// return array_eq(a, a2)
if a1.len != a2.len {
return false
}
size_of_string := int(sizeof(string))
for i in 0 .. a1.len {
offset := i * size_of_string
s1 := unsafe { &string(&byte(a1.data) + offset) }
s2 := unsafe { &string(&byte(a2.data) + offset) }
if *s1 != *s2 {
return false
}
}
return true
}
// pointers returns a new array, where each element // pointers returns a new array, where each element
// is the address of the corresponding element in the array. // is the address of the corresponding element in the array.
[unsafe] [unsafe]

View File

@ -340,22 +340,6 @@ struct RepIndex {
val_idx int val_idx int
} }
// compare_rep_index returns the result of comparing RepIndex `a` and `b`.
fn compare_rep_index(a &RepIndex, b &RepIndex) int {
if a.idx < b.idx {
return -1
}
if a.idx > b.idx {
return 1
}
return 0
}
// sort2 sorts the RepIndex array using `compare_rep_index`.
fn (mut a []RepIndex) sort2() {
a.sort_with_compare(compare_rep_index)
}
// replace_each replaces all occurences of the string pairs given in `vals`. // replace_each replaces all occurences of the string pairs given in `vals`.
// Example: assert 'ABCD'.replace_each(['B','C/','C','D','D','C']) == 'AC/DC' // Example: assert 'ABCD'.replace_each(['B','C/','C','D','D','C']) == 'AC/DC'
[direct_array_access] [direct_array_access]
@ -404,7 +388,7 @@ pub fn (s string) replace_each(vals []string) string {
if idxs.len == 0 { if idxs.len == 0 {
return s.clone() return s.clone()
} }
idxs.sort2() idxs.sort(a.idx < b.idx)
mut b := unsafe { malloc_noscan(new_len + 1) } // add space for 0 terminator mut b := unsafe { malloc_noscan(new_len + 1) } // add space for 0 terminator
// Fill the new string // Fill the new string
mut idx_pos := 0 mut idx_pos := 0
@ -1192,17 +1176,6 @@ pub fn compare_strings(a &string, b &string) int {
return 0 return 0
} }
// compare_strings_reverse returns `1` if `a < b`, `-1` if `a > b` else `0`.
fn compare_strings_reverse(a &string, b &string) int {
if a < b {
return 1
}
if a > b {
return -1
}
return 0
}
// compare_strings_by_len returns `-1` if `a.len < b.len`, `1` if `a.len > b.len` else `0`. // compare_strings_by_len returns `-1` if `a.len < b.len`, `1` if `a.len > b.len` else `0`.
fn compare_strings_by_len(a &string, b &string) int { fn compare_strings_by_len(a &string, b &string) int {
if a.len < b.len { if a.len < b.len {
@ -1221,11 +1194,6 @@ fn compare_lower_strings(a &string, b &string) int {
return compare_strings(&aa, &bb) return compare_strings(&aa, &bb)
} }
// sort sorts the string array.
pub fn (mut s []string) sort() {
s.sort_with_compare(compare_strings)
}
// sort_ignore_case sorts the string array using case insesitive comparing. // sort_ignore_case sorts the string array using case insesitive comparing.
pub fn (mut s []string) sort_ignore_case() { pub fn (mut s []string) sort_ignore_case() {
s.sort_with_compare(compare_lower_strings) s.sort_with_compare(compare_lower_strings)

View File

@ -3,7 +3,7 @@ import os.cmdline
fn test_options() { fn test_options() {
args := ['v', '-d', 'aa', '-d', 'bb', '-d', 'cc'] args := ['v', '-d', 'aa', '-d', 'bb', '-d', 'cc']
ret := cmdline.options(args, '-d') ret := cmdline.options(args, '-d')
assert ret.eq(['aa', 'bb', 'cc']) assert ret == ['aa', 'bb', 'cc']
} }
fn test_option() { fn test_option() {
@ -15,23 +15,23 @@ fn test_option() {
fn test_options_before() { fn test_options_before() {
args := ['-stat', 'test', 'aaa.v'] args := ['-stat', 'test', 'aaa.v']
ret := cmdline.options_before(args, ['test']) ret := cmdline.options_before(args, ['test'])
assert ret.eq(['-stat']) assert ret == ['-stat']
} }
fn test_options_after() { fn test_options_after() {
args := ['-stat', 'test', 'aaa.v'] args := ['-stat', 'test', 'aaa.v']
ret := cmdline.options_after(args, ['test']) ret := cmdline.options_after(args, ['test'])
assert ret.eq(['aaa.v']) assert ret == ['aaa.v']
} }
fn test_only_non_options() { fn test_only_non_options() {
args := ['-d', 'aa', '--help', 'bb'] args := ['-d', 'aa', '--help', 'bb']
ret := cmdline.only_non_options(args) ret := cmdline.only_non_options(args)
assert ret.eq(['aa', 'bb']) assert ret == ['aa', 'bb']
} }
fn test_only_options() { fn test_only_options() {
args := ['-d', 'aa', '--help', 'bb'] args := ['-d', 'aa', '--help', 'bb']
ret := cmdline.only_options(args) ret := cmdline.only_options(args)
assert ret.eq(['-d', '--help']) assert ret == ['-d', '--help']
} }

View File

@ -221,6 +221,10 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) {
// println(rec_sym.kind) // println(rec_sym.kind)
verror('.sort() is an array method') verror('.sort() is an array method')
} }
if g.pref.is_bare {
g.writeln('bare_panic(_SLIT("sort does not work with -freestanding"))')
return
}
info := rec_sym.info as ast.Array info := rec_sym.info as ast.Array
// `users.sort(a.age > b.age)` // `users.sort(a.age > b.age)`
// Generate a comparison function for a custom type // Generate a comparison function for a custom type