-
Notifications
You must be signed in to change notification settings - Fork 684
/
wordwrap.go
45 lines (43 loc) · 947 Bytes
/
wordwrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"strings"
)
func wordwrap(width int, str string) string {
var words []string
for len(str) > 0 {
sep := strings.IndexAny(str, " \n")
if sep < 0 {
words = append(words, str)
break
}
word := str[:sep]
rest := str[sep+1:]
words = append(words, word)
// First space after a period is a non-breaking space;
// encode that with an empty word.
if strings.HasSuffix(word, ".") && strings.HasPrefix(rest, " ") {
words = append(words, "")
}
str = strings.TrimLeft(rest, " \n")
}
linewidth := 0
ret := new(strings.Builder)
sep := ""
for _, word := range words {
if word == "" {
sep = " "
} else if linewidth+len(sep)+len(word) > width {
ret.WriteString("\n")
ret.WriteString(word)
linewidth = len(word)
sep = " "
} else {
ret.WriteString(sep)
ret.WriteString(word)
linewidth += len(sep) + len(word)
sep = " "
}
}
ret.WriteString("\n")
return ret.String()
}