1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*******************************************************************************
*
* Copyright (c) 2026 Haixing Hu.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0.
*
******************************************************************************/
//! Mica-style random UUID-like ID generation.
//!
//! The formatting approach follows Mica's fast UUID helper and unsigned
//! hexadecimal formatter from [`StringUtil`], plus the related
//! [Mica UUID benchmark notes].
//!
//! [`StringUtil`]: https://github.com/lets-mica/mica/blob/master/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java#L335
//! [Mica UUID benchmark notes]: https://github.com/lets-mica/mica-jmh/wiki/uuid
use crate::;
/// Lowercase hexadecimal digits used by the Mica UUID-like formatter.
const HEX: & = b"0123456789abcdef";
/// Mask for extracting one hexadecimal digit from the low four bits.
///
/// A hexadecimal digit is a 4-bit nibble. After shifting the source value by a
/// multiple of four bits, this mask keeps only the current digit. This mirrors
/// the Java helper's `MASK = HEX_RADIX - 1` constant in Mica's
/// `StringUtil::formatUnsignedLong`.
const HEX_DIGIT_MASK: u128 = 0x0f;
/// Mica-style UUID-like random ID generator.
///
/// This generator is only a random number generator that mimics the canonical
/// UUID text shape. It produces 128 random bits and formats them as lowercase
/// UUID-like text, but it does not rewrite RFC UUID version or variant bits.
/// Therefore it should not be treated as a standards-compliant UUID v4
/// generator.
///
/// # Origin
/// The formatting approach is based on Mica's fast UUID utility and
/// `formatUnsignedLong` helper:
/// <https://github.com/lets-mica/mica/blob/master/mica-core/src/main/java/net/dreamlu/mica/core/utils/StringUtil.java#L348>.
/// The Java source also points to Mica's UUID benchmark notes:
/// <https://github.com/lets-mica/mica-jmh/wiki/uuid>.
;
/// Generates a canonical lowercase UUID-like random string.
///
/// # Returns
/// UUID-like text in `8-4-4-4-12` lowercase hexadecimal form.
///
/// # Errors
/// Returns [`IdError::RandomSourceUnavailable`] when the operating system
/// random source cannot fill 16 bytes.
/// Generates a compact lowercase UUID-like random string.
///
/// # Returns
/// UUID-like text as 32 lowercase hexadecimal digits without separators.
///
/// # Errors
/// Returns [`IdError::RandomSourceUnavailable`] when the operating system
/// random source cannot fill 16 bytes.
/// Appends fixed-width lowercase hexadecimal digits to a string.
///
/// # Parameters
/// - `output`: Destination string.
/// - `value`: Source value; only the lowest `digits * 4` bits are used.
/// - `digits`: Number of hexadecimal digits to append.