Skip to content

Commit

Permalink
Update README docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Nov 22, 2021
1 parent bacdf79 commit 88871a6
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 95 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ Generation of UUIDs based on [RFC 4122](https://datatracker.ietf.org/doc/html/rf
## Usage

```motoko
let g = uuid.Generator();
uuid.toText(g.new());
// import Source uuid/async/SourceV4
// import UUID uuid/UUID
let g = Source.Source();
UUID.toText(await g.new());
// F253F1F0-2885-169F-169F-2885F253F1F0
```
28 changes: 14 additions & 14 deletions example/main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import Source "../src/Source";
import AsyncSource "../src/async/SourceV4";

actor {
private let ae = AsyncSource.Source();
private let ae = AsyncSource.Source();

private let rr = XorShift.toReader(XorShift.XorShift64(null));
private let c : [Nat8] = [0, 0, 0, 0, 0, 0]; // Replace with identifier of canister f.e.
private let se = Source.Source(rr, c);
private let rr = XorShift.toReader(XorShift.XorShift64(null));
private let c : [Nat8] = [0, 0, 0, 0, 0, 0]; // Replace with identifier of canister f.e.
private let se = Source.Source(rr, c);

public func newAsync() : async Text {
let id = await ae.new();
Debug.print(debug_show((id, id.size())));
UUID.toText(id);
};
public func newAsync() : async Text {
let id = await ae.new();
Debug.print(debug_show((id, id.size())));
UUID.toText(id);
};

public func newSync() : async Text {
let id = se.new();
Debug.print(debug_show((id, id.size())));
UUID.toText(id);
};
public func newSync() : async Text {
let id = se.new();
Debug.print(debug_show((id, id.size())));
UUID.toText(id);
};
};
128 changes: 64 additions & 64 deletions src/Source.mo
Original file line number Diff line number Diff line change
Expand Up @@ -11,73 +11,73 @@ import Time "mo:base/Time";
import UUID "UUID";

module {
public class Source(
rand : IO.Reader<Nat8>,
node : [Nat8],
) {
if (node.size() != 6) assert(false);
public class Source(
rand : IO.Reader<Nat8>,
node : [Nat8],
) {
if (node.size() != 6) assert(false);

private let lillian : Nat64 = 2299160; // Julian day of 15 Oct 1582.
private let unix : Nat64 = 2440587; // Julian day of 1 Jan 1970.
private let epoch : Nat64 = unix - lillian; // Days between epochs.
private let g1582 : Nat64 = epoch * 86400; // Seconds between epochs.
private let g1582ns100 : Nat64 = g1582 * 10000000; // 100s of a nanoseconds between epochs.
private var lastTime : Nat64 = 0;
private var clockSequence : Nat16 = 0;
private let lillian : Nat64 = 2299160; // Julian day of 15 Oct 1582.
private let unix : Nat64 = 2440587; // Julian day of 1 Jan 1970.
private let epoch : Nat64 = unix - lillian; // Days between epochs.
private let g1582 : Nat64 = epoch * 86400; // Seconds between epochs.
private let g1582ns100 : Nat64 = g1582 * 10000000; // 100s of a nanoseconds between epochs.
private var lastTime : Nat64 = 0;
private var clockSequence : Nat16 = 0;

// Generates a new UUID.
public func new() : UUID.UUID {
let (now, clock) = time();
let low = Nat32.fromNat(Nat64.toNat(now & 0xFFFFFFFF));
let mid = Nat16.fromNat(Nat64.toNat((now >> 32) & 0xFFFF));
let high = Nat16.fromNat(Nat64.toNat((now >> 48) & 0x0FFF)) | 0x1000;
Array.flatten<Nat8>([
Binary.BigEndian.fromNat32(low),
Binary.BigEndian.fromNat16(mid),
Binary.BigEndian.fromNat16(high),
Binary.BigEndian.fromNat16(clock),
node,
]);
};
// Generates a new UUID.
public func new() : UUID.UUID {
let (now, clock) = time();
let low = Nat32.fromNat(Nat64.toNat(now & 0xFFFFFFFF));
let mid = Nat16.fromNat(Nat64.toNat((now >> 32) & 0xFFFF));
let high = Nat16.fromNat(Nat64.toNat((now >> 48) & 0x0FFF)) | 0x1000;
Array.flatten<Nat8>([
Binary.BigEndian.fromNat32(low),
Binary.BigEndian.fromNat16(mid),
Binary.BigEndian.fromNat16(high),
Binary.BigEndian.fromNat16(clock),
node,
]);
};

private func time() : (Nat64, Nat16) {
let t = Nat64.fromNat(Int.abs(Time.now()));
if (clockSequence == 0) {
setClockSequence(null);
};
let now = t/100 + g1582ns100;
if (now <= lastTime) {
clockSequence := ((clockSequence + 1) & 0x3fff) | 0x8000;
};
lastTime := now;
(t, clockSequence);
};
private func time() : (Nat64, Nat16) {
let t = Nat64.fromNat(Int.abs(Time.now()));
if (clockSequence == 0) {
setClockSequence(null);
};
let now = t/100 + g1582ns100;
if (now <= lastTime) {
clockSequence := ((clockSequence + 1) & 0x3fff) | 0x8000;
};
lastTime := now;
(t, clockSequence);
};

// Sets the clock sequence to the lower 14 bits of seq, null generates a new clock sequence.
public func setClockSequence(seq : ?Nat16) {
var s : Nat16 = switch (seq) {
case (null) {
let bs = switch (rand.read(2)) {
case (#ok(bs)) bs;
case (#eof(bs)) bs;
case (#err(_)) {
assert(false); [];
};
};
nat8to16(bs[0]) << 8 | nat8to16(bs[1]);
};
case (? s) { s; };
};
let oldSequence = clockSequence;
clockSequence := s & 0x3FFF | 0x8000;
if (oldSequence != clockSequence) {
lastTime := 0;
};
};
// Sets the clock sequence to the lower 14 bits of seq, null generates a new clock sequence.
public func setClockSequence(seq : ?Nat16) {
var s : Nat16 = switch (seq) {
case (null) {
let bs = switch (rand.read(2)) {
case (#ok(bs)) bs;
case (#eof(bs)) bs;
case (#err(_)) {
assert(false); [];
};
};
nat8to16(bs[0]) << 8 | nat8to16(bs[1]);
};
case (? s) { s; };
};
let oldSequence = clockSequence;
clockSequence := s & 0x3FFF | 0x8000;
if (oldSequence != clockSequence) {
lastTime := 0;
};
};

private func nat8to16(n : Nat8) : Nat16 {
Nat16.fromNat(Nat8.toNat(n));
};
};
private func nat8to16(n : Nat8) : Nat16 {
Nat16.fromNat(Nat8.toNat(n));
};
};
};
30 changes: 15 additions & 15 deletions src/UUID.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import Hex "mo:encoding/Hex";
import List "mo:base/List";

module {
// A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC 4122.
public type UUID = [Nat8];
// A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC 4122.
public type UUID = [Nat8];

// Converts the UUID to its textual representation.
public func toText(uuid : UUID) : Text {
var t = "";
let xs = List.fromArray(uuid);
let (p0, xs0) = List.split(4, xs);
t #= Hex.encode(List.toArray(p0)) # "-";
let (p1, xs1) = List.split(2, xs0);
t #= Hex.encode(List.toArray(p1)) # "-";
let (p2, xs2) = List.split(2, xs1);
t #= Hex.encode(List.toArray(p2)) # "-";
let (p3, xs3) = List.split(2, xs2);
t # Hex.encode(List.toArray(p3)) # "-" # Hex.encode(List.toArray(xs3));
};
// Converts the UUID to its textual representation.
public func toText(uuid : UUID) : Text {
var t = "";
let xs = List.fromArray(uuid);
let (p0, xs0) = List.split(4, xs);
t #= Hex.encode(List.toArray(p0)) # "-";
let (p1, xs1) = List.split(2, xs0);
t #= Hex.encode(List.toArray(p1)) # "-";
let (p2, xs2) = List.split(2, xs1);
t #= Hex.encode(List.toArray(p2)) # "-";
let (p3, xs3) = List.split(2, xs2);
t # Hex.encode(List.toArray(p3)) # "-" # Hex.encode(List.toArray(xs3));
};
};

0 comments on commit 88871a6

Please sign in to comment.