Skip to content

Commit dee6158

Browse files
committed
Cosmetic
1 parent 22dfe8e commit dee6158

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

guid.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ func (guid *Guid) encodeBase64URL(dst []byte) {
226226
// It always fills b entirely, and returns len(b) and nil error.
227227
// guid.Read() is up to 7x faster than crypto/rand.Read() for small slices.
228228
// if b is > 512 bytes, it simply calls crypto/rand.Read().
229-
func (r reader) Read(b []byte) (n int, err error) {
229+
func (r reader) Read(b []byte) (int, error) {
230230
const MaxBytesToFillViaGuids = 512
231-
n = len(b)
231+
n := len(b)
232232

233233
if n == 0 {
234-
return
234+
return 0, nil
235235
}
236236

237237
if n > MaxBytesToFillViaGuids {
@@ -240,27 +240,21 @@ func (r reader) Read(b []byte) (n int, err error) {
240240

241241
guidCacheRef := guidCachePool.Get().(*guidCache)
242242

243-
// Calculate the starting position for the copy.
244-
startPos := int(guidCacheRef.index) * GuidByteSize
245-
246-
if n > (guidCacheByteSize - startPos) {
247-
// Not enough bytes remaining: refill completely. Go 1.24+ guarantees crypto/rand.Read succeeds.
248-
cryptoRand.Read(guidCacheRef.buffer)
243+
if n > (guidCacheByteSize - int(guidCacheRef.index)*GuidByteSize) {
244+
cryptoRand.Read(guidCacheRef.buffer) // Not enough bytes remaining: refill completely. Go 1.24+ guarantees crypto/rand.Read succeeds.
249245
guidCacheRef.index = 0
250-
startPos = 0
251246
} else if guidCacheRef.index == 0 {
252-
// Refill buffer if index wraps (Go 1.24+: cryptoRand.Read is guaranteed to succeed)
253-
cryptoRand.Read(guidCacheRef.buffer)
247+
cryptoRand.Read(guidCacheRef.buffer) // Refill buffer if index wraps (Go 1.24+: cryptoRand.Read is guaranteed to succeed)
254248
}
255249

256-
copy(b, guidCacheRef.buffer[startPos:])
250+
copy(b, guidCacheRef.buffer[int(guidCacheRef.index)*GuidByteSize:])
257251

258252
// Update the index based on the number of Guids consumed.
259253
// The ceiling division ensures the index increments correctly for partial Guid consumption.
260254
guidCacheRef.index += byte((n + GuidByteSize - 1) / GuidByteSize)
261255

262256
guidCachePool.Put(guidCacheRef)
263-
return
257+
return n, nil
264258
} //func (r reader) Read
265259

266260
//==============================================

0 commit comments

Comments
 (0)