-
Notifications
You must be signed in to change notification settings - Fork 2
/
padding.go
50 lines (43 loc) · 940 Bytes
/
padding.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
package utils
import (
"strings"
)
// Only kept for legacy compliance
const (
// LEFT_PADDING_CHARACTER ...
LEFT_PADDING_CHARACTER rune = 2 // Unicode U+0002: start of text
// RIGHT_PADDING_CHARACTER ...
RIGHT_PADDING_CHARACTER rune = 3 // Unicode U+0003: end of text
)
// LeftPad ...
func LeftPad(str string, minLength int) string {
if len(str) >= minLength {
return str
}
for len(str) < minLength {
str = string(LEFT_PADDING_CHARACTER) + str
}
return str
}
// RightPad ...
func RightPad(str string, minLength int) string {
if len(str) >= minLength {
return str
}
for len(str) < minLength {
str = str + string(RIGHT_PADDING_CHARACTER)
}
return str
}
// Unpad ...
func Unpad(str string) string {
if len(str) > 0 {
for strings.HasPrefix(str, string(LEFT_PADDING_CHARACTER)) {
str = str[1:]
}
for strings.HasSuffix(str, string(RIGHT_PADDING_CHARACTER)) {
str = str[:len(str)-1]
}
}
return str
}