Scoop API

scoop:scoop.std:0.1.0

src/uuid/uuid.scoop

Selected production source · src/uuid/uuid.scoop

1package scoop.uuid
3import scoop.decodeTextValue
5import scoop.random.Random
6import scoop.text.toHexString
8/**
9 * A 128-bit identifier represented by unsigned high and low halves. Ordering compares those halves
10 * unsigned, and byte conversion uses network byte order.
11 */
12public struct Uuid(val mostSignificantBits: ULong, val leastSignificantBits: ULong)
13 : Equals<Uuid>, Compare<Uuid>, Hash, ToString {
14 /**
15 * Compares the stored value components for equality.
16 */
17 public override operator fun equals(other: Uuid): Boolean =
18 mostSignificantBits == other.mostSignificantBits && leastSignificantBits == other.leastSignificantBits
20 /**
21 * Orders the unsigned high 64 bits first, then the unsigned low 64 bits.
22 */
23 public override operator fun compareTo(other: Uuid): Long =
24 if (mostSignificantBits != other.mostSignificantBits) mostSignificantBits.compareTo(other.mostSignificantBits)
25 else leastSignificantBits.compareTo(other.leastSignificantBits)
27 /**
28 * Computes a hash consistent with component equality.
29 */
30 public override fun hash(): Long = mostSignificantBits.hash() * 31L + leastSignificantBits.hash()
31 /**
32 * Returns the raw high and low bit patterns reinterpreted as signed Long values.
33 */
34 public fun toLongs(): (Long, Long) = (mostSignificantBits.toLong(), leastSignificantBits.toLong())
35 /**
36 * Returns 16 bytes in network byte order.
37 */
38 public fun toByteArray(): Array<Byte> = Array<Byte>(16L) { index ->
39 val part = if (index < 8L) mostSignificantBits else leastSignificantBits;
40 (part shr ((7L - index % 8L) * 8L)).toByte()
41 }
42 /**
43 * Returns 32 lowercase hexadecimal digits without separators.
44 */
45 public fun toHexString(): String = toByteArray().toHexString()
46 /**
47 * Returns lowercase hexadecimal text in the 8-4-4-4-12 grouping.
48 */
49 public fun toHexDashString(): String {
50 val hex = toHexString()
51 return String.fromChars(Array<Char>(36L) { index ->
52 if (index == 8L || index == 13L || index == 18L || index == 23L) '-'
53 else {
54 val offset = if (index < 8L) 0L else if (index < 13L) 1L else if (index < 18L) 2L
55 else if (index < 23L) 3L else 4L
56 hex.getCharAt(index - offset)
57 }
58 })
59 }
60 /**
61 * Returns the lowercase hyphenated UUID representation.
62 */
63 public override fun toString(): String = toHexDashString()
65 /**
66 * Value construction and serialization helpers.
67 */
68 public companion object : ValueEncoder<Uuid>, ValueDecoder<Uuid> {
69 /**
70 * Encodes the value as its canonical text representation.
71 */
72 public override fun encode(value: Uuid, encoder: Encoder) { encoder.singleValue().writeString(value.toString()) }
73 /**
74 * Reads a string value and validates it using this type's parser.
75 */
76 public override fun decode(decoder: Decoder): Uuid =
77 decodeTextValue<Uuid>(decoder, { text -> parseOrNone(text) }, "UUID")
79 /**
80 * The UUID whose 128 bits are all zero.
81 */
82 public val NIL: Uuid = Uuid(0uL, 0uL)
83 /**
84 * The UUID whose 128 bits are all one.
85 */
86 public val MAX: Uuid = Uuid(ULong.MAX_VALUE, ULong.MAX_VALUE)
87 /**
88 * Reinterprets two signed Long bit patterns as the high and low UUID halves.
89 */
90 public fun fromLongs(mostSignificantBits: Long, leastSignificantBits: Long): Uuid =
91 Uuid(mostSignificantBits.toULong(), leastSignificantBits.toULong())
92 /**
93 * Constructs a UUID from exactly 16 network-order bytes; other sizes throw.
94 */
95 public fun fromByteArray(bytes: List<Byte>): Uuid = uuidFromBytes(bytes)
96 /**
97 * Parses UUID hexadecimal text, with or without the standard hyphens, returning None for
98 * malformed or out-of-range input.
99 */
100 public fun parseOrNone(text: String): Uuid? = parseUuid(text)
101 /**
102 * Parses UUID hexadecimal text, with or without the standard hyphens or throws
103 * IllegalArgumentException for invalid input.
104 */
105 public fun parse(text: String): Uuid = when (parseOrNone(text)) {
106 case Some(val value) -> value
107 case None -> throw IllegalArgumentException(Some("invalid UUID"))
108 }
109 /**
110 * Creates a version-4 UUID from the supplied random source and sets the version/variant
111 * bits. Security depends on that source.
112 */
113 public fun random(random: Random): Uuid {
114 val value = fromByteArray(random.nextBytes(16L))
115 return Uuid((value.mostSignificantBits and 0xffffffffffff0fffuL) or 0x4000uL,
116 (value.leastSignificantBits and 0x3fffffffffffffffuL) or 0x8000000000000000uL)
117 }
118 }