-
Notifications
You must be signed in to change notification settings - Fork 1
/
stripcol.go
39 lines (36 loc) · 1.06 KB
/
stripcol.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
package stripcol
func StripColor(text string) string {
strippedText := make([]byte, 0, len(text))
// Iterate through the text character by character
for i := 0; i < len(text); i++ {
if text[i] != 27 {
strippedText = append(strippedText, text[i])
} else {
// Check if the current character is an escape character
if i+1 < len(text) && text[i+1] == '[' {
i += 2
// Check if the following characters are digits
for i < len(text) && text[i] >= '0' && text[i] <= '9' {
i++
}
// Check if the next character is ';'
for i < len(text) && text[i] == ';' {
i++
// Check if the following characters are digits
for i < len(text) && text[i] >= '0' && text[i] <= '9' {
i++
}
}
// Check if the next character is 'm'
if i < len(text) && text[i] == 'm' {
continue
}
// If the next character is not 'm', add the escape character to the strippedText
strippedText = append(strippedText, text[i-1])
} else {
strippedText = append(strippedText, text[i])
}
}
}
return string(strippedText)
}