1
0
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:
Larpon 2022-06-07 17:43:06 +02:00 committed by GitHub
parent 1d462136bc
commit 96a9faf2fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -126,3 +126,24 @@ pub fn find_between_pair_string(input string, start string, end string) string {
} }
return '' 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
}

View File

@ -94,3 +94,14 @@ fn test_find_between_pair_family() {
assert '$e1' == '$e2' 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']
}