mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-08 14:42:27 +08:00
refactor: refact some strutil functions
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package strutil
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
@@ -111,31 +112,27 @@ func padAtPosition(str string, length int, padStr string, position int) string {
|
||||
padStr = " "
|
||||
}
|
||||
|
||||
length = length - len(str)
|
||||
startPadLen := 0
|
||||
totalPad := length - len(str)
|
||||
startPad := 0
|
||||
|
||||
if position == 0 {
|
||||
startPadLen = length / 2
|
||||
startPad = totalPad / 2
|
||||
} else if position == 1 {
|
||||
startPadLen = length
|
||||
startPad = totalPad
|
||||
} else if position == 2 {
|
||||
startPad = 0
|
||||
}
|
||||
endPadLen := length - startPadLen
|
||||
endPad := totalPad - startPad
|
||||
|
||||
charLen := len(padStr)
|
||||
leftPad := ""
|
||||
cur := 0
|
||||
for cur < startPadLen {
|
||||
leftPad += string(padStr[cur%charLen])
|
||||
cur++
|
||||
repeatPad := func(n int) string {
|
||||
repeated := strings.Repeat(padStr, (n+len(padStr)-1)/len(padStr))
|
||||
return repeated[:n]
|
||||
}
|
||||
|
||||
cur = 0
|
||||
rightPad := ""
|
||||
for cur < endPadLen {
|
||||
rightPad += string(padStr[cur%charLen])
|
||||
cur++
|
||||
}
|
||||
left := repeatPad(startPad)
|
||||
right := repeatPad(endPad)
|
||||
|
||||
return leftPad + str + rightPad
|
||||
return left + str + right
|
||||
}
|
||||
|
||||
// isLetter checks r is a letter but not CJK character.
|
||||
|
||||
Reference in New Issue
Block a user