Compare commits

...
5 Commits
Author SHA1 Message Date
dudaodong 3d1bd08434 doc: update doc for new functions 2024-03-01 22:38:57 +08:00
dudaodong a62ad71791 Merge branch 'rc' into v2 2024-03-01 21:45:08 +08:00
235d2f2486 修复MAXIMUM_CAPACITY超过int最大值 (#196)
* refactor:refactor random function

* fix:fix random function

---------

Co-authored-by: zhuhebin <zhuhebin@fengtaisec.com>
2024-03-01 15:52:49 +08:00
dudaodong e9380a3d9f fix: fix unused parameters vet issue 2024-03-01 10:05:28 +08:00
dudaodong 7290296849 update Contributing Guide 2024-02-29 11:49:34 +08:00
10 changed files with 96 additions and 7 deletions
+1 -1
View File
@@ -30,7 +30,7 @@ We are excited that you are interested in contributing to lancet. Before submitt
- Before submitting a PR, please execute the unit test command: `go test -v ./...` to ensure that all unit test tasks should pass. - Before submitting a PR, please execute the unit test command: `go test -v ./...` to ensure that all unit test tasks should pass.
- Make sure PRs are created to `v2` branch instead of `master` branch. - Make sure PRs are created to `rc` branch instead of `master` branch.
- If your PR fixes a bug, please provide a description about the related bug. - If your PR fixes a bug, please provide a description about the related bug.
+1 -1
View File
@@ -30,7 +30,7 @@ Lancet 的成长离不开大家的支持,如果你愿意为 Lancet 贡献代
- 提交 PR 前请执行单元测试命令:go test -v ./...,确保所有单元测试任务通过。 - 提交 PR 前请执行单元测试命令:go test -v ./...,确保所有单元测试任务通过。
- 确保 PR 是提交到 `v2` 分支,而不是 `main` 分支。 - 确保 PR 是提交到 `rc` 分支,而不是其他分支。
- 如果是修复 bug,请在 PR 中给出描述信息。 - 如果是修复 bug,请在 PR 中给出描述信息。
+12
View File
@@ -318,6 +318,14 @@ import "github.com/duke-git/lancet/v2/convertor"
- **<big>GbkToUtf8</big>** : converts GBK encoding data to utf8 encoding data. - **<big>GbkToUtf8</big>** : converts GBK encoding data to utf8 encoding data.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#GbkToUtf8)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#GbkToUtf8)]
[[play](https://go.dev/play/p/OphmHCN_9u8)] [[play](https://go.dev/play/p/OphmHCN_9u8)]
- **<big>ToStdBase64</big>** : converts a value to a string encoded in standard Base64.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#ToStdBase64)]
- **<big>ToUrlBase64</big>** : converts a value to a string encoded in url Base64.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#ToUrlBase64)]
- **<big>ToRawStdBase64</big>** : converts a value to a string encoded in raw standard Base64.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#ToRawStdBase64)]
- **<big>ToRawUrlBase64</big>** : converts a value to a string encoded in raw url Base64.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#ToRawUrlBase64)]
<h3 id="cryptor"> 6. Cryptor package is for data encryption and decryption.&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="cryptor"> 6. Cryptor package is for data encryption and decryption.&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
@@ -1364,6 +1372,8 @@ import "github.com/duke-git/lancet/v2/slice"
- **<big>Random</big>** : get a random item of slice, return its index, when slice is empty, return -1. - **<big>Random</big>** : get a random item of slice, return its index, when slice is empty, return -1.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Random)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Random)]
[[play](https://go.dev/play/p/UzpGQptWppw)] [[play](https://go.dev/play/p/UzpGQptWppw)]
- **<big>SetToDefaultIf</big>** : set elements to their default value if they match the given predicate.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#SetToDefaultIf)]
<h3 id="stream"> 19. Stream package implements a sequence of elements supporting sequential and operations. this package is an experiment to explore if stream in go can work as the way java does. its function is very limited. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="stream"> 19. Stream package implements a sequence of elements supporting sequential and operations. this package is an experiment to explore if stream in go can work as the way java does. its function is very limited. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
@@ -1609,6 +1619,8 @@ import "github.com/duke-git/lancet/v2/strutil"
- **<big>RemoveWhiteSpace</big>** : remove whitespace characters from a string. - **<big>RemoveWhiteSpace</big>** : remove whitespace characters from a string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#RemoveWhiteSpace)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#RemoveWhiteSpace)]
[[play](https://go.dev/play/p/HzLC9vsTwkf)] [[play](https://go.dev/play/p/HzLC9vsTwkf)]
- **<big>SubInBetween</big>** : return substring between the start and end position(excluded) of source string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#SubInBetween)]
<h3 id="system"> 22. System package contain some functions about os, runtime, shell command. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="system"> 22. System package contain some functions about os, runtime, shell command. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
+14
View File
@@ -317,6 +317,15 @@ import "github.com/duke-git/lancet/v2/convertor"
- **<big>GbkToUtf8</big>** : GBK 编码转 utf8 编码。 - **<big>GbkToUtf8</big>** : GBK 编码转 utf8 编码。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/convertor.md#GbkToUtf8)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/convertor.md#GbkToUtf8)]
[[play](https://go.dev/play/p/OphmHCN_9u8)] [[play](https://go.dev/play/p/OphmHCN_9u8)]
- **<big>ToStdBase64</big>** : 将值转换为StdBase64编码的字符串。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/convertor.md#ToStdBase64)]
- **<big>ToUrlBase64</big>** : 将值转换为url Base64编码的字符串。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/convertor.md#ToUrlBase64)]
- **<big>ToRawStdBase64</big>** : 将值转换为RawStdBase64编码的字符串。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/convertor.md#ToRawStdBase64)]
- **<big>ToRawUrlBase64</big>** : 将值转换为RawUrlBase64编码的字符串。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/convertor.md#ToRawUrlBase64)]
<h3 id="cryptor"> 6. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="cryptor"> 6. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
@@ -1358,6 +1367,8 @@ import "github.com/duke-git/lancet/v2/slice"
- **<big>Random</big>** : 随机返回切片中元素以及下标, 当切片长度为0时返回下标-1。 - **<big>Random</big>** : 随机返回切片中元素以及下标, 当切片长度为0时返回下标-1。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Random)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Random)]
[[play](https://go.dev/play/p/UzpGQptWppw)] [[play](https://go.dev/play/p/UzpGQptWppw)]
- **<big>SetToDefaultIf</big>** : 根据给定给定的predicate判定函数来修改切片中的元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#SetToDefaultIf)]
@@ -1607,6 +1618,9 @@ import "github.com/duke-git/lancet/v2/strutil"
- **<big>RemoveWhiteSpace</big>** : 删除字符串中的空格。 - **<big>RemoveWhiteSpace</big>** : 删除字符串中的空格。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#RemoveWhiteSpace)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#RemoveWhiteSpace)]
[[play](https://go.dev/play/p/HzLC9vsTwkf)] [[play](https://go.dev/play/p/HzLC9vsTwkf)]
- **<big>SubInBetween</big>** : 获取字符串中指定的起始字符串start和终止字符串end直接的子字符串。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#SubInBetween)]
<h3 id="system"> 22. system 包含 os, runtime, shell command 的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="system"> 22. system 包含 os, runtime, shell command 的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
+1 -1
View File
@@ -883,7 +883,7 @@ func main() {
### <span id="ToStdBase64">ToStdBase64</span> ### <span id="ToStdBase64">ToStdBase64</span>
<p>值转换为 StdBase64 编码的字符串。error 类型的数据也会把 error 的原因进行编码,复杂的结构会转为 JSON 格式的字符串</p> <p>值转换为StdBase64编码的字符串。error类型的数据也会把error的原因进行编码,复杂的结构会转为JSON格式的字符串</p>
<b>函数签名:</b> <b>函数签名:</b>
+32
View File
@@ -93,6 +93,7 @@ import (
- [KeyBy](#KeyBy) - [KeyBy](#KeyBy)
- [Join](#Join) - [Join](#Join)
- [Partition](#Partition) - [Partition](#Partition)
- [SetToDefaultIf](#SetToDefaultIf)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -2568,4 +2569,35 @@ func main() {
// Output: // Output:
// okk // okk
} }
```
### <span id="SetToDefaultIf">SetToDefaultIf</span>
<p>根据给定给定的predicate判定函数来修改切片中的元素。对于满足的元素,将其替换为指定的默认值,同时保持元素在切片中的位置不变。函数返回修改后的切片以及被修改的元素个数。</p>
<b>函数签名:</b>
```go
func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int)
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
strs := []string{"a", "b", "a", "c", "d", "a"}
modifiedStrs, count := slice.SetToDefaultIf(strs, func(s string) bool { return "a" == s })
fmt.Println(modifiedStrs)
fmt.Println(count)
// Output:
// [ b c d ]
// 3
}
``` ```
+1 -1
View File
@@ -955,7 +955,7 @@ func main() {
### <span id="ToUrlBase64">ToUrlBase64</span> ### <span id="ToUrlBase64">ToUrlBase64</span>
<p>Convert a value to a string encoded in Url Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON-formatted string.</p> <p>Convert a value to a string encoded in url Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON-formatted string.</p>
<b>Signature:</b> <b>Signature:</b>
+31
View File
@@ -2565,4 +2565,35 @@ func main() {
// Output: // Output:
// okk // okk
} }
```
### <span id="SetToDefaultIf">SetToDefaultIf</span>
<p>Sets elements to their default value if they match the given predicate. It retains the positions of the elements in the slice. It returns slice of T and the count of modified slice items</p>
<b>Signature:</b>
```go
func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int)
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
strs := []string{"a", "b", "a", "c", "d", "a"}
modifiedStrs, count := slice.SetToDefaultIf(strs, func(s string) bool { return "a" == s })
fmt.Println(modifiedStrs)
fmt.Println(count)
// Output:
// [ b c d ]
// 3
}
``` ```
+1 -1
View File
@@ -47,7 +47,7 @@ func TestBefore(t *testing.T) {
var res []int64 var res []int64
type cb func(args ...any) []reflect.Value type cb func(args ...any) []reflect.Value
appendStr := func(i int, s string, fn cb) { appendStr := func(i int, _ string, fn cb) {
v := fn(i) v := fn(i)
res = append(res, v[0].Int()) res = append(res, v[0].Int())
} }
+2 -2
View File
@@ -17,7 +17,7 @@ import (
) )
const ( const (
MaximumCapacity = 1 << 31 MaximumCapacity = math.MaxInt>>1 + 1
Numeral = "0123456789" Numeral = "0123456789"
LowwerLetters = "abcdefghijklmnopqrstuvwxyz" LowwerLetters = "abcdefghijklmnopqrstuvwxyz"
UpperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" UpperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@@ -157,7 +157,7 @@ func random(s string, length int) string {
cache, remain = rn.Int63(), letterIdMax cache, remain = rn.Int63(), letterIdMax
} }
// 从可用字符的字符串中随机选择一个字符 // 从可用字符的字符串中随机选择一个字符
if idx := int(cache & letterIdMask); idx < len(s) { if idx := int(cache & letterIdMask); idx < strLength {
bytes[i] = s[idx] bytes[i] = s[idx]
i-- i--
} }