package strings
import (
"unicode"
"utf8"
)
func explode(s string, n int) []string {
if n == 0 {
return nil
}
l := utf8.RuneCountInString(s)
if n <= 0 || n > l {
n = l
}
a := make([]string, n)
var size, rune int
i, cur := 0, 0
for ; i+1 < n; i++ {
rune, size = utf8.DecodeRuneInString(s[cur:])
a[i] = string(rune)
cur += size
}
if cur < len(s) {
a[i] = s[cur:]
}
return a
}
func Count(s, sep string) int {
if sep == "" {
return utf8.RuneCountInString(s) + 1
}
c := sep[0]
l := len(sep)
n := 0
if l == 1 {
for i := 0; i < len(s); i++ {
if s[i] == c {
n++
}
}
return n
}
for i := 0; i+l <= len(s); i++ {
if s[i] == c && s[i:i+l] == sep {
n++
i += l - 1
}
}
return n
}
func Index(s, sep string) int {
n := len(sep)
if n == 0 {
return 0
}
c := sep[0]
if n == 1 {
for i := 0; i < len(s); i++ {
if s[i] == c {
return i
}
}
return -1
}
for i := 0; i+n <= len(s); i++ {
if s[i] == c && s[i:i+n] == sep {
return i
}
}
return -1
}
func LastIndex(s, sep string) int {
n := len(sep)
if n == 0 {
return len(s)
}
c := sep[0]
if n == 1 {
for i := len(s) - 1; i >= 0; i-- {
if s[i] == c {
return i
}
}
return -1
}
for i := len(s) - n; i >= 0; i-- {
if s[i] == c && s[i:i+n] == sep {
return i
}
}
return -1
}
func IndexRune(s string, rune int) int {
for i, c := range s {
if c == rune {
return i
}
}
return -1
}
func IndexAny(s, chars string) int {
if len(chars) > 0 {
for i, c := range s {
for _, m := range chars {
if c == m {
return i
}
}
}
}
return -1
}
func genSplit(s, sep string, sepSave, n int) []string {
if n == 0 {
return nil
}
if sep == "" {
return explode(s, n)
}
if n < 0 {
n = Count(s, sep) + 1
}
c := sep[0]
start := 0
a := make([]string, n)
na := 0
for i := 0; i+len(sep) <= len(s) && na+1 < n; i++ {
if s[i] == c && (len(sep) == 1 || s[i:i+len(sep)] == sep) {
a[na] = s[start : i+sepSave]
na++
start = i + len(sep)
i += len(sep) - 1
}
}
a[na] = s[start:]
return a[0 : na+1]
}
func Split(s, sep string, n int) []string { return genSplit(s, sep, 0, n) }
func SplitAfter(s, sep string, n int) []string {
return genSplit(s, sep, len(sep), n)
}
func Fields(s string) []string {
return FieldsFunc(s, unicode.IsSpace)
}
func FieldsFunc(s string, f func(int) bool) []string {
n := 0
inField := false
for _, rune := range s {
wasInField := inField
inField = !f(rune)
if inField && !wasInField {
n++
}
}
a := make([]string, n)
na := 0
fieldStart := -1
for i, rune := range s {
if f(rune) {
if fieldStart >= 0 {
a[na] = s[fieldStart:i]
na++
fieldStart = -1
}
} else if fieldStart == -1 {
fieldStart = i
}
}
if fieldStart != -1 {
a[na] = s[fieldStart:]
}
return a
}
func Join(a []string, sep string) string {
if len(a) == 0 {
return ""
}
if len(a) == 1 {
return a[0]
}
n := len(sep) * (len(a) - 1)
for i := 0; i < len(a); i++ {
n += len(a[i])
}
b := make([]byte, n)
bp := 0
for i := 0; i < len(a); i++ {
s := a[i]
for j := 0; j < len(s); j++ {
b[bp] = s[j]
bp++
}
if i+1 < len(a) {
s = sep
for j := 0; j < len(s); j++ {
b[bp] = s[j]
bp++
}
}
}
return string(b)
}
func HasPrefix(s, prefix string) bool {
return len(s) >= len(prefix) && s[0:len(prefix)] == prefix
}
func HasSuffix(s, suffix string) bool {
return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
}
func Map(mapping func(rune int) int, s string) string {
maxbytes := len(s)
nbytes := 0
b := make([]byte, maxbytes)
for _, c := range s {
rune := mapping(c)
if rune >= 0 {
wid := 1
if rune >= utf8.RuneSelf {
wid = utf8.RuneLen(rune)
}
if nbytes+wid > maxbytes {
maxbytes = maxbytes*2 + utf8.UTFMax
nb := make([]byte, maxbytes)
copy(nb, b[0:nbytes])
b = nb
}
nbytes += utf8.EncodeRune(rune, b[nbytes:maxbytes])
}
}
return string(b[0:nbytes])
}
func Repeat(s string, count int) string {
b := make([]byte, len(s)*count)
bp := 0
for i := 0; i < count; i++ {
for j := 0; j < len(s); j++ {
b[bp] = s[j]
bp++
}
}
return string(b)
}
func ToUpper(s string) string { return Map(unicode.ToUpper, s) }
func ToLower(s string) string { return Map(unicode.ToLower, s) }
func ToTitle(s string) string { return Map(unicode.ToTitle, s) }
func ToUpperSpecial(_case unicode.SpecialCase, s string) string {
return Map(func(r int) int { return _case.ToUpper(r) }, s)
}
func ToLowerSpecial(_case unicode.SpecialCase, s string) string {
return Map(func(r int) int { return _case.ToLower(r) }, s)
}
func ToTitleSpecial(_case unicode.SpecialCase, s string) string {
return Map(func(r int) int { return _case.ToTitle(r) }, s)
}
func isSeparator(rune int) bool {
if rune <= 0x7F {
switch {
case '0' <= rune && rune <= '9':
return false
case 'a' <= rune && rune <= 'z':
return false
case 'A' <= rune && rune <= 'Z':
return false
case rune == '_':
return false
}
return true
}
if unicode.IsLetter(rune) || unicode.IsDigit(rune) {
return false
}
return unicode.IsSpace(rune)
}
func Title(s string) string {
prev := ' '
return Map(
func(r int) int {
if isSeparator(prev) {
prev = r
return unicode.ToTitle(r)
}
prev = r
return r
},
s)
}
func TrimLeftFunc(s string, f func(r int) bool) string {
i := indexFunc(s, f, false)
if i == -1 {
return ""
}
return s[i:]
}
func TrimRightFunc(s string, f func(r int) bool) string {
i := lastIndexFunc(s, f, false)
if i >= 0 && s[i] >= utf8.RuneSelf {
_, wid := utf8.DecodeRuneInString(s[i:])
i += wid
} else {
i++
}
return s[0:i]
}
func TrimFunc(s string, f func(r int) bool) string {
return TrimRightFunc(TrimLeftFunc(s, f), f)
}
func IndexFunc(s string, f func(r int) bool) int {
return indexFunc(s, f, true)
}
func LastIndexFunc(s string, f func(r int) bool) int {
return lastIndexFunc(s, f, true)
}
func indexFunc(s string, f func(r int) bool, truth bool) int {
start := 0
for start < len(s) {
wid := 1
rune := int(s[start])
if rune >= utf8.RuneSelf {
rune, wid = utf8.DecodeRuneInString(s[start:])
}
if f(rune) == truth {
return start
}
start += wid
}
return -1
}
func lastIndexFunc(s string, f func(r int) bool, truth bool) int {
end := len(s)
for end > 0 {
start := end - 1
rune := int(s[start])
if rune >= utf8.RuneSelf {
for start--; start >= 0; start-- {
if utf8.RuneStart(s[start]) {
break
}
}
if start < 0 {
return -1
}
var wid int
rune, wid = utf8.DecodeRuneInString(s[start:end])
if start+wid < end && f(utf8.RuneError) == truth {
return end - 1
}
}
if f(rune) == truth {
return start
}
end = start
}
return -1
}
func makeCutsetFunc(cutset string) func(rune int) bool {
return func(rune int) bool { return IndexRune(cutset, rune) != -1 }
}
func Trim(s string, cutset string) string {
if s == "" || cutset == "" {
return s
}
return TrimFunc(s, makeCutsetFunc(cutset))
}
func TrimLeft(s string, cutset string) string {
if s == "" || cutset == "" {
return s
}
return TrimLeftFunc(s, makeCutsetFunc(cutset))
}
func TrimRight(s string, cutset string) string {
if s == "" || cutset == "" {
return s
}
return TrimRightFunc(s, makeCutsetFunc(cutset))
}
func TrimSpace(s string) string {
return TrimFunc(s, unicode.IsSpace)
}
func Replace(s, old, new string, n int) string {
if old == new || n == 0 {
return s
}
if m := Count(s, old); m == 0 {
return s
} else if n < 0 || m < n {
n = m
}
t := make([]byte, len(s)+n*(len(new)-len(old)))
w := 0
start := 0
for i := 0; i < n; i++ {
j := start
if len(old) == 0 {
if i > 0 {
_, wid := utf8.DecodeRuneInString(s[start:])
j += wid
}
} else {
j += Index(s[start:], old)
}
w += copyString(t[w:], s[start:j])
w += copyString(t[w:], new)
start = j + len(old)
}
w += copyString(t[w:], s[start:])
return string(t[0:w])
}
func copyString(dst []byte, src string) int {
n := len(dst)
if n > len(src) {
n = len(src)
}
for i := 0; i < n; i++ {
dst[i] = src[i]
}
return n
}