-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathpad.go
26 lines (23 loc) · 845 Bytes
/
pad.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
package strutil
// PadLeft left pads a string str with "pad". The string is padded to
// the size of width.
func PadLeft(str string, width int, pad string) string {
return Tile(pad, width-Len(str)) + str
}
// PadRight right pads a string str with "pad". The string is padded to
// the size of width.
func PadRight(str string, width int, pad string) string {
return str + Tile(pad, width-Len(str))
}
// Pad left and right pads a string str with leftPad and rightPad. The string
// is padded to the size of width.
func Pad(str string, width int, leftPad string, rightPad string) string {
switch {
case Len(leftPad) == 0:
return PadRight(str, width, rightPad)
case Len(rightPad) == 0:
return PadLeft(str, width, leftPad)
}
padLen := (width - Len(str)) / 2
return Tile(leftPad, padLen) + str + Tile(rightPad, width-Len(str)-padLen)
}