1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-07 06:02:27 +08:00

feat: fix and add SnakeCase/UpperSnakeCase

This commit is contained in:
dudaodong
2022-12-14 21:17:30 +08:00
parent 5b3d48a1e7
commit 5cfb11f036
3 changed files with 43 additions and 59 deletions

View File

@@ -1,47 +1,9 @@
package strutil
import (
"strings"
"unicode"
)
// splitWordsToLower split a string into worlds by uppercase char
func splitWordsToLower(s string) []string {
var result []string
upperIndexes := upperIndex(s)
l := len(upperIndexes)
if upperIndexes == nil || l == 0 {
if s != "" {
result = append(result, s)
}
return result
}
for i := 0; i < l; i++ {
if i < l-1 {
result = append(result, strings.ToLower(s[upperIndexes[i]:upperIndexes[i+1]]))
} else {
result = append(result, strings.ToLower(s[upperIndexes[i]:]))
}
}
return result
}
// upperIndex get a int slice which elements are all the uppercase char index of a string
func upperIndex(s string) []int {
var result []int
for i := 0; i < len(s); i++ {
if 64 < s[i] && s[i] < 91 {
result = append(result, i)
}
}
if len(s) > 0 && result != nil && result[0] != 0 {
result = append([]int{0}, result...)
}
return result
}
func splitIntoStrings(s string, upperCase bool) []string {
var runes [][]rune
lastCharType := 0
@@ -83,7 +45,6 @@ func splitIntoStrings(s string, upperCase bool) []string {
result = append(result, string(toUpperAll(rs)))
} else {
result = append(result, string(toLowerAll(rs)))
}
}
}