mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
strings: add split_capital (#14716)
This commit is contained in:
parent
1d462136bc
commit
96a9faf2fd
@ -126,3 +126,24 @@ pub fn find_between_pair_string(input string, start string, end string) string {
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
// split_capital returns an array containing the contents of `s` split by capital letters.
|
||||
// Example: assert strings.split_capital('XYZ') == ['X', 'Y', 'Z']
|
||||
// Example: assert strings.split_capital('XYStar') == ['X', 'Y', 'Star']
|
||||
pub fn split_capital(s string) []string {
|
||||
mut res := []string{}
|
||||
mut word_start := 0
|
||||
for idx, c in s {
|
||||
if c >= `A` && c <= `Z` {
|
||||
if word_start != idx {
|
||||
res << s#[word_start..idx]
|
||||
}
|
||||
word_start = idx
|
||||
continue
|
||||
}
|
||||
}
|
||||
if word_start != s.len {
|
||||
res << s#[word_start..]
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
@ -94,3 +94,14 @@ fn test_find_between_pair_family() {
|
||||
assert '$e1' == '$e2'
|
||||
}
|
||||
}
|
||||
|
||||
fn test_split_capital() {
|
||||
assert strings.split_capital('') == []
|
||||
assert strings.split_capital('abc') == ['abc']
|
||||
assert strings.split_capital('X') == ['X']
|
||||
assert strings.split_capital('XX') == ['X', 'X']
|
||||
assert strings.split_capital('XYZ') == ['X', 'Y', 'Z']
|
||||
assert strings.split_capital('JohnWilliams') == ['John', 'Williams']
|
||||
assert strings.split_capital('JDStar') == ['J', 'D', 'Star']
|
||||
assert strings.split_capital('cpDumpRotarySpring') == ['cp', 'Dump', 'Rotary', 'Spring']
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user