Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
bddjr committed Dec 2, 2024
1 parent 40bbeff commit f6ea07c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ go get github.com/bddjr/cryptorandstr

```
MustRand10 6
961812
186418
476781
683053
189178
227679
901676
193607
026687
243505
MustRand16 27
d4ff1e125208f0e2954001b3c6e
36c517865848a7444454a7711ca
f5348bba35053bfeb962d3822b9
df46ffa337732945d6c98ed0276
280a987f779f9775d8decffb150
017ced60ee75751a027605270fe
2d89aa349e78988585524c5d074
d155483394a0c8f75becd533faf
7714135085cb7967f6ee645edb5
0d0e22c51fe5cad882d6a0867d7
MustRand32 27
fjq0iakoubcl2sp92ekaq9ftsan
mgu2bms5d90ai35mtjdrru3rkfe
pt6k8rmsneovn3bq3q1ru2m26bk
91rn0793voe4tupdcm2b31rfdd1
ank42vo7ju49t0irpmmn65osj5p
5e9hoqmhspbsnng2ct2hgosko4e
e9f5e7i5i24d9tlf1tef8fgcsba
0dhkjpef6nqj1bnm81rhnjgjqjj
kj968dr8sc9srpk9roj3go45k8r
tvkb7pl542c3v5e3opqg5jcejhm
MustRand64 27
bMPa2jTkoJF4NzWgv4wr6Qmc-OK
_oWdmJSErNbW1rval-gM7ult-6_
KhMddFuOkKCf9eHXx3s11H4AZmE
oCvBxMPnZwLV_Bi540RnH8_Nmk6
st-G5VLmETyHV8WLhAOEaIWfuEn
xA-ZoFjHgQ927Jxnhly7WWRMvjw
NIBvnag5JrYhvHZ-NaKEZkmBgco
YD3qxeMe7AGlXllP0M07GkTXnMA
fC7_-XWOEf-BPv-U5_bRXrkEDqi
b1t6xZLgmW2v9KdxGEKqVIp-WAH
PASS
ok github.com/bddjr/cryptorandstr 0.253s
ok github.com/bddjr/cryptorandstr 0.034s
```

## License
Expand Down
14 changes: 5 additions & 9 deletions rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ func Rand(strLen int, chars string) (string, error) {
r := randWithGC{
outBitLen: bitLen(maxB),
}
difference := 8 - r.outBitLen

out := make([]byte, strLen)
i := 0
for i < strLen {
b, err := r.byte(chunk)
b, err := r.byte(chunk, difference)
if err != nil {
return "", err
}
Expand All @@ -103,10 +104,7 @@ type randWithGC struct {
cache uint16
}

// len(chunk) == 1
func (r *randWithGC) byte(chunk []byte) (byte, error) {
difference := 8 - r.outBitLen

func (r *randWithGC) byte(chunk []byte, difference byte) (byte, error) {
// read cache
if r.cacheBitLen >= r.outBitLen {
b := (byte(r.cache) << difference) >> difference
Expand All @@ -126,12 +124,10 @@ func (r *randWithGC) byte(chunk []byte) (byte, error) {
}
}

b := chunk[0]

// write cache
r.cache <<= difference
r.cache |= uint16(b >> r.outBitLen)
r.cache |= uint16(chunk[0] >> r.outBitLen)
r.cacheBitLen += difference

return (b << difference) >> difference, nil
return (chunk[0] << difference) >> difference, nil
}

0 comments on commit f6ea07c

Please sign in to comment.