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

feat: add SplitWords

This commit is contained in:
dudaodong
2023-02-21 14:16:36 +08:00
parent af7b9d2710
commit ed98ad53ec
4 changed files with 117 additions and 0 deletions

View File

@@ -287,3 +287,45 @@ func Substring(s string, offset int, length uint) string {
return strings.Replace(str, "\x00", "", -1)
}
// SplitWords splits a string into words, word only contains alphabetic characters.
// Play: todo
func SplitWords(s string) []string {
var word string
var words []string
var r rune
var size, pos int
isWord := false
for len(s) > 0 {
r, size = utf8.DecodeRuneInString(s)
switch {
case isLetter(r):
if !isWord {
isWord = true
word = s
pos = 0
}
case isWord && (r == '\'' || r == '-'):
// is word
default:
if isWord {
isWord = false
words = append(words, word[:pos])
}
}
pos += size
s = s[size:]
}
if isWord {
words = append(words, word[:pos])
}
return words
}