1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-07 06:02:27 +08:00

fix: fix bugs of PadEnd and PadStart

This commit is contained in:
dudaodong
2023-02-19 12:34:39 +08:00
parent a774c060ce
commit 84cd68de7f
3 changed files with 45 additions and 33 deletions

View File

@@ -98,3 +98,40 @@ func toUpperAll(rs []rune) []rune {
}
return rs
}
// padWithPosition pads string
func padAtPosition(str string, length int, padStr string, position int) string {
if len(str) >= length {
return str
}
if padStr == "" {
padStr = " "
}
length = length - len(str)
startPadLen := 0
if position == 0 {
startPadLen = length / 2
} else if position == 1 {
startPadLen = length
}
endPadLen := length - startPadLen
charLen := len(padStr)
leftPad := ""
cur := 0
for cur < startPadLen {
leftPad += string(padStr[cur%charLen])
cur++
}
cur = 0
rightPad := ""
for cur < endPadLen {
rightPad += string(padStr[cur%charLen])
cur++
}
return leftPad + str + rightPad
}