-
Notifications
You must be signed in to change notification settings - Fork 112
/
string.go
212 lines (194 loc) · 5.36 KB
/
string.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package util
import (
"fmt"
"strings"
"unicode/utf8"
)
// Constants
const (
EscapeReset = EscapeStart + "0" + EscapeStop
EscapeStart = "\x1b["
EscapeStartRune = rune(27) // \x1b
EscapeStop = "m"
EscapeStopRune = 'm'
)
// FixedLengthString returns the given string with a fixed length. For ex.:
// FixedLengthString("Ghost", 0, "~") == "Ghost"
// FixedLengthString("Ghost", 1, "~") == "~"
// FixedLengthString("Ghost", 3, "~") == "Gh~"
// FixedLengthString("Ghost", 5, "~") == "Ghost"
// FixedLengthString("Ghost", 7, "~") == "Ghost "
func FixedLengthString(s string, length int, snipIndicator string) string {
if length > 0 {
lenStr := RuneCountWithoutEscapeSeq(s)
if lenStr < length {
return fmt.Sprintf("%-"+fmt.Sprint(length)+"s", s)
} else if lenStr > length {
lenStrFinal := length - RuneCountWithoutEscapeSeq(snipIndicator)
return TrimTextWithoutEscapeSeq(s, lenStrFinal) + snipIndicator
}
}
return s
}
// GetLongestLineLength returns the length of the longest "line" within the
// argument string. For ex.:
// GetLongestLineLength("Ghost!\nCome back here!\nRight now!") == 15
func GetLongestLineLength(s string) int {
maxLength, currLength, isEscSeq := 0, 0, false
for _, c := range s {
if c == EscapeStartRune {
isEscSeq = true
} else if isEscSeq && c == EscapeStopRune {
isEscSeq = false
continue
}
if c == '\n' {
if currLength > maxLength {
maxLength = currLength
}
currLength = 0
} else if !isEscSeq {
currLength++
}
}
if currLength > maxLength {
return currLength
}
return maxLength
}
// InsertRuneEveryN inserts the rune every N characters in the string. For ex.:
// InsertRuneEveryN("Ghost", '-', 1) == "G-h-o-s-t"
// InsertRuneEveryN("Ghost", '-', 2) == "Gh-os-t"
// InsertRuneEveryN("Ghost", '-', 3) == "Gho-st"
// InsertRuneEveryN("Ghost", '-', 4) == "Ghos-t"
// InsertRuneEveryN("Ghost", '-', 5) == "Ghost"
func InsertRuneEveryN(s string, r rune, n int) string {
sLen := RuneCountWithoutEscapeSeq(s)
var out strings.Builder
out.Grow(sLen + (sLen / n))
outLen, isEscSeq := 0, false
for idx, c := range s {
if c == EscapeStartRune {
isEscSeq = true
}
if !isEscSeq && outLen > 0 && (outLen%n) == 0 && idx != sLen {
out.WriteRune(r)
}
out.WriteRune(c)
if !isEscSeq {
outLen++
}
if isEscSeq && c == EscapeStopRune {
isEscSeq = false
}
}
return out.String()
}
// RepeatAndTrim repeats the given string until it is as long as maxRunes.
// For ex.:
// RepeatAndTrim("Ghost", 0) == ""
// RepeatAndTrim("Ghost", 5) == "Ghost"
// RepeatAndTrim("Ghost", 7) == "GhostGh"
// RepeatAndTrim("Ghost", 10) == "GhostGhost"
func RepeatAndTrim(s string, maxRunes int) string {
if maxRunes == 0 {
return ""
} else if maxRunes == len(s) {
return s
}
return TrimTextWithoutEscapeSeq(strings.Repeat(s, int(maxRunes/utf8.RuneCountInString(s))+1), maxRunes)
}
// RuneCountWithoutEscapeSeq is similar to utf8.RuneCountInString, except for
// the fact that it ignores escape sequences in the counting. For ex.:
// RuneCountWithoutEscapeSeq("") == 0
// RuneCountWithoutEscapeSeq("Ghost") == 5
// RuneCountWithoutEscapeSeq("\x1b[33mGhost\x1b[0m") == 5
// RuneCountWithoutEscapeSeq("\x1b[33mGhost\x1b[0") == 5
func RuneCountWithoutEscapeSeq(s string) int {
count, isEscSeq := 0, false
for _, c := range s {
if c == EscapeStartRune {
isEscSeq = true
} else if isEscSeq {
if c == EscapeStopRune {
isEscSeq = false
}
} else {
count++
}
}
return count
}
// TrimTextWithoutEscapeSeq trims a string to the given length accounting for
// escape sequences For ex.:
// TrimTextWithoutEscapeSeq("Ghost", 3) == "Gho"
// TrimTextWithoutEscapeSeq("Ghost", 6) == "Ghost"
// TrimTextWithoutEscapeSeq("\x1b[33mGhost\x1b[0m", 3) == "\x1b[33mGho\x1b[0m"
// TrimTextWithoutEscapeSeq("\x1b[33mGhost\x1b[0m", 6) == "\x1b[33mGhost\x1b[0m"
func TrimTextWithoutEscapeSeq(s string, n int) string {
if n <= 0 {
return ""
}
var out strings.Builder
out.Grow(n)
outLen, isEscSeq, lastEscSeq := 0, false, strings.Builder{}
for _, sChr := range s {
out.WriteRune(sChr)
if sChr == EscapeStartRune {
isEscSeq = true
lastEscSeq.Reset()
lastEscSeq.WriteRune(sChr)
} else if isEscSeq {
lastEscSeq.WriteRune(sChr)
if sChr == EscapeStopRune {
isEscSeq = false
}
} else {
outLen++
if outLen == n {
break
}
}
}
if lastEscSeq.Len() > 0 && lastEscSeq.String() != EscapeReset {
out.WriteString(EscapeReset)
}
return out.String()
}
// WrapText wraps a string to the given length using a newline. For ex.:
// WrapText("Ghost", 1) == "G\nh\no\ns\nt"
// WrapText("Ghost", 2) == "Gh\nos\nt"
// WrapText("Ghost", 3) == "Gho\nst"
// WrapText("Ghost", 4) == "Ghos\nt"
// WrapText("Ghost", 5) == "Ghost"
// WrapText("Ghost", 6) == "Ghost"
// WrapText("Jon\nSnow", 2) == "Jo\nn\nSn\now"
// WrapText("Jon\nSnow\n", 2) == "Jo\nn\nSn\now\n"
func WrapText(s string, n int) string {
if n <= 0 {
return ""
}
var out strings.Builder
sLen := utf8.RuneCountInString(s)
out.Grow(sLen + (sLen / n))
lineIdx, isEscSeq := 0, false
for idx, c := range s {
if c == EscapeStartRune {
isEscSeq = true
}
if !isEscSeq && lineIdx == n && c != '\n' {
out.WriteRune('\n')
lineIdx = 0
}
out.WriteRune(c)
if c == '\n' {
lineIdx = 0
} else if !isEscSeq && idx < sLen {
lineIdx++
}
if isEscSeq && c == EscapeStopRune {
isEscSeq = false
}
}
return out.String()
}