Files
opencloud/ocis-pkg/conversions/strings.go
David Christofas 1088faf95d pre allocate slices
Pre allocating slices with a target size reduces the number of allocations because we already know how big the slice will be and therefore need to allocate only once
2021-02-22 19:41:48 +01:00

30 lines
632 B
Go

package conversions
import (
"strings"
"unicode/utf8"
)
// StringToSliceString splits a string into a slice string according to separator
func StringToSliceString(src string, sep string) []string {
parsed := strings.Split(src, sep)
parts := make([]string, 0, len(parsed))
for _, v := range parsed {
parts = append(parts, strings.TrimSpace(v))
}
return parts
}
// Reverse reverses a string
func Reverse(s string) string {
size := len(s)
buf := make([]byte, size)
for start := 0; start < size; {
r, n := utf8.DecodeRuneInString(s[start:])
start += n
utf8.EncodeRune(buf[size-start:], r)
}
return string(buf)
}