-
Notifications
You must be signed in to change notification settings - Fork 7
/
Blocked.go
42 lines (38 loc) · 1.04 KB
/
Blocked.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
package text
import "strings"
const (
// textLineLimit is the lowest number found by a cursory search.
// It appears that various texts have different limits.
textLineLimit = 79
)
// Blocked splits the given input string into a series of lines, usable for blocked serialization.
// It returns an array of strings that each are below a maximum line length. The list is terminated with
// one empty line (necessary for compound strings).
func Blocked(input string) []string {
var result []string
textLines := strings.Split(input, "\n")
resultLine := ""
addBlock := func() {
if len(resultLine) > 0 {
result = append(result, resultLine)
resultLine = ""
}
}
for textLineIndex, textLine := range textLines {
words := strings.Split(textLine, " ")
for wordIndex, word := range words {
if wordIndex > 0 {
resultLine += " "
}
if (len(resultLine) + len(word)) > textLineLimit {
addBlock()
}
resultLine += word
}
if textLineIndex < (len(textLines) - 1) {
resultLine += "\n"
}
}
addBlock()
return append(result, "")
}