33 lines
935 B
Go
33 lines
935 B
Go
package utils
|
|
|
|
import "strings"
|
|
|
|
func abs(i int) int {
|
|
if i < 0 {
|
|
return -i
|
|
}
|
|
return i
|
|
}
|
|
|
|
func SplitCenter(text string) []string {
|
|
centerIndex := int(len(text) / 2)
|
|
whitespaceFrontIndex := strings.IndexRune(text[centerIndex:], ' ')
|
|
whitespaceBackIndex := strings.LastIndex(text[:centerIndex], " ")
|
|
frontIndex := centerIndex + whitespaceFrontIndex
|
|
frontDiff := 9999
|
|
backDiff := 9999
|
|
if whitespaceFrontIndex < 1 && whitespaceBackIndex < 1 {
|
|
return []string{text}
|
|
}
|
|
if whitespaceFrontIndex >= 0 {
|
|
frontDiff = len(text[:frontIndex]) - len(text[frontIndex:])
|
|
}
|
|
if whitespaceBackIndex >= 0 {
|
|
backDiff = len(text[:whitespaceBackIndex]) - len(text[whitespaceBackIndex:])
|
|
}
|
|
if abs(frontDiff) < abs(backDiff) {
|
|
return []string{strings.TrimSpace(text[:frontIndex]), strings.TrimSpace(text[frontIndex:])}
|
|
}
|
|
return []string{strings.TrimSpace(text[:whitespaceBackIndex]), strings.TrimSpace(text[whitespaceBackIndex:])}
|
|
}
|