uuid

    Portable SIMD UUID parsing and formatting ported from uuid-simd

    uuid
    guid
    simd
    uuid-simd
    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    49 minutes ago
    Downloads
    1

    No dependencies

    #UUID checked examples

    ///|
    test "parse mixed-case UUID and format both spellings" {
    let id = @uuid.parse("67E55044-10b1-426f-9247-BB680e5fe0c8")
    assert_eq(id.format_simple(), "67e5504410b1426f9247bb680e5fe0c8")
    assert_eq(
    id.format_hyphenated(case=@uuid.Upper),
    "67E55044-10B1-426F-9247-BB680E5FE0C8",
    )
    }

    ///|
    test "write into a reusable guarded byte buffer" {
    let id = @uuid.parse_bytes(b"00000000000000000000000000000000")
    let output : FixedArray[Byte] = FixedArray::make(34, b'!')
    id.write_simple(output, offset=1)
    assert_eq(Bytes::from_array(output), b"!00000000000000000000000000000000!")
    }

    UuidError

    pub(all) suberror UuidError {
    InvalidUuid
    InvalidByteLength(Int)
    InvalidBuffer(offset~ : Int, required~ : Int, length~ : Int)
    } derive(Eq,
    Debug
    )

    Reports invalid UUID text, binary lengths, or destination buffer ranges.

    InvalidUuid covers length, wrappers, hyphen placement, and hexadecimal syntax, matching upstream's opaque parsing error. InvalidByteLength reports a binary input that is not exactly sixteen bytes. InvalidBuffer reports an output range that cannot fit; the destination remains unchanged.

    Algorithm

    pub(all) enum Algorithm {
    Auto
    Scalar
    Simd
    } derive(Eq,
    Debug
    )

    Selects UUID parsing and formatting implementations.

    Scalar always uses the portable scalar implementation. Simd requests V128 operations on native and linear-memory Wasm and falls back to scalar on Wasm GC and JavaScript. Auto selects the measured backend default.

    AsciiCase

    pub(all) enum AsciiCase {
    Lower
    Upper
    } derive(Eq,
    Debug
    )

    Selects lowercase or uppercase ASCII hexadecimal output.

    Parsing accepts either case independently of this formatting option.

    Uuid

    pub struct Uuid {
    // private fields
    } derive(Compare, Eq, Hash,
    Debug
    )

    An immutable UUID stored as two unsigned words in network byte order.

    Equality, ordering, and hashing use all 128 bits. Any bit pattern is accepted; parsing does not enforce a particular UUID version or variant. Representation is private. Assignments safely share immutable state, and constructors retain no mutable buffers or input views. Returned text and bytes are independent immutable values on every backend.
    impl Show for Uuid

    Uuid::format_hyphenated

    fn Uuid::format_hyphenated(self : Uuid, case? : AsciiCase, algorithm? : Algorithm) -> String

    Formats this UUID as exactly 36 ASCII characters in 8-4-4-4-12 form.

    Hexadecimal case defaults to lowercase. Hyphens occur at offsets 8, 13, 18, and 23. The UUID is unchanged and the returned string is independent. Formatting cannot fail. SIMD requests use scalar fallback on GC/JS; allocation and ASCII-to-string conversion are part of this operation.

    Uuid::format_simple

    fn Uuid::format_simple(self : Uuid, case? : AsciiCase, algorithm? : Algorithm) -> String

    Formats this UUID as exactly 32 ASCII hexadecimal characters.

    case defaults to lowercase. The result owns immutable storage; the UUID is unchanged. Every UUID bit pattern is representable and formatting cannot fail. Simd falls back to scalar on Wasm GC and JavaScript. Allocation and conversion of the ASCII output to a MoonBit string are included.

    Uuid::from_bytes

    fn Uuid::from_bytes(input : BytesView) -> Uuid raise UuidError

    Constructs a UUID from exactly sixteen bytes in network order.

    The first eight bytes form the high word. Offset views are accepted and no bytes outside the view are read. Raises InvalidByteLength otherwise. The input is not mutated or retained; no version or variant bits are rewritten.

    Uuid::to_bytes

    fn Uuid::to_bytes(self : Uuid) -> Bytes

    Returns the UUID's sixteen bytes in network order.

    The result owns its immutable storage and shares no mutable destination buffer. Windows GUID field endianness is not applied. This method cannot fail and has the same byte order on every backend.

    Uuid::to_string

    fn Uuid::to_string(self : Uuid) -> String

    Makes the canonical display operation available through ordinary method syntax.

    Uuid::write_hyphenated

    fn Uuid::write_hyphenated(self : Uuid, destination : FixedArray[Byte], offset? : Int, case? : AsciiCase, algorithm? : Algorithm) -> Unit raise UuidError

    Writes exactly 36 ASCII UUID bytes in 8-4-4-4-12 form.

    Writes [offset, offset + 36) and preserves all other bytes, including guards immediately outside the range. Raises InvalidBuffer before any mutation for negative offsets or insufficient capacity. No buffer is allocated or retained. The UUID is unchanged. Case defaults to lowercase; SIMD requests use scalar fallback on Wasm GC and JavaScript.

    Uuid::write_simple

    fn Uuid::write_simple(self : Uuid, destination : FixedArray[Byte], offset? : Int, case? : AsciiCase, algorithm? : Algorithm) -> Unit raise UuidError

    Writes exactly 32 ASCII hexadecimal bytes into a reusable destination.

    Writes [offset, offset + 32) and preserves all other bytes. Case defaults to lowercase. Raises InvalidBuffer for negative offsets or insufficient capacity before changing any byte. The UUID is unchanged, no destination is retained, and the operation allocates no output buffer. SIMD requests use scalar fallback on Wasm GC and JavaScript.

    parse

    fn parse(input : StringView, algorithm? : Algorithm) -> Uuid raise UuidError

    Parses the four UUID spellings accepted by uuid-simd 0.8.0.

    Accepts 32 hexadecimal digits, 8-4-4-4-12 hyphenated text, that hyphenated text enclosed in one pair of braces, or prefixed by the exact lowercase urn:uuid:. Hexadecimal digits may use mixed case. Whitespace, misplaced hyphens, non-ASCII code units, and other wrappers raise InvalidUuid. The input view is neither mutated nor retained. No version or variant validation is performed. Simd falls back on Wasm GC and JavaScript.

    parse_bytes

    fn parse_bytes(input : BytesView, algorithm? : Algorithm) -> Uuid raise UuidError

    Parses arbitrary bytes in the same four spellings as parse.

    Requires ASCII hexadecimal text with exact lengths and wrapper spelling. Invalid bytes, including high-bit bytes or embedded NUL, raise InvalidUuid. No UTF-8 conversion is required; offsets count bytes and all loads stay inside the supplied view. The input is not mutated or retained. SIMD requests fall back on Wasm GC and JavaScript.

    parse_hyphenated

    fn parse_hyphenated(input : StringView, algorithm? : Algorithm) -> Uuid raise UuidError

    Parses exactly the 36-code-unit 8-4-4-4-12 ASCII UUID spelling.

    Mixed-case hexadecimal digits are accepted; hyphens are required at offsets 8, 13, 18, and 23. Invalid length, characters, or syntax raise InvalidUuid. This method does not accept wrappers or enforce version/variant bits. It neither mutates nor retains its input. SIMD requests fall back on GC/JS.

    parse_simple

    fn parse_simple(input : StringView, algorithm? : Algorithm) -> Uuid raise UuidError

    Parses exactly 32 ASCII hexadecimal code units, accepting mixed case.

    Hyphens, braces, prefixes, whitespace, and non-ASCII values raise InvalidUuid. Input views are read within their bounds and not retained. All UUID bit patterns are accepted. algorithm controls the implementation; SIMD requests use a scalar fallback on Wasm GC and JavaScript.

    simd_available

    fn simd_available() -> Bool

    Reports whether a SIMD request can use V128 operations on this backend.

    Returns true for native and linear-memory Wasm, false for Wasm GC and JavaScript. This describes the compiled implementation, not optional CPU feature detection or the Auto policy.