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:
@@ -1616,8 +1616,32 @@ pub fn (s string) repeat(count int) string {
|
||||
// fields returns a string array of the string split by `\t` and ` `
|
||||
// Example: assert '\t\tv = v'.fields() == ['', '', 'v', '=', 'v']
|
||||
pub fn (s string) fields() []string {
|
||||
// TODO do this in a better way
|
||||
return s.replace('\t', ' ').split(' ')
|
||||
mut res := []string{}
|
||||
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
|
||||
@@ -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`.
|
||||
// example: ' sss ssss'.split_by_whitespace() => ['sss', 'ssss']
|
||||
|
||||
[deprecated: 'use string.fields() instead']
|
||||
pub fn (s string) split_by_whitespace() []string {
|
||||
mut res := []string{}
|
||||
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
|
||||
return s.fields()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user