View Source ShortUUID.Core (ShortUUID v4.1.0)
Core module for ShortUUID encoding and decoding.
This module provides the core functionality for encoding and decoding UUIDs using various alphabets. It includes functions for parsing UUIDs, encoding them into shorter strings, and decoding those strings back into UUIDs.
Functions
parse_uuid/1
- Parses a UUID string into a normalized format.encode_binary/5
- Encodes a UUID into a shorter string using the specified alphabet.decode_string/4
- Decodes a shortened string back into a UUID.format_uuid/1
- Formats an integer value as a UUID string.encode_int/5
- Encodes an integer value into a string using the specified alphabet.decode_to_int/3
- Decodes a string into an integer value using the specified alphabet.int_to_string/4
- Converts an integer value into a string using the specified alphabet.
Summary
Functions
Decodes a shortened string back into a UUID using the specified base and alphabet. Validates the input length and ensures the decoded value is within valid UUID range.
Encodes a UUID string into a shorter string using the specified alphabet and base. Takes a UUID string, base number, alphabet tuple, desired length, and padding character.
Encodes a 128-bit integer into a string using the specified base and alphabet. Pads the result to the desired length using the padding character.
Formats a 128-bit integer as a standard UUID string with dashes. Converts the integer to a 32-character hex string and inserts dashes in the correct positions.
Parses and normalizes various UUID string formats.
Types
Functions
@spec decode_string(String.t(), pos_integer(), map(), pos_integer()) :: {:ok, uuid_string()} | {:error, String.t()}
Decodes a shortened string back into a UUID using the specified base and alphabet. Validates the input length and ensures the decoded value is within valid UUID range.
Examples
iex> alphabet = String.graphemes("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") |> Enum.with_index() |> Map.new()
iex> ShortUUID.Core.decode_string("BWBeN28Vb7cMEx7Ym8AUzs", 58, alphabet, 22)
{:ok, "550e8400-e29b-41d4-a716-446655440000"}
iex> alphabet = String.graphemes("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") |> Enum.with_index() |> Map.new()
iex> ShortUUID.Core.decode_string("invalid", 58, alphabet, 22)
{:error, "Invalid input"}
@spec encode_binary(uuid_string(), pos_integer(), tuple(), pos_integer(), String.t()) :: {:ok, short_uuid()} | {:error, String.t()}
Encodes a UUID string into a shorter string using the specified alphabet and base. Takes a UUID string, base number, alphabet tuple, desired length, and padding character.
Examples
iex> alphabet = String.graphemes("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") |> List.to_tuple()
iex> ShortUUID.Core.encode_binary("550e8400-e29b-41d4-a716-446655440000", 58, alphabet, 22, "1")
{:ok, "BWBeN28Vb7cMEx7Ym8AUzs"}
iex> alphabet = String.graphemes("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") |> List.to_tuple()
iex> ShortUUID.Core.encode_binary("invalid", 58, alphabet, 22, "1")
{:error, "Invalid UUID"}
@spec encode_int(binary(), pos_integer(), tuple(), pos_integer(), String.t()) :: {:ok, short_uuid()} | {:error, String.t()}
Encodes a 128-bit integer into a string using the specified base and alphabet. Pads the result to the desired length using the padding character.
@spec format_uuid(non_neg_integer()) :: uuid_string()
Formats a 128-bit integer as a standard UUID string with dashes. Converts the integer to a 32-character hex string and inserts dashes in the correct positions.
Examples
iex> ShortUUID.Core.format_uuid(0x550e8400e29b41d4a716446655440000)
"550e8400-e29b-41d4-a716-446655440000"
@spec parse_uuid(String.t()) :: {:ok, normalized_uuid()} | {:error, String.t()}
Parses and normalizes various UUID string formats.
Examples
iex> ShortUUID.Core.parse_uuid("550e8400-e29b-41d4-a716-446655440000")
{:ok, "550e8400e29b41d4a716446655440000"}
iex> ShortUUID.Core.parse_uuid("{550e8400-e29b-41d4-a716-446655440000}")
{:ok, "550e8400e29b41d4a716446655440000"}
iex> ShortUUID.Core.parse_uuid("not-a-uuid")
{:error, "Invalid UUID"}