mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
string: add map method
This commit is contained in:
parent
5749add670
commit
0052ab71e9
@ -1354,6 +1354,10 @@ pub fn (s string) fields() []string {
|
|||||||
return s.replace('\t', ' ').split(' ')
|
return s.replace('\t', ' ').split(' ')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn (s string) map(func fn(byte) byte) string {
|
||||||
|
return string(s.bytes().map(func(it)))
|
||||||
|
}
|
||||||
|
|
||||||
// Allows multi-line strings to be formatted in a way that removes white-space
|
// Allows multi-line strings to be formatted in a way that removes white-space
|
||||||
// before a delimeter. by default `|` is used.
|
// before a delimeter. by default `|` is used.
|
||||||
// Note: the delimiter has to be a byte at this time. That means surrounding
|
// Note: the delimiter has to be a byte at this time. That means surrounding
|
||||||
|
@ -742,6 +742,19 @@ fn test_double_quote_inter() {
|
|||||||
assert '${a} ${b}' == "1 2"
|
assert '${a} ${b}' == "1 2"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_string_map() {
|
||||||
|
a := 'Hello'.map(fn (b byte) byte {
|
||||||
|
return b + 1
|
||||||
|
})
|
||||||
|
assert a == 'Ifmmp'
|
||||||
|
|
||||||
|
assert 'foo'.map(foo) == r'\ee'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo(b byte) byte {
|
||||||
|
return b - 10
|
||||||
|
}
|
||||||
|
|
||||||
fn test_split_into_lines() {
|
fn test_split_into_lines() {
|
||||||
line_content := 'Line'
|
line_content := 'Line'
|
||||||
text_crlf := '${line_content}\r\n${line_content}\r\n${line_content}'
|
text_crlf := '${line_content}\r\n${line_content}\r\n${line_content}'
|
||||||
|
Loading…
Reference in New Issue
Block a user