Module base

Module base 

Source
Expand description

Mathematical base representation, positional numeral systems, and number string conversion utilities.

This module addresses two distinct, interrelated domains:

  1. Mathematical Positional Radix Systems (Number-Oriented):

    • Represents numeric quantities in positional notation across integer radices from 1 (unary) to 64 (Base, BaseAlphabet, NumeralSystem).
    • Evaluates and formats scalar mathematical values (Natural, integer primitives).
    • In positional number systems, leading zeros are semantically inert in value (e.g. 0042 and 42 denote the identical scalar integer).
  2. Delimited Text Stream Parsing & Formatting (String-Oriented):

    • Parses, converts, and formats strings containing one or more numbers separated by delimiters (commas, spaces) or prefixes (0x, 0b, 0o) via format_base_string and base_to_base_string.
    • Configured via BaseStringFormatSettings, supporting custom padding, delimiters, character filtering, and radix prefixes.
    • Supports fixed-width digit chunking via the limit configuration parameter (e.g. limit = 255 allows chunking numbers into byte-sized units).

§Distinction from Byte-Stream Data Armoring

This module is intended for mathematical numerals, not for arbitrary binary byte data armoring:

  • In data armoring (e.g. continuous hex streams, hexdumps, Base16b, or RFC 4648 Base64 byte armoring), exact byte framing is critical: leading 0x00 bytes are real data elements whose removal alters data length and corrupts serialization.
  • In positional numerals (this module), values are abstract scalars. While BaseAlphabet::Base64Standard is provided, it represents a mathematical base-64 positional radix ($b = 64$) over Natural numbers, rather than an octet-stream block armor codec.
  • For raw byte sequence armoring and hexdumps, refer to ctb_formats_armor, ctb_formats_hexdump, and ctb_formats_base64.

Structs§

Base
Represents a mathematical integer base / radix (e.g. 1, 2, 8, 10, 16, 64).
BaseConversionPaddingMode
Zero-padding configuration for delimited base string conversion.
BaseStringFormatSettings
Settings for parsing and formatting delimited strings of numbers across bases.
NumeralSystem
Represents a numeral system defined by a mathematical radix and a digit alphabet.

Enums§

BaseAlphabet
Alphabet used by a numeral system.

Constants§

Binary
Decimal
Hex
Hexadecimal
Octal
Unary

Functions§

base_to_base_string
Parse a string containing numbers in base 2 through 64, convert it to the target base, and print it formatted. Will warn for extra characters other than spaces and commas.
casefold_base_chars_in_string
char_to_digit
Parses a single character into a digit value in the given base.
dec_to_hex_single
dec_to_hex_string
Convenience function to convert a delimited string of decimal numbers to hexadecimal using default format settings.
digit_to_char
Returns the single-character digit for a numeric value in the given base.
format_base_string
Parse a string containing numbers in base 2 through 64 and print it formatted. Will warn for extra characters other than spaces and commas.
format_natural
Formats a Natural number in the given base with optional minimum width.
format_natural_system
Formats a Natural number in the given numeral system with optional minimum width.
get_digits_needed
hex_to_dec_single
hex_to_dec_string
Convenience function to convert a delimited string of hexadecimal numbers to decimal using default format settings.
int_from_base_str_big
Returns the integer represented by n in the requested base.
int_from_base_str_big_alphabet
Returns the integer represented by n in the requested base using the given alphabet.
int_from_base_str_u32
Returns the integer represented by n in the requested base.
int_from_base_str_u128
Returns the integer represented by n in the requested base.
int_to_base_str
Returns the integer represented by n in the requested base.
int_to_base_str_big_alphabet
Returns the string representation of an arbitrary-precision integer in the requested base and alphabet.
is_base_digit
Returns true if the single-character string is a valid digit in the standard alphabet for the given base.
is_base_digit_alphabet
Returns true if the character is a valid digit for the given base in the specified alphabet.
is_base_str
Returns true if all characters in the string are valid digits in the standard alphabet for the given base.
is_base_str_alphabet
Returns true if all characters in the string are valid digits for the given base in the specified alphabet.
is_supported_base
Returns true if the numeric base is supported (1..=64). Radices greater than 36 require an expanded alphabet such as BaseAlphabet::Base64Standard.
is_supported_base_with_default_alphabet
Returns true if the base is supported by the standard alphanumeric alphabet (1..=36).
parse_natural
Parses a string representing a number in the given base into a Natural.
parse_natural_system
Parses a string representing a number in the given numeral system into a Natural.