Scoop API

scoop:scoop.std:0.1.0

src/uuid/uuid_parse.scoop

Selected production source · src/uuid/uuid_parse.scoop

1package scoop.uuid
3import scoop.text.hexToByteArrayOrNone
5internal fun uuidFromBytes(bytes: List<Byte>): Uuid {
6 if (bytes.size != 16L) throw IllegalArgumentException(Some("UUID requires 16 bytes"))
7 var high = 0uL
8 var low = 0uL
9 var index = 0L
10 while (index < 16L) {
11 val byte = bytes[index].toUByte().toULong()
12 if (index < 8L) high = (high shl 8L) or byte
13 else low = (low shl 8L) or byte
14 index += 1L
15 }
16 return Uuid(high, low)
19internal fun parseUuid(text: String): Uuid? {
20 if (text.byteLength != 32L && text.byteLength != 36L) return None
21 val chars = text.toCharArray()
22 if (chars.size != text.byteLength) return None
23 var compact = text
24 if (chars.size == 36L) {
25 if (chars[8L] != '-' || chars[13L] != '-' || chars[18L] != '-' || chars[23L] != '-') return None
26 compact = String.fromChars(Array<Char>(32L) { index ->
27 val offset = if (index < 8L) 0L else if (index < 12L) 1L else if (index < 16L) 2L
28 else if (index < 20L) 3L else 4L
29 chars[index + offset]
30 })
31 }
32 return when (compact.hexToByteArrayOrNone()) {
33 case None -> None
34 case Some(val bytes) -> Some(uuidFromBytes(bytes))
35 }