1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add Shuffle for string

This commit is contained in:
dudaodong
2024-09-03 15:54:05 +08:00
parent c32a19868d
commit 63216d9b1c
4 changed files with 82 additions and 1 deletions

View File

@@ -64,6 +64,7 @@ import (
- [HammingDistance](#HammingDistance) - [HammingDistance](#HammingDistance)
- [Concat](#Concat) - [Concat](#Concat)
- [Ellipsis](#Ellipsis) - [Ellipsis](#Ellipsis)
- [Shuffle](#Shuffle)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -1576,7 +1577,7 @@ func main() {
func Ellipsis(str string, length int) string func Ellipsis(str string, length int) string
``` ```
<b>示例:<span style="float:right;display:inline-block;">[Run]()</span></b> <b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>
```go ```go
import ( import (
@@ -1598,4 +1599,28 @@ func main() {
// 你好... // 你好...
// 😀😃😄... // 😀😃😄...
} }
```
### <span id="Shuffle">Shuffle</span>
<p>打乱给定字符串中的字符顺序。</p>
<b>函数签名:</b>
```go
func Shuffle(str string) string
```
<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
result := strutil.Shuffle("hello")
fmt.Println(result) //olelh (random order)
}
``` ```

View File

@@ -64,6 +64,7 @@ import (
- [HammingDistance](#HammingDistance) - [HammingDistance](#HammingDistance)
- [Concat](#Concat) - [Concat](#Concat)
- [Ellipsis](#Ellipsis) - [Ellipsis](#Ellipsis)
- [Shuffle](#Shuffle)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -1600,4 +1601,28 @@ func main() {
// 你好... // 你好...
// 😀😃😄... // 😀😃😄...
} }
```
### <span id="Shuffle">Shuffle</span>
<p>Shuffle the order of characters of given string.</p>
<b>Signature:</b>
```go
func Shuffle(str string) 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() {
result := strutil.Shuffle("hello")
fmt.Println(result) //olelh (random order)
}
``` ```

View File

@@ -5,13 +5,18 @@ package strutil
import ( import (
"errors" "errors"
"math/rand"
"regexp" "regexp"
"strings" "strings"
"time"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"unsafe" "unsafe"
) )
// used in `Shuffle` function
var rng = rand.New(rand.NewSource(int64(time.Now().UnixNano())))
// CamelCase coverts string to camelCase string. Non letters and numbers will be ignored. // CamelCase coverts string to camelCase string. Non letters and numbers will be ignored.
// Play: https://go.dev/play/p/9eXP3tn2tUy // Play: https://go.dev/play/p/9eXP3tn2tUy
func CamelCase(s string) string { func CamelCase(s string) string {
@@ -658,3 +663,16 @@ func Ellipsis(str string, length int) string {
return string(runes[:length]) + "..." return string(runes[:length]) + "..."
} }
// Shuffle the order of characters of given string.
// Play: todo
func Shuffle(str string) string {
runes := []rune(str)
for i := len(runes) - 1; i > 0; i-- {
j := rng.Intn(i + 1)
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}

View File

@@ -728,3 +728,16 @@ func TestEllipsis(t *testing.T) {
assert.Equal(tt.want, Ellipsis(tt.input, tt.length)) assert.Equal(tt.want, Ellipsis(tt.input, tt.length))
} }
} }
func TestShuffle(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestShuffle")
assert.Equal("", Shuffle(""))
assert.Equal("a", Shuffle("a"))
str := "hello"
shuffledStr := Shuffle(str)
assert.Equal(5, len(shuffledStr))
}