mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: check filter, map and sort left type (#6952)
This commit is contained in:
@ -1490,34 +1490,6 @@ pub fn (s string) fields() []string {
|
||||
return s.replace('\t', ' ').split(' ')
|
||||
}
|
||||
|
||||
pub fn (s string) map(func fn(byte) byte) string {
|
||||
unsafe {
|
||||
mut res := malloc(s.len + 1)
|
||||
for i in 0..s.len {
|
||||
res[i] = func(s[i])
|
||||
}
|
||||
return tos(res, s.len)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (s string) filter(func fn(b byte) bool) string {
|
||||
mut new_len := 0
|
||||
mut buf := malloc(s.len + 1)
|
||||
for i in 0 .. s.len {
|
||||
mut b := s[i]
|
||||
if func(b) {
|
||||
unsafe {
|
||||
buf[new_len] = b
|
||||
}
|
||||
new_len++
|
||||
}
|
||||
}
|
||||
unsafe {
|
||||
buf[new_len] = 0
|
||||
return buf.vstring_with_len(new_len)
|
||||
}
|
||||
}
|
||||
|
||||
// Allows multi-line strings to be formatted in a way that removes white-space
|
||||
// before a delimeter. by default `|` is used.
|
||||
// Note: the delimiter has to be a byte at this time. That means surrounding
|
||||
|
@ -816,41 +816,10 @@ fn test_double_quote_inter() {
|
||||
assert '${a} ${b}' == "1 2"
|
||||
}
|
||||
|
||||
fn test_string_map() {
|
||||
$if windows {
|
||||
// TODO
|
||||
return
|
||||
}
|
||||
original := 'Hello'
|
||||
println('original.len = $original.len')
|
||||
a := original.map(fn (b byte) byte {
|
||||
return b + 1
|
||||
})
|
||||
expected := 'Ifmmp'
|
||||
println('a[0] = ' + a[0].str())
|
||||
println('a[1] = ' + a[1].str())
|
||||
println('a[2] = ' + a[2].str())
|
||||
println('a[3] = ' + a[3].str())
|
||||
println('a[4] = ' + a[4].str())
|
||||
println('a.len = $a.len')
|
||||
assert a.len == expected.len
|
||||
assert a == expected
|
||||
|
||||
assert 'foo'.map(foo) == r'\ee'
|
||||
}
|
||||
|
||||
fn foo(b byte) byte {
|
||||
return b - 10
|
||||
}
|
||||
|
||||
fn test_string_filter() {
|
||||
foo := 'V is awesome!!!!'.filter(fn (b byte) bool {
|
||||
return b != `!`
|
||||
})
|
||||
assert foo == 'V is awesome'
|
||||
assert 'Alexander'.filter(filter) == 'Alexnder'
|
||||
}
|
||||
|
||||
fn filter(b byte) bool {
|
||||
return b != `a`
|
||||
}
|
||||
|
Reference in New Issue
Block a user