mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-02 02:11:18 -06:00
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
30 lines
632 B
Go
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)
|
|
}
|