mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
feat: add Rotate for string
This commit is contained in:
@@ -65,6 +65,7 @@ import (
|
|||||||
- [Concat](#Concat)
|
- [Concat](#Concat)
|
||||||
- [Ellipsis](#Ellipsis)
|
- [Ellipsis](#Ellipsis)
|
||||||
- [Shuffle](#Shuffle)
|
- [Shuffle](#Shuffle)
|
||||||
|
- [Rotate](#Rotate)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -1553,17 +1554,17 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
result1 := strutil.Concat(12, "Hello", " ", "World", "!")
|
result1 := strutil.Concat(12, "Hello", " ", "World", "!")
|
||||||
result2 := strutil.Concat(11, "Go", " ", "Language")
|
result2 := strutil.Concat(11, "Go", " ", "Language")
|
||||||
result3 := strutil.Concat(0, "An apple a ", "day,", "keeps the", " doctor away")
|
result3 := strutil.Concat(0, "An apple a ", "day,", "keeps the", " doctor away")
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// Hello World!
|
// Hello World!
|
||||||
// Go Language
|
// Go Language
|
||||||
// An apple a day,keeps the doctor away
|
// An apple a day,keeps the doctor away
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1623,4 +1624,38 @@ func main() {
|
|||||||
result := strutil.Shuffle("hello")
|
result := strutil.Shuffle("hello")
|
||||||
fmt.Println(result) //olelh (random order)
|
fmt.Println(result) //olelh (random order)
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Rotate">Rotate</span>
|
||||||
|
|
||||||
|
<p>打乱给定字符串中的字符顺序。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Rotate(str string, shift int) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := Rotate("Hello", 0)
|
||||||
|
result2 := Rotate("Hello", 1)
|
||||||
|
result3 := Rotate("Hello", 2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Hello
|
||||||
|
// oHell
|
||||||
|
// loHel
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -65,6 +65,7 @@ import (
|
|||||||
- [Concat](#Concat)
|
- [Concat](#Concat)
|
||||||
- [Ellipsis](#Ellipsis)
|
- [Ellipsis](#Ellipsis)
|
||||||
- [Shuffle](#Shuffle)
|
- [Shuffle](#Shuffle)
|
||||||
|
- [Rotate](#Rotate)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -1625,4 +1626,37 @@ func main() {
|
|||||||
result := strutil.Shuffle("hello")
|
result := strutil.Shuffle("hello")
|
||||||
fmt.Println(result) //olelh (random order)
|
fmt.Println(result) //olelh (random order)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Rotate">Rotate</span>
|
||||||
|
|
||||||
|
<p>Rotates the string by the specified number of characters.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Rotate(str string, shift int) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block;">[Run]()</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := Rotate("Hello", 0)
|
||||||
|
result2 := Rotate("Hello", 1)
|
||||||
|
result3 := Rotate("Hello", 2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Hello
|
||||||
|
// oHell
|
||||||
|
// loHel
|
||||||
|
}
|
||||||
@@ -676,3 +676,31 @@ func Shuffle(str string) string {
|
|||||||
|
|
||||||
return string(runes)
|
return string(runes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rotate rotates the string by the specified number of characters.
|
||||||
|
// Play: todo
|
||||||
|
func Rotate(str string, shift int) string {
|
||||||
|
if shift == 0 {
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
runes := []rune(str)
|
||||||
|
length := len(runes)
|
||||||
|
if length == 0 {
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
shift = shift % length
|
||||||
|
|
||||||
|
if shift < 0 {
|
||||||
|
shift = length + shift
|
||||||
|
}
|
||||||
|
|
||||||
|
var sb strings.Builder
|
||||||
|
sb.Grow(length)
|
||||||
|
|
||||||
|
sb.WriteString(string(runes[length-shift:]))
|
||||||
|
sb.WriteString(string(runes[:length-shift]))
|
||||||
|
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|||||||
@@ -709,3 +709,18 @@ func ExampleEllipsis() {
|
|||||||
// 你好...
|
// 你好...
|
||||||
// 😀😃😄...
|
// 😀😃😄...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleRotate() {
|
||||||
|
result1 := Rotate("Hello", 0)
|
||||||
|
result2 := Rotate("Hello", 1)
|
||||||
|
result3 := Rotate("Hello", 2)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Hello
|
||||||
|
// oHell
|
||||||
|
// loHel
|
||||||
|
}
|
||||||
|
|||||||
@@ -710,9 +710,9 @@ func TestEllipsis(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestEllipsis")
|
assert := internal.NewAssert(t, "TestEllipsis")
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
input string
|
input string
|
||||||
length int
|
length int
|
||||||
want string
|
expected string
|
||||||
}{
|
}{
|
||||||
{"", 0, ""},
|
{"", 0, ""},
|
||||||
{"hello world", 0, ""},
|
{"hello world", 0, ""},
|
||||||
@@ -725,7 +725,7 @@ func TestEllipsis(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
assert.Equal(tt.want, Ellipsis(tt.input, tt.length))
|
assert.Equal(tt.expected, Ellipsis(tt.input, tt.length))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -741,3 +741,28 @@ func TestShuffle(t *testing.T) {
|
|||||||
shuffledStr := Shuffle(str)
|
shuffledStr := Shuffle(str)
|
||||||
assert.Equal(5, len(shuffledStr))
|
assert.Equal(5, len(shuffledStr))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRotate(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestRotate")
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
input string
|
||||||
|
shift int
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{"", 1, ""},
|
||||||
|
{"a", 0, "a"},
|
||||||
|
{"a", 1, "a"},
|
||||||
|
{"a", -1, "a"},
|
||||||
|
|
||||||
|
{"Hello", -2, "lloHe"},
|
||||||
|
{"Hello", 1, "oHell"},
|
||||||
|
{"Hello, world!", 3, "ld!Hello, wor"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
assert.Equal(tt.expected, Rotate(tt.input, tt.shift))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user