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

builtin: deprecate string.split_by_whitespace in favor of fields()

This commit is contained in:
Alexander Medvednikov 2021-03-16 08:29:14 +03:00
parent 138da8e130
commit 93df560cf9
2 changed files with 133 additions and 118 deletions

View File

@ -1616,8 +1616,32 @@ pub fn (s string) repeat(count int) string {
// fields returns a string array of the string split by `\t` and ` ` // fields returns a string array of the string split by `\t` and ` `
// Example: assert '\t\tv = v'.fields() == ['', '', 'v', '=', 'v'] // Example: assert '\t\tv = v'.fields() == ['', '', 'v', '=', 'v']
pub fn (s string) fields() []string { pub fn (s string) fields() []string {
// TODO do this in a better way mut res := []string{}
return s.replace('\t', ' ').split(' ') mut word_start := 0
mut word_end := 0
mut is_in_word := false
mut is_space := false
for i, c in s {
is_space = c in [` `, `\t`, `\n`]
if !is_in_word && !is_space {
word_start = i
is_in_word = true
continue
}
if is_space && is_in_word {
word_end = i
res << s[word_start..word_end]
is_in_word = false
word_end = 0
word_start = 0
continue
}
}
if is_in_word && word_start > 0 {
// collect the remainder word at the end
res << s[word_start..s.len]
}
return res
} }
// strip_margin allows multi-line strings to be formatted in a way that removes white-space // strip_margin allows multi-line strings to be formatted in a way that removes white-space
@ -1684,31 +1708,8 @@ pub fn (s string) strip_margin_custom(del byte) string {
// split_by_whitespace - extract only the non whitespace tokens/words from the given string `s`. // split_by_whitespace - extract only the non whitespace tokens/words from the given string `s`.
// example: ' sss ssss'.split_by_whitespace() => ['sss', 'ssss'] // example: ' sss ssss'.split_by_whitespace() => ['sss', 'ssss']
[deprecated: 'use string.fields() instead']
pub fn (s string) split_by_whitespace() []string { pub fn (s string) split_by_whitespace() []string {
mut res := []string{} return s.fields()
mut word_start := 0
mut word_end := 0
mut is_in_word := false
mut is_space := false
for i, c in s {
is_space = c in [` `, `\t`, `\n`]
if !is_in_word && !is_space {
word_start = i
is_in_word = true
continue
}
if is_space && is_in_word {
word_end = i
res << s[word_start..word_end]
is_in_word = false
word_end = 0
word_start = 0
continue
}
}
if is_in_word && word_start > 0 {
// collect the remainder word at the end
res << s[word_start..s.len]
}
return res
} }

View File

@ -1,3 +1,5 @@
import strings
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
@ -88,7 +90,10 @@ fn test_compare_strings() {
fn test_sort() { fn test_sort() {
mut vals := [ mut vals := [
'arr', 'an', 'a', 'any' 'arr',
'an',
'a',
'any',
] ]
len := vals.len len := vals.len
vals.sort() vals.sort()
@ -101,7 +106,10 @@ fn test_sort() {
fn test_sort_reverse() { fn test_sort_reverse() {
mut vals := [ mut vals := [
'arr', 'an', 'a', 'any' 'arr',
'an',
'a',
'any',
] ]
len := vals.len len := vals.len
vals.sort(b > a) vals.sort(b > a)
@ -113,35 +121,35 @@ fn test_sort_reverse() {
} }
fn test_split_nth() { fn test_split_nth() {
a := "1,2,3" a := '1,2,3'
assert (a.split(',').len == 3) assert a.split(',').len == 3
assert (a.split_nth(',', -1).len == 3) assert a.split_nth(',', -1).len == 3
assert (a.split_nth(',', 0).len == 3) assert a.split_nth(',', 0).len == 3
assert (a.split_nth(',', 1).len == 1) assert a.split_nth(',', 1).len == 1
assert (a.split_nth(',', 2).len == 2) assert a.split_nth(',', 2).len == 2
assert (a.split_nth(',', 10).len == 3) assert a.split_nth(',', 10).len == 3
b := "1::2::3" b := '1::2::3'
assert (b.split('::').len == 3) assert b.split('::').len == 3
assert (b.split_nth('::', -1).len == 3) assert b.split_nth('::', -1).len == 3
assert (b.split_nth('::', 0).len == 3) assert b.split_nth('::', 0).len == 3
assert (b.split_nth('::', 1).len == 1) assert b.split_nth('::', 1).len == 1
assert (b.split_nth('::', 2).len == 2) assert b.split_nth('::', 2).len == 2
assert (b.split_nth('::', 10).len == 3) assert b.split_nth('::', 10).len == 3
c := "ABCDEF" c := 'ABCDEF'
println(c.split('').len) println(c.split('').len)
assert (c.split('').len == 6) assert c.split('').len == 6
assert (c.split_nth('', 3).len == 3) assert c.split_nth('', 3).len == 3
assert (c.split_nth('BC', -1).len == 2) assert c.split_nth('BC', -1).len == 2
d := "," d := ','
assert (d.split(',').len == 2) assert d.split(',').len == 2
assert (d.split_nth('', 3).len == 1) assert d.split_nth('', 3).len == 1
assert (d.split_nth(',', -1).len == 2) assert d.split_nth(',', -1).len == 2
assert (d.split_nth(',', 3).len == 2) assert d.split_nth(',', 3).len == 2
e := ",,,0,,,,,a,,b," e := ',,,0,,,,,a,,b,'
assert (e.split(',,').len == 5) assert e.split(',,').len == 5
assert (e.split_nth(',,', 3).len == 3) assert e.split_nth(',,', 3).len == 3
assert (e.split_nth(',', -1).len == 12) assert e.split_nth(',', -1).len == 12
assert (e.split_nth(',', 3).len == 3) assert e.split_nth(',', 3).len == 3
} }
fn test_split_nth_values() { fn test_split_nth_values() {
@ -241,10 +249,12 @@ fn test_join() {
mut strings := ['a', 'b', 'c'] mut strings := ['a', 'b', 'c']
mut s := strings.join(' ') mut s := strings.join(' ')
assert s == 'a b c' assert s == 'a b c'
strings = ['one strings = [
'one
two ', two ',
'three! 'three!
four!'] four!',
]
s = strings.join(' ') s = strings.join(' ')
assert s.contains('one') && s.contains('two ') && s.contains('four') assert s.contains('one') && s.contains('two ') && s.contains('four')
empty := []string{len: 0} empty := []string{len: 0}
@ -284,31 +294,39 @@ fn test_replace() {
assert c.replace('', '-') == c assert c.replace('', '-') == c
v := 'a b c d' v := 'a b c d'
assert v.replace(' ', ' ') == 'a b c d' assert v.replace(' ', ' ') == 'a b c d'
} }
fn test_replace_each() { fn test_replace_each() {
s := 'hello man man :)' s := 'hello man man :)'
q := s.replace_each([ q := s.replace_each([
'man', 'dude', 'man',
'hello', 'hey' 'dude',
'hello',
'hey',
]) ])
assert q == 'hey dude dude :)' assert q == 'hey dude dude :)'
bb := '[b]bold[/b] [code]code[/code]' bb := '[b]bold[/b] [code]code[/code]'
assert bb.replace_each([ assert bb.replace_each([
'[b]', '<b>', '[b]',
'[/b]', '</b>', '<b>',
'[code]', '<code>', '[/b]',
'[/code]', '</code>' '</b>',
'[code]',
'<code>',
'[/code]',
'</code>',
]) == '<b>bold</b> <code>code</code>' ]) == '<b>bold</b> <code>code</code>'
bb2 := '[b]cool[/b]' bb2 := '[b]cool[/b]'
assert bb2.replace_each([ assert bb2.replace_each([
'[b]', '<b>', '[b]',
'[/b]', '</b>', '<b>',
'[/b]',
'</b>',
]) == '<b>cool</b>' ]) == '<b>cool</b>'
t := 'aaaaaaaa' t := 'aaaaaaaa'
y := t.replace_each([ y := t.replace_each([
'aa', 'b' 'aa',
'b',
]) ])
assert y == 'bbbb' assert y == 'bbbb'
} }
@ -425,7 +443,7 @@ fn test_to_num() {
fn test_inter_format_string() { fn test_inter_format_string() {
float_num := 1.52345 float_num := 1.52345
float_num_string := '-${float_num:.03f}-' float_num_string := '-${float_num:.3f}-'
assert float_num_string == '-1.523-' assert float_num_string == '-1.523-'
int_num := 7 int_num := 7
int_num_string := '-${int_num:03d}-' int_num_string := '-${int_num:03d}-'
@ -645,14 +663,13 @@ fn test_for_loop_two() {
} }
fn test_quote() { fn test_quote() {
a := `'` a := `\'`
println("testing double quotes") println('testing double quotes')
b := "hi" b := 'hi'
assert b == 'hi' assert b == 'hi'
assert a.str() == '\'' assert a.str() == "'"
} }
fn test_ustring_comparisons() { fn test_ustring_comparisons() {
/* /*
QTODO QTODO
@ -773,8 +790,8 @@ fn test_raw_with_quotes() {
fn test_escape() { fn test_escape() {
a := 10 a := 10
println("\"$a") println('\"$a')
assert "\"$a" == "\"10" assert '\"$a' == '"10'
} }
fn test_atoi() { fn test_atoi() {
@ -815,9 +832,9 @@ fn test_inter_before_comp_if() {
fn test_double_quote_inter() { fn test_double_quote_inter() {
a := 1 a := 1
b := 2 b := 2
println("${a} ${b}") println('$a $b')
assert "${a} ${b}" == "1 2" assert '$a $b' == '1 2'
assert '${a} ${b}' == "1 2" assert '$a $b' == '1 2'
} }
fn foo(b byte) byte { fn foo(b byte) byte {
@ -830,7 +847,7 @@ fn filter(b byte) bool {
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'
lines_crlf := text_crlf.split_into_lines() lines_crlf := text_crlf.split_into_lines()
assert lines_crlf.len == 3 assert lines_crlf.len == 3
@ -838,7 +855,7 @@ fn test_split_into_lines() {
assert line == line_content assert line == line_content
} }
text_lf := '${line_content}\n${line_content}\n${line_content}' text_lf := '$line_content\n$line_content\n$line_content'
lines_lf := text_lf.split_into_lines() lines_lf := text_lf.split_into_lines()
assert lines_lf.len == 3 assert lines_lf.len == 3
@ -848,13 +865,10 @@ fn test_split_into_lines() {
} }
fn test_string_literal_with_backslash() { fn test_string_literal_with_backslash() {
a := 'Hello\ a := 'HelloWorld'
World'
assert a == 'HelloWorld' assert a == 'HelloWorld'
b := 'One\ b := 'OneTwoThree'
Two\
Three'
assert b == 'OneTwoThree' assert b == 'OneTwoThree'
} }
@ -887,7 +901,7 @@ fn test_sorter() {
Ka{ Ka{
s: 'ccc' s: 'ccc'
i: 102 i: 102
} },
] ]
cmp := fn (a &Ka, b &Ka) int { cmp := fn (a &Ka, b &Ka) int {
return compare_strings(a.s, b.s) return compare_strings(a.s, b.s)
@ -902,10 +916,10 @@ fn test_sorter() {
} }
fn test_split_by_whitespace() { fn test_split_by_whitespace() {
assert 'a bcde'.split_by_whitespace() == ['a', 'bcde'] assert 'a bcde'.fields() == ['a', 'bcde']
assert ' sss \t ssss '.split_by_whitespace() == ['sss', 'ssss'] assert ' sss \t ssss '.fields() == ['sss', 'ssss']
assert '\n xyz \t abc def'.split_by_whitespace() == ['xyz', 'abc', 'def'] assert '\n xyz \t abc def'.fields() == ['xyz', 'abc', 'def']
assert ''.split_by_whitespace() == [] assert ''.fields() == []
} }
fn test_interpolation_after_quoted_variable_still_works() { fn test_interpolation_after_quoted_variable_still_works() {