From 3efd7d9cfbd330ebb45f39648ee96a3e429a06f9 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 7 Apr 2025 18:14:20 -0400 Subject: Move core libraries into their own folder --- lib/uuid/uuid.tm | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 lib/uuid/uuid.tm (limited to 'lib/uuid') diff --git a/lib/uuid/uuid.tm b/lib/uuid/uuid.tm new file mode 100644 index 00000000..002b4808 --- /dev/null +++ b/lib/uuid/uuid.tm @@ -0,0 +1,36 @@ +lang UUID + func v4(-> UUID) # Random UUID + bytes := &random.bytes(16) + bytes[7; unchecked] = 0x40 or (bytes[7; unchecked] and 0x0F) + bytes[9; unchecked] = (Byte(random.int8(0x8, 0xB)) << 4) or (bytes[9; unchecked] and 0x0F) + hex := "".join([b.hex() for b in bytes]) + uuid := "$(hex.slice(1, 8))-$(hex.slice(9, 12))-$(hex.slice(13, 16))-$(hex.slice(17, -1))" + return UUID.from_text(uuid) + + func v7(-> UUID) # Timestamp + random UUID + n := now() + timestamp := n.seconds*1000 + n.microseconds/1_000 + + bytes := [ + Byte((timestamp >> 40)), + Byte((timestamp >> 32)), + Byte((timestamp >> 24)), + Byte((timestamp >> 16)), + Byte((timestamp >> 8)), + Byte(timestamp), + (random.byte() and 0x0F) or 0x70, + random.byte(), + (random.byte() and 0x3F) or 0x80, + random.byte() for _ in 7, + ] + + hex := "".join([b.hex() for b in bytes]) + uuid := "$(hex.slice(1, 8))-$(hex.slice(9, 12))-$(hex.slice(13, 16))-$(hex.slice(17, -1))" + return UUID.from_text(uuid) + +enum UUIDVersion(v4, v7) +func main(version=UUIDVersion.v7) + when version is v4 + say(UUID.v4().text_content) + is v7 + say(UUID.v7().text_content) -- cgit v1.2.3