Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit d580294

Browse files
committed
stdlib: add BaseUid generator
1 parent 6e54a6a commit d580294

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

core/stdlib/baseuid.core

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// BaseUid-functionality is not intended to stay in stdlib root.
2+
// It's only here because we lack the infrastructure for it to be somewhere else.
3+
4+
use std.Array
5+
use std.Random
6+
7+
let RANDOM: Random = Random::new(0)
8+
9+
@pub
10+
fun createBaseUid(): String = createBaseUidFrom(std::timestamp())
11+
12+
@pub
13+
fun createBaseUidFrom(milliseconds: Int64): String {
14+
let time0 = toNanos(milliseconds)
15+
//PREVIOUS_TIME.compareAndExchange(time0, time0 + 0b1000000000000000)
16+
//time0 = Math.max(time0, PREVIOUS_TIME.get())
17+
//PREVIOUS_TIME.set(time0)
18+
19+
let rand1 = RANDOM.nextInt32()
20+
let rand2 = RANDOM.nextInt32()
21+
let rand3 = RANDOM.nextInt32()
22+
23+
let buffer = Array[UInt8]::zero(15)
24+
buffer( 0) = time0.shiftRight(55i32).toUInt8()
25+
buffer( 1) = time0.shiftRight(47i32).toUInt8()
26+
buffer( 2) = time0.shiftRight(39i32).toUInt8()
27+
buffer( 3) = time0.shiftRight(31i32).toUInt8()
28+
buffer( 4) = time0.shiftRight(23i32).toUInt8()
29+
buffer( 5) = time0.shiftRight(15i32).toUInt8()
30+
buffer( 6) = rand1 .toUInt8()
31+
buffer( 7) = rand2.shiftRight(24i32).toUInt8()
32+
buffer( 8) = rand2.shiftRight(16i32).toUInt8()
33+
buffer( 9) = rand2.shiftRight( 8i32).toUInt8()
34+
buffer(10) = rand2 .toUInt8()
35+
buffer(11) = rand3.shiftRight(24i32).toUInt8()
36+
buffer(12) = rand3.shiftRight(16i32).toUInt8()
37+
buffer(13) = rand3.shiftRight( 8i32).toUInt8()
38+
buffer(14) = rand3 .toUInt8()
39+
40+
std::base64::encodeOrdered(buffer)
41+
}
42+
43+
@pub // pretend we have more precision than we actually have
44+
fun toNanos(milliseconds: Int64): Int64 = milliseconds * 1'000'000

core/stdlib/stdlib.core

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
@pub mod annotations;
99
@pub mod base64;
10+
@pub mod baseuid;
1011
@pub mod collections;
1112
@pub mod primitives
1213
@pub mod rand

tests/baseuid/baseuid.core

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
fun main(): Unit {
2+
testNoClash()
3+
testHasLetterIn2023()
4+
testHasLetterIn2024()
5+
testHasLetterIn2038()
6+
// printRange48()
7+
}
8+
9+
fun testNoClash(): Unit {
10+
let buid1 = std::baseuid::createBaseUid()
11+
let buid2 = std::baseuid::createBaseUid()
12+
assert(buid1 != buid2)
13+
}
14+
15+
fun testHasLetterIn2023(): Unit {
16+
let base2023 = std::baseuid::createBaseUidFrom(1'672'531'200'000)
17+
assert(base2023.startsWith("Aak"))
18+
}
19+
20+
fun testHasLetterIn2024(): Unit {
21+
let base2024 = std::baseuid::createBaseUidFrom(1'704'067'200'000)
22+
assert(base2024.startsWith("Aok"))
23+
}
24+
25+
fun testHasLetterIn2038(): Unit {
26+
let base2038 = std::baseuid::createBaseUidFrom(2'145'916'800'000)
27+
assert(base2038.startsWith("Dsy"))
28+
}
29+
30+
fun printRange48(): Unit {
31+
let base2020 = std::baseuid::createBaseUidFrom(1'577'836'800'000)
32+
let base2023 = std::baseuid::createBaseUidFrom(1'672'531'200'000)
33+
let base2024 = std::baseuid::createBaseUidFrom(1'704'067'200'000)
34+
let base2038 = std::baseuid::createBaseUidFrom(2'145'916'800'000)
35+
let base2040 = std::baseuid::createBaseUidFrom(2'208'988'800'000)
36+
let base2050 = std::baseuid::createBaseUidFrom(2'524'608'000'000)
37+
let base2060 = std::baseuid::createBaseUidFrom(2'840'140'800'000)
38+
let base2070 = std::baseuid::createBaseUidFrom(3'155'760'000'000)
39+
let base2080 = std::baseuid::createBaseUidFrom(3'471'292'800'000)
40+
let base2090 = std::baseuid::createBaseUidFrom(3'786'912'000'000)
41+
let base2100 = std::baseuid::createBaseUidFrom(4'102'444'800'000)
42+
let base2150 = std::baseuid::createBaseUidFrom(5'680'281'600'000)
43+
let base2200 = std::baseuid::createBaseUidFrom(7'258'118'400'000)
44+
println("2020 " + base2020)
45+
println("2023 " + base2023)
46+
println("2024 " + base2024)
47+
println("2038 " + base2038)
48+
println("2040 " + base2040)
49+
println("2050 " + base2050)
50+
println("2060 " + base2060)
51+
println("2070 " + base2070)
52+
println("2080 " + base2080)
53+
println("2090 " + base2090)
54+
println("2100 " + base2100)
55+
println("2150 " + base2150)
56+
println("2200 " + base2200)
57+
}

tests/fatal1.core

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//= error code 1
2-
//= stderr "fatal error: bla\n std::fatalError (stdlib/stdlib.core:17)\n main (tests/fatal1.core:5)\n"
2+
//= stderr "fatal error: bla\n std::fatalError (stdlib/stdlib.core:18)\n main (tests/fatal1.core:5)\n"
33

44
fun main(): Unit {
55
std::fatalError("bla");

0 commit comments

Comments
 (0)