-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrim.go
More file actions
28 lines (23 loc) · 613 Bytes
/
trim.go
File metadata and controls
28 lines (23 loc) · 613 Bytes
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
package text
import (
"strings"
"unicode"
)
// TrimSpace remove the leading and trailing whitespace while ignoring the
// terminal escape sequences.
// Returns the number of trimmed space on both side.
func TrimSpace(line string) string {
cleaned, escapes := ExtractTermEscapes(line)
// trim left while counting
left := 0
trimmed := strings.TrimLeftFunc(cleaned, func(r rune) bool {
if unicode.IsSpace(r) {
left++
return true
}
return false
})
trimmed = strings.TrimRightFunc(trimmed, unicode.IsSpace)
escapes = OffsetEscapes(escapes, -left)
return ApplyTermEscapes(trimmed, escapes)
}