Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复bug #164

Merged
merged 3 commits into from
Mar 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions strutil/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

// some consts string chars
const (
MaximumCapacity = 1 << 31
MaximumCapacity = math.MaxInt>>1 + 1
Numbers = "0123456789"
HexChars = "0123456789abcdef" // base16

Expand Down Expand Up @@ -63,11 +63,11 @@
}
// letters的字符需要使用多少个比特位数才能表示完
// letterIdBits := int(math.Ceil(math.Log2(strLength))),下面比上面的代码快
letterIdBits := int(math.Log2(float64(nearestPowerOfTwo(strLength))))

Check warning on line 66 in strutil/random.go

View workflow job for this annotation

GitHub Actions / Test on go 1.20 and ubuntu-latest

var letterIdBits should be letterIDBits
// 最大的字母id掩码
var letterIdMask int64 = 1<<letterIdBits - 1

Check warning on line 68 in strutil/random.go

View workflow job for this annotation

GitHub Actions / Test on go 1.20 and ubuntu-latest

var letterIdMask should be letterIDMask
// 可用次数的最大值
letterIdMax := 63 / letterIdBits

Check warning on line 70 in strutil/random.go

View workflow job for this annotation

GitHub Actions / Test on go 1.20 and ubuntu-latest

var letterIdMax should be letterIDMax
// UnixNano: 1607400451937462000
// 循环生成随机字符串
for i, cache, remain := length-1, rn.Int63(), letterIdMax; i >= 0; {
Expand All @@ -76,7 +76,7 @@
cache, remain = rn.Int63(), letterIdMax
}
// 从可用字符的字符串中随机选择一个字符
if idx := int(cache & letterIdMask); idx < len(letters) {
if idx := int(cache & letterIdMask); idx < strLength {
bytes[i] = letters[idx]
i--
}
Expand Down
Loading