Skip to content

Commit 4318b5f

Browse files
committed
Обновление синтаксиса и исправление багов
1 parent 0ab8536 commit 4318b5f

4 files changed

Lines changed: 49 additions & 38 deletions

File tree

core.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,14 @@ func (uuid UUID) Validate() error {
387387
}
388388
switch version {
389389
case 1, 6:
390-
macValid := true
390+
macIsZero := true
391391
for _, b := range uuid[10:16] {
392392
if b != 0 {
393-
macValid = false
393+
macIsZero = false
394394
break
395395
}
396396
}
397-
if macValid {
397+
if macIsZero {
398398
return ErrInvalidUUIDMAC
399399
}
400400
case 2:

marshal.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ func (uuid *UUID) MarshalText() ([]byte, error) {
3333
buf := initCachePool.Get().(*[36]byte)
3434
defer initCachePool.Put(buf)
3535
encodeHex(buf[:], *uuid)
36+
out := make([]byte, 36)
37+
copy(out, buf[:])
3638
return buf[:], nil
3739
}
3840
func (uuid *UUID) UnmarshalBinary(data []byte) error {

uuid.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ var (
9292
initRandPool = sync.Pool{
9393
New: func() any {
9494
buf := make([]byte, bufSize)
95-
_, _ = rand.Read(buf)
95+
if _, err := rand.Read(buf); err != nil {
96+
fallbackSeed(buf)
97+
}
9698
return &poolBuffer{
9799
buf: buf,
98100
pos: atomic.Uint32{},
@@ -173,6 +175,13 @@ func encodeHex(buf []byte, uuid UUID) {
173175
buf[34] = hexTable[uuid[15]>>4]
174176
buf[35] = hexTable[uuid[15]&0x0f]
175177
}
178+
func fallbackSeed(b []byte) {
179+
t := uint64(time.Now().UnixNano())
180+
for i := range b {
181+
t = t*31 + uint64(i)
182+
b[i] = byte(t >> (i % 8))
183+
}
184+
}
176185
func genRandCrypto(b []byte) {
177186
if len(b) == 0 {
178187
return

uuid_bench_test.go

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,6 @@ import (
77
)
88

99
// Бенчмарки компонентов
10-
func Benchmark_Parse(b *testing.B) {
11-
UUID := testUUIDVUString
12-
b.ResetTimer()
13-
for i := 0; i < b.N; i++ {
14-
_, _ = Parse(UUID)
15-
}
16-
}
17-
func Benchmark_String(b *testing.B) {
18-
uuid, _ := Parse(testUUIDVUString)
19-
b.ResetTimer()
20-
for i := 0; i < b.N; i++ {
21-
_ = uuid.String()
22-
}
23-
}
24-
func Benchmark_Time(b *testing.B) {
25-
for i := 0; i < b.N; i++ {
26-
waitTime(time.Nanosecond * 100)
27-
}
28-
}
29-
func Benchmark_Validate(b *testing.B) {
30-
uuid, _ := Parse(testUUIDVUString)
31-
b.ResetTimer()
32-
for i := 0; i < b.N; i++ {
33-
_ = uuid.Validate()
34-
}
35-
}
3610
func Benchmark_NewV1(b *testing.B) {
3711
b.Run("Multi", func(b *testing.B) {
3812
b.ResetTimer()
@@ -49,7 +23,7 @@ func Benchmark_NewV1(b *testing.B) {
4923
}
5024
})
5125
}
52-
func Benchmark_V1_Info(b *testing.B) {
26+
func Benchmark_NewV1_Info(b *testing.B) {
5327
b.Run("With Mock Time", func(b *testing.B) {
5428
// Ленивая инициализация глобального состояния
5529
initSync.Do(func() {
@@ -102,7 +76,7 @@ func Benchmark_NewV2(b *testing.B) {
10276
}
10377
})
10478
}
105-
func Benchmark_V2_Info(b *testing.B) {
79+
func Benchmark_NewV2_Info(b *testing.B) {
10680
b.Run("Without Mock Time", func(b *testing.B) {
10781
b.ResetTimer()
10882
for i := 0; i < b.N; i++ {
@@ -127,7 +101,7 @@ func Benchmark_NewV3(b *testing.B) {
127101
}
128102
})
129103
}
130-
func Benchmark_V3_Info(b *testing.B) {
104+
func Benchmark_NewV3_Info(b *testing.B) {
131105
b.Run("Without Mock Time", func(b *testing.B) {
132106
b.ResetTimer()
133107
for i := 0; i < b.N; i++ {
@@ -152,7 +126,7 @@ func Benchmark_NewV4(b *testing.B) {
152126
}
153127
})
154128
}
155-
func Benchmark_V4_Info(b *testing.B) {
129+
func Benchmark_NewV4_Info(b *testing.B) {
156130
b.Run("Without Mock Time", func(b *testing.B) {
157131
b.ResetTimer()
158132
for i := 0; i < b.N; i++ {
@@ -177,7 +151,7 @@ func Benchmark_NewV5(b *testing.B) {
177151
}
178152
})
179153
}
180-
func Benchmark_V5_Info(b *testing.B) {
154+
func Benchmark_NewV5_Info(b *testing.B) {
181155
b.Run("Without Mock Time", func(b *testing.B) {
182156
b.ResetTimer()
183157
for i := 0; i < b.N; i++ {
@@ -202,7 +176,7 @@ func Benchmark_NewV6(b *testing.B) {
202176
}
203177
})
204178
}
205-
func Benchmark_V6_Info(b *testing.B) {
179+
func Benchmark_NewV6_Info(b *testing.B) {
206180
b.Run("With Mock Time", func(b *testing.B) {
207181
// Ленивая инициализация глобального состояния
208182
initSync.Do(func() {
@@ -255,7 +229,7 @@ func Benchmark_NewV7(b *testing.B) {
255229
}
256230
})
257231
}
258-
func Benchmark_V7_Info(b *testing.B) {
232+
func Benchmark_NewV7_Info(b *testing.B) {
259233
b.Run("With Mock Time", func(b *testing.B) {
260234
// Ленивая инициализация глобального состояния
261235
initSync.Do(func() {
@@ -308,7 +282,7 @@ func Benchmark_NewV8(b *testing.B) {
308282
}
309283
})
310284
}
311-
func Benchmark_V8_Info(b *testing.B) {
285+
func Benchmark_NewV8_Info(b *testing.B) {
312286
b.Run("With Mock Time", func(b *testing.B) {
313287
// Ленивая инициализация глобального состояния
314288
initSync.Do(func() {
@@ -345,3 +319,29 @@ func Benchmark_V8_Info(b *testing.B) {
345319
}
346320
})
347321
}
322+
func Benchmark_Parse(b *testing.B) {
323+
UUID := testUUIDVUString
324+
b.ResetTimer()
325+
for i := 0; i < b.N; i++ {
326+
_, _ = Parse(UUID)
327+
}
328+
}
329+
func Benchmark_String(b *testing.B) {
330+
uuid, _ := Parse(testUUIDVUString)
331+
b.ResetTimer()
332+
for i := 0; i < b.N; i++ {
333+
_ = uuid.String()
334+
}
335+
}
336+
func Benchmark_Time(b *testing.B) {
337+
for i := 0; i < b.N; i++ {
338+
waitTime(time.Nanosecond * 100)
339+
}
340+
}
341+
func Benchmark_Validate(b *testing.B) {
342+
uuid, _ := Parse(testUUIDVUString)
343+
b.ResetTimer()
344+
for i := 0; i < b.N; i++ {
345+
_ = uuid.Validate()
346+
}
347+
}

0 commit comments

Comments
 (0)