Skip to content

Commit

Permalink
feat: add Shuffle for string
Browse files Browse the repository at this point in the history
  • Loading branch information
duke-git committed Sep 3, 2024
1 parent c32a198 commit 63216d9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
27 changes: 26 additions & 1 deletion docs/api/packages/strutil.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import (
- [HammingDistance](#HammingDistance)
- [Concat](#Concat)
- [Ellipsis](#Ellipsis)
- [Shuffle](#Shuffle)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -1576,7 +1577,7 @@ func main() {
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
import (
Expand All @@ -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)
}
```
25 changes: 25 additions & 0 deletions docs/en/api/packages/strutil.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import (
- [HammingDistance](#HammingDistance)
- [Concat](#Concat)
- [Ellipsis](#Ellipsis)
- [Shuffle](#Shuffle)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -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)
}
```
18 changes: 18 additions & 0 deletions strutil/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ package strutil

import (
"errors"
"math/rand"
"regexp"
"strings"
"time"
"unicode"
"unicode/utf8"
"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.
// Play: https://go.dev/play/p/9eXP3tn2tUy
func CamelCase(s string) string {
Expand Down Expand Up @@ -658,3 +663,16 @@ func Ellipsis(str string, length int) string {

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)
}
13 changes: 13 additions & 0 deletions strutil/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,3 +728,16 @@ func TestEllipsis(t *testing.T) {
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))
}

0 comments on commit 63216d9

Please sign in to comment.