-
Notifications
You must be signed in to change notification settings - Fork 0
/
lines.go
249 lines (215 loc) · 7.28 KB
/
lines.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package tplfuncs
import (
"bufio"
"fmt"
htmlTemplate "html/template"
"regexp"
"sort"
"strings"
textTemplate "text/template"
)
// LinesHelpers returns a text template FuncMap with functions related to line processing
func LinesHelpers() textTemplate.FuncMap {
return textTemplate.FuncMap{
"getLines": getLinesFunc,
"line": lineFunc,
"lineOrErr": lineOrErrFunc,
"head": headFunc,
"skipHead": skipHeadFunc,
"tail": tailFunc,
"skipTail": skipTailFunc,
"sortLines": sortLinesFunc,
"trimLines": trimLinesFunc,
"trimAll": trimAllFunc,
"wrapLines": wrapLinesFunc,
"withoutLineComments": withoutLineCommentsFunc,
"withoutEmptyLines": withoutEmptyLinesFunc,
"match": matchFunc,
"notMatch": notMatchFunc,
"regexpReplaceLine": regexpReplaceLineFunc,
"joinLines": asString,
"indentSpaceLines": indentSpaceLinesFunc,
"indentTabLines": indentTabLinesFunc,
"prefixLines": prefixLinesFunc,
}
}
// LinesHelpersHTML returns an HTML template FuncMap with functions related to line processing
func LinesHelpersHTML() htmlTemplate.FuncMap {
return htmlTemplate.FuncMap(LinesHelpers())
}
// Doc: `lineOrErr` returns a single line from the multiline input. The index is 1-based. Returns an error, if the line does not exist.
func lineOrErrFunc(number int, input string) (string, error) {
lines := getLines(input)
if number < 1 || number > len(lines) {
return "", fmt.Errorf("line ")
}
return lines[number-1], nil
}
// Doc: `line` returns a single line from the multiline input. The index is 1-based. Returns an empty string, if the line does not exist.
func lineFunc(number int, input string) string {
lines := getLines(input)
if number < 1 || number > len(lines) {
return ""
}
return lines[number-1]
}
// Doc: Return the multiline input sorted alphabetically line by line.
func sortLinesFunc(input string) string {
lines := getLines(input)
sort.Strings(lines)
return asString(lines)
}
// Doc: `head` returns the first n lines of a multiline string as one string, or all of it if there is less than n lines in total.
func headFunc(count int, input string) string {
lines := getLines(input)
if count < len(lines) {
lines = lines[:count]
}
return asString(lines)
}
// Doc: `skipHead` returns the multiline string given without the first n lines or an empty string if there is less than n lines in total.
func skipHeadFunc(count int, input string) string {
lines := getLines(input)
if count < len(lines) {
lines = lines[count:]
}
return asString(lines)
}
// Doc: `tail` returns the last n lines of a multiline string as one string, or all of it if there is less than n lines in total.
func tailFunc(count int, input string) string {
lines := getLines(input)
if count < len(lines) {
lines = lines[len(lines)-count:]
}
return asString(lines)
}
// Doc: `skipTail` returns the multiline string given without the last n lines or an empty string if there is less than n lines in total.
func skipTailFunc(count int, input string) string {
lines := getLines(input)
if count < len(lines) {
lines = lines[0 : len(lines)-count]
}
return asString(lines)
}
// Doc: `trimLines` returns the multiline string given without leading and trailing empty lines.
func trimLinesFunc(input string) string {
lines := getLines(input)
var (
firstContentLine = 0
lastContentLine = len(lines)
)
// leading
for i, l := range lines {
if strings.TrimSpace(l) != "" {
break
}
firstContentLine = i + 1
}
// trailing
for i := len(lines) - 1; i >= 0; i-- {
if strings.TrimSpace(lines[i]) != "" {
break
}
lastContentLine = i
}
return asString(lines[firstContentLine:lastContentLine])
}
// Doc: `trimAll` returns the multiline string given with leading and trailing space removed for any line individually.
func trimAllFunc(input string) string {
lines := getLines(input)
for i, l := range lines {
lines[i] = strings.TrimSpace(l)
}
return asString(lines)
}
// Doc: `notMatch` does return a string with all lines from the given multiline string that do not match the regexp given.
func notMatchFunc(regExp, input string) string {
lines := getLines(input)
r := regexp.MustCompile(regExp)
result := make([]string, 0)
for _, line := range lines {
if r.MatchString(line) {
continue
}
result = append(result, line)
}
return asString(result)
}
// Doc: `match` does return a string with all lines from the given multiline string that do match the regexp given.
func matchFunc(regExp, input string) string {
lines := getLines(input)
r := regexp.MustCompile(regExp)
result := make([]string, 0)
for _, line := range lines {
if !r.MatchString(line) {
continue
}
result = append(result, line)
}
return asString(result)
}
// Doc: `withoutEmptyLines` returns the multiline string given without empty lines.
func withoutEmptyLinesFunc(input string) string {
return notMatchFunc(`^\s*$`, input)
}
// Doc: `withoutLineComments` returns the multiline string given without line comments (lines starting with optional whitespace and // or #).
func withoutLineCommentsFunc(input string) string {
return notMatchFunc(`^\s*(//|#)`, input)
}
// Doc: `wrapLines` returns the multiline string with every single line wrapped with the given leading and trailing string.
func wrapLinesFunc(leading, trailing, input string) string {
lines := getLines(input)
for i, line := range lines {
lines[i] = leading + line + trailing
}
return asString(lines)
}
// Doc: `indentSpaceLines` returns the multiline string given with every line indented by additional n spaces.
func indentSpaceLinesFunc(spaceCount int, input string) string {
lines := getLines(input)
for i, line := range lines {
lines[i] = strings.Repeat(" ", spaceCount) + line
}
return asString(lines)
}
// Doc: `prefixLines` returns the multiline string given with every line prefixed with the string given.
func prefixLinesFunc(prefix string, input string) string {
lines := getLines(input)
for i, line := range lines {
lines[i] = prefix + line
}
return asString(lines)
}
// Doc: `indentTabLines` returns the multiline string given with every line indented by additional n tab characters.
func indentTabLinesFunc(tabCount int, input string) string {
lines := getLines(input)
for i, line := range lines {
lines[i] = strings.Repeat("\n", tabCount) + line
}
return asString(lines)
}
// Doc: `getLines` returns the individual lines of a multiline string.
func getLinesFunc(input string) []string {
return getLines(input)
}
func getLines(input string) []string {
var lines []string
s := bufio.NewScanner(strings.NewReader(input))
for s.Scan() {
lines = append(lines, s.Text())
}
return lines
}
// Doc: `asString` returns a string separated by newline characters from a string slice.
func asString(lines []string) string {
return strings.Join(lines, "\n")
}
// Doc: `regexpReplaceLine` returns a string from a multiline string where the regexp given is executed on every single line and the replacement executed if there was one or more matches.
func regexpReplaceLineFunc(regExp, replacement, input string) string {
lines := getLines(input)
r := regexp.MustCompile(regExp)
for i, line := range lines {
lines[i] = r.ReplaceAllString(line, replacement)
}
return asString(lines)
}