mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
3 Commits
a774c060ce
...
fee8d325b7
| Author | SHA1 | Date | |
|---|---|---|---|
| fee8d325b7 | |||
| 8784be1583 | |||
| 84cd68de7f |
+47
-1
@@ -33,8 +33,9 @@ import (
|
|||||||
- [UpperKebabCase](#UpperKebabCase)
|
- [UpperKebabCase](#UpperKebabCase)
|
||||||
- [LowerFirst](#LowerFirst)
|
- [LowerFirst](#LowerFirst)
|
||||||
- [UpperFirst](#UpperFirst)
|
- [UpperFirst](#UpperFirst)
|
||||||
- [PadEnd](#PadEnd)
|
- [Pad](#Pad)
|
||||||
- [PadStart](#PadStart)
|
- [PadStart](#PadStart)
|
||||||
|
- [PadEnd](#PadEnd)
|
||||||
- [Reverse](#Reverse)
|
- [Reverse](#Reverse)
|
||||||
- [SnakeCase](#SnakeCase)
|
- [SnakeCase](#SnakeCase)
|
||||||
- [UpperSnakeCase](#UpperSnakeCase)
|
- [UpperSnakeCase](#UpperSnakeCase)
|
||||||
@@ -449,6 +450,51 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Pad">Pad</span>
|
||||||
|
|
||||||
|
<p>Pads string on the left and right side if it's shorter than size.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Pad(source string, size int, padStr string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.Pad("foo", 1, "bar")
|
||||||
|
result2 := strutil.Pad("foo", 2, "bar")
|
||||||
|
result3 := strutil.Pad("foo", 3, "bar")
|
||||||
|
result4 := strutil.Pad("foo", 4, "bar")
|
||||||
|
result5 := strutil.Pad("foo", 5, "bar")
|
||||||
|
result6 := strutil.Pad("foo", 6, "bar")
|
||||||
|
result7 := strutil.Pad("foo", 7, "bar")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
// Output:
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
|
// foob
|
||||||
|
// bfoob
|
||||||
|
// bfooba
|
||||||
|
// bafooba
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="PadEnd">PadEnd</span>
|
### <span id="PadEnd">PadEnd</span>
|
||||||
|
|
||||||
<p>Pads string on the right side if it's shorter than size.</p>
|
<p>Pads string on the right side if it's shorter than size.</p>
|
||||||
|
|||||||
@@ -449,6 +449,51 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Pad">Pad</span>
|
||||||
|
|
||||||
|
<p>如果字符串长度短于size,则在左右两侧填充字符串。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Pad(source string, size int, padStr string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.Pad("foo", 1, "bar")
|
||||||
|
result2 := strutil.Pad("foo", 2, "bar")
|
||||||
|
result3 := strutil.Pad("foo", 3, "bar")
|
||||||
|
result4 := strutil.Pad("foo", 4, "bar")
|
||||||
|
result5 := strutil.Pad("foo", 5, "bar")
|
||||||
|
result6 := strutil.Pad("foo", 6, "bar")
|
||||||
|
result7 := strutil.Pad("foo", 7, "bar")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
// Output:
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
|
// foob
|
||||||
|
// bfoob
|
||||||
|
// bfooba
|
||||||
|
// bafooba
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="PadEnd">PadEnd</span>
|
### <span id="PadEnd">PadEnd</span>
|
||||||
|
|
||||||
<p>如果字符串长度短于size,则在右侧填充字符串。</p>
|
<p>如果字符串长度短于size,则在右侧填充字符串。</p>
|
||||||
|
|||||||
+11
-30
@@ -67,44 +67,25 @@ func LowerFirst(s string) string {
|
|||||||
return string(r) + s[size:]
|
return string(r) + s[size:]
|
||||||
}
|
}
|
||||||
|
|
||||||
// PadEnd pads string on the right side if it's shorter than size.
|
// PadStart pads string on the left and right side if it's shorter than size.
|
||||||
// Padding characters are truncated if they exceed size.
|
// Padding characters are truncated if they exceed size.
|
||||||
// Play: https://go.dev/play/p/9xP8rN0vz--
|
// Play: todo
|
||||||
func PadEnd(source string, size int, padStr string) string {
|
func Pad(source string, size int, padStr string) string {
|
||||||
len1 := len(source)
|
return padAtPosition(source, size, padStr, 0)
|
||||||
len2 := len(padStr)
|
|
||||||
|
|
||||||
if len1 >= size {
|
|
||||||
return source
|
|
||||||
}
|
|
||||||
|
|
||||||
fill := ""
|
|
||||||
if len2 >= size-len1 {
|
|
||||||
fill = padStr[0 : size-len1]
|
|
||||||
} else {
|
|
||||||
fill = strings.Repeat(padStr, size-len1)
|
|
||||||
}
|
|
||||||
return source + fill[0:size-len1]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PadStart pads string on the left side if it's shorter than size.
|
// PadStart pads string on the left side if it's shorter than size.
|
||||||
// Padding characters are truncated if they exceed size.
|
// Padding characters are truncated if they exceed size.
|
||||||
// Play: https://go.dev/play/p/xpTfzArDfvT
|
// Play: https://go.dev/play/p/xpTfzArDfvT
|
||||||
func PadStart(source string, size int, padStr string) string {
|
func PadStart(source string, size int, padStr string) string {
|
||||||
len1 := len(source)
|
return padAtPosition(source, size, padStr, 1)
|
||||||
len2 := len(padStr)
|
}
|
||||||
|
|
||||||
if len1 >= size {
|
// PadEnd pads string on the right side if it's shorter than size.
|
||||||
return source
|
// Padding characters are truncated if they exceed size.
|
||||||
}
|
// Play: https://go.dev/play/p/9xP8rN0vz--
|
||||||
|
func PadEnd(source string, size int, padStr string) string {
|
||||||
fill := ""
|
return padAtPosition(source, size, padStr, 2)
|
||||||
if len2 >= size-len1 {
|
|
||||||
fill = padStr[0 : size-len1]
|
|
||||||
} else {
|
|
||||||
fill = strings.Repeat(padStr, size-len1)
|
|
||||||
}
|
|
||||||
return fill[0:size-len1] + source
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// KebabCase coverts string to kebab-case, non letters and numbers will be ignored.
|
// KebabCase coverts string to kebab-case, non letters and numbers will be ignored.
|
||||||
|
|||||||
@@ -186,6 +186,33 @@ func ExampleUpperFirst() {
|
|||||||
// Bar大
|
// Bar大
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExamplePad() {
|
||||||
|
result1 := Pad("foo", 1, "bar")
|
||||||
|
result2 := Pad("foo", 2, "bar")
|
||||||
|
result3 := Pad("foo", 3, "bar")
|
||||||
|
result4 := Pad("foo", 4, "bar")
|
||||||
|
result5 := Pad("foo", 5, "bar")
|
||||||
|
result6 := Pad("foo", 6, "bar")
|
||||||
|
result7 := Pad("foo", 7, "bar")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
|
// foob
|
||||||
|
// bfoob
|
||||||
|
// bfooba
|
||||||
|
// bafooba
|
||||||
|
}
|
||||||
|
|
||||||
func ExamplePadEnd() {
|
func ExamplePadEnd() {
|
||||||
result1 := PadEnd("foo", 1, "bar")
|
result1 := PadEnd("foo", 1, "bar")
|
||||||
result2 := PadEnd("foo", 2, "bar")
|
result2 := PadEnd("foo", 2, "bar")
|
||||||
|
|||||||
@@ -98,3 +98,40 @@ func toUpperAll(rs []rune) []rune {
|
|||||||
}
|
}
|
||||||
return rs
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -168,9 +168,19 @@ func TestLowerFirst(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPad(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestPad")
|
||||||
|
|
||||||
|
assert.Equal("a ", Pad("a", 2, ""))
|
||||||
|
assert.Equal("a", Pad("a", 1, "b"))
|
||||||
|
assert.Equal("ab", Pad("a", 2, "b"))
|
||||||
|
assert.Equal("mabcdm", Pad("abcd", 6, "m"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestPadEnd(t *testing.T) {
|
func TestPadEnd(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestPadEnd")
|
assert := internal.NewAssert(t, "TestPadEnd")
|
||||||
|
|
||||||
|
assert.Equal("a ", PadEnd("a", 2, " "))
|
||||||
assert.Equal("a", PadEnd("a", 1, "b"))
|
assert.Equal("a", PadEnd("a", 1, "b"))
|
||||||
assert.Equal("ab", PadEnd("a", 2, "b"))
|
assert.Equal("ab", PadEnd("a", 2, "b"))
|
||||||
assert.Equal("abcdmn", PadEnd("abcd", 6, "mno"))
|
assert.Equal("abcdmn", PadEnd("abcd", 6, "mno"))
|
||||||
|
|||||||
Reference in New Issue
Block a user