Expand description
The creation and processing of number format packed structs.
This creates the format specification as a 128-bit packed struct,
represented as a u128 through the NumberFormatBuilder and
with helpers to access format options through NumberFormat.
This has a consistent API whether or not the format
feature is enabled, however, most functionality will be disabled if the
feature is not enabled.
§Creating Formats
Formats can be created through NumberFormatBuilder:
use core::num;
use lexical_util::{NumberFormat, NumberFormatBuilder};
// create the format for literal Rustt floats
const RUST: u128 = NumberFormatBuilder::new()
.digit_separator(num::NonZeroU8::new(b'_'))
.required_digits(true)
.no_positive_mantissa_sign(true)
.no_special(true)
.internal_digit_separator(true)
.trailing_digit_separator(true)
.consecutive_digit_separator(true)
.build_strict();
// then, access the formats's properties
let format = NumberFormat::<{ RUST }> {};
assert!(format.no_positive_mantissa_sign());
assert!(format.no_special());
assert!(format.internal_digit_separator());
assert!(format.trailing_digit_separator());
assert!(format.consecutive_digit_separator());
assert!(!format.no_exponent_notation());These pre-built formats can then be used for FromLexicalWithOptions
and ToLexicalWithOptions conversions.
§Pre-Defined Formats
These are the pre-defined formats for parsing numbers from various programming, markup, and data languages.
STANDARD: Standard number format. This is identical to the Rust string format.
§Low-Level Schema
This describes how to directly get and set flags from the NumberFormat
packed struct. It is not recommended to use these directly, but for example,
the following can be done:
use lexical_util::format;
assert_eq!(
format::NumberFormatBuilder::new()
.required_integer_digits(true)
.build_strict(),
format::STANDARD | format::REQUIRED_INTEGER_DIGITS
);§Syntax Flags
Bitflags to get and set syntax flags for the format packed struct.
REQUIRED_INTEGER_DIGITS: If digits are required before the decimal point.REQUIRED_FRACTION_DIGITS: If digits are required after the decimal point.REQUIRED_EXPONENT_DIGITS: If digits are required after the exponent character.REQUIRED_MANTISSA_DIGITS: If significant digits are required.REQUIRED_DIGITS: If at least 1 digit in the number is required.NO_POSITIVE_MANTISSA_SIGN: If a positive sign before the mantissa is not allowed.REQUIRED_MANTISSA_SIGN: If a sign symbol before the mantissa is required.NO_EXPONENT_NOTATION: If exponent notation is not allowed.NO_POSITIVE_EXPONENT_SIGN: If a positive sign before the exponent is not allowed.REQUIRED_EXPONENT_SIGN: If a sign symbol before the exponent is required.NO_EXPONENT_WITHOUT_FRACTION: If an exponent without fraction is not allowed.NO_SPECIAL: If special (non-finite) values are not allowed.CASE_SENSITIVE_SPECIAL: If special (non-finite) values are case-sensitive.NO_INTEGER_LEADING_ZEROS: If leading zeros before an integer are not allowed.NO_FLOAT_LEADING_ZEROS: If leading zeros before a float are not allowed.REQUIRED_EXPONENT_NOTATION: If exponent notation is required.CASE_SENSITIVE_EXPONENT: If exponent characters are case-sensitive.CASE_SENSITIVE_BASE_PREFIX: If base prefixes are case-sensitive.CASE_SENSITIVE_BASE_SUFFIX: If base suffixes are case-sensitive.
§Digit Separator Flags
Bitflags to get and set digit separators flags for the format packed struct.
INTEGER_INTERNAL_DIGIT_SEPARATOR: If digit separators are allowed between integer digits.FRACTION_INTERNAL_DIGIT_SEPARATOR: If digit separators are allowed between fraction digits.EXPONENT_INTERNAL_DIGIT_SEPARATOR: If digit separators are allowed between exponent digits.INTEGER_LEADING_DIGIT_SEPARATOR: If a digit separator is allowed before any integer digits.FRACTION_LEADING_DIGIT_SEPARATOR: If a digit separator is allowed before any integer digits.EXPONENT_LEADING_DIGIT_SEPARATOR: If a digit separator is allowed before any exponent digits.INTEGER_TRAILING_DIGIT_SEPARATOR: If a digit separator is allowed after any integer digits.FRACTION_TRAILING_DIGIT_SEPARATOR: If a digit separator is allowed after any fraction digits.EXPONENT_TRAILING_DIGIT_SEPARATOR: If a digit separator is allowed after any exponent digits.INTEGER_CONSECUTIVE_DIGIT_SEPARATOR: If multiple consecutive integer digit separators are allowed.FRACTION_CONSECUTIVE_DIGIT_SEPARATOR: If multiple consecutive fraction digit separators are allowed.EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR: If multiple consecutive exponent digit separators are allowed.INTERNAL_DIGIT_SEPARATOR: If digit separators are allowed between digits.LEADING_DIGIT_SEPARATOR: Get if a digit separator is allowed before any digits.TRAILING_DIGIT_SEPARATOR: If a digit separator is allowed after any digits.CONSECUTIVE_DIGIT_SEPARATOR: If multiple consecutive digit separators are allowed.SPECIAL_DIGIT_SEPARATOR: If any digit separators are allowed in special (non-finite) values.
§Character Shifts and Masks
Bitmasks and bit shifts to get and set control characters for the format packed struct.
DIGIT_SEPARATOR_SHIFT: Shift to convert to and from a digit separator as au8.DIGIT_SEPARATOR: Mask to extract the digit separator character.BASE_PREFIX_SHIFT: Shift to convert to and from a base prefix as au8.BASE_PREFIX: Mask to extract the base prefix character.BASE_SUFFIX_SHIFT: Shift to convert to and from a base suffix as au8.BASE_SUFFIX: Mask to extract the base suffix character.MANTISSA_RADIX_SHIFT: Shift to convert to and from a mantissa radix as au32.MANTISSA_RADIX: Mask to extract the mantissa radix: the radix for the significant digits.RADIX_SHIFT: Alias forMANTISSA_RADIX_SHIFT.RADIX: Alias forMANTISSA_RADIX.EXPONENT_BASE_SHIFT: Shift to convert to and from an exponent base as au32.EXPONENT_BASE: Mask to extract the exponent base: the base the exponent is raised to.EXPONENT_RADIX_SHIFT: Shift to convert to and from an exponent radix as au32.EXPONENT_RADIX: Mask to extract the exponent radix: the radix for the exponent digits.
§Character Functions
Functions to get control characters from the format packed struct.
digit_separator: Extract the digit separator from the format packed struct.base_prefix: Extract the base prefix character from the format packed struct.base_suffix: Extract the base suffix character from the format packed struct.mantissa_radix: Extract the mantissa radix from the format packed struct.exponent_base: Extract the exponent base from the format packed struct.exponent_radix: Extract the exponent radix from the format packed struct.
§Validators
Functions to validate control characters for the format packed struct.
is_valid_exponent_flags: Determine if the provided exponent flags are valid.is_valid_digit_separator: Determine if the digit separator is valid.is_valid_base_prefix: Determine if the base prefix character is valid.is_valid_base_suffix: Determine if the base suffix character is valid.is_valid_punctuation: Determine if all of the “punctuation” characters are valid.is_valid_radix: Determine if the radix is valid.
Structs§
- Number
Format - Helper to access features from the packed format struct.
- Number
Format Builder - Validating builder for
NumberFormatfrom the provided specifications.
Constants§
- BASE_
PREFIX - Mask to extract the base prefix character.
- BASE_
PREFIX_ SHIFT - Shift to convert to and from a base prefix as a
u8. - BASE_
SUFFIX - Mask to extract the base suffix character.
- BASE_
SUFFIX_ SHIFT - Shift to convert to and from a base suffix as a
u8. - CASE_
SENSITIVE_ BASE_ PREFIX - Base prefixes are case-sensitive.
- CASE_
SENSITIVE_ BASE_ SUFFIX - Base suffixes are case-sensitive.
- CASE_
SENSITIVE_ EXPONENT - Exponent characters are case-sensitive.
- CASE_
SENSITIVE_ SPECIAL - Special (non-finite) values are case-sensitive.
- CONSECUTIVE_
DIGIT_ SEPARATOR - Multiple consecutive digit separators are allowed.
- DIGIT_
SEPARATOR - Mask to extract the digit separator character.
- DIGIT_
SEPARATOR_ SHIFT - Shift to convert to and from a digit separator as a
u8. - EXPONENT_
BASE - Mask to extract the exponent base: the base the exponent is raised to.
- EXPONENT_
BASE_ SHIFT - Shift to convert to and from an exponent base as a
u32. - EXPONENT_
CONSECUTIVE_ DIGIT_ SEPARATOR - Multiple consecutive exponent digit separators are allowed.
- EXPONENT_
INTERNAL_ DIGIT_ SEPARATOR - Digit separators are allowed between exponent digits.
- EXPONENT_
LEADING_ DIGIT_ SEPARATOR - A digit separator is allowed before any exponent digits.
- EXPONENT_
RADIX - Mask to extract the exponent radix: the radix for the exponent digits.
- EXPONENT_
RADIX_ SHIFT - Shift to convert to and from an exponent radix as a
u32. - EXPONENT_
TRAILING_ DIGIT_ SEPARATOR - A digit separator is allowed after any exponent digits.
- FRACTION_
CONSECUTIVE_ DIGIT_ SEPARATOR - Multiple consecutive fraction digit separators are allowed.
- FRACTION_
INTERNAL_ DIGIT_ SEPARATOR - Digit separators are allowed between fraction digits.
- FRACTION_
LEADING_ DIGIT_ SEPARATOR - A digit separator is allowed before any fraction digits.
- FRACTION_
TRAILING_ DIGIT_ SEPARATOR - A digit separator is allowed after any fraction digits.
- INTEGER_
CONSECUTIVE_ DIGIT_ SEPARATOR - Multiple consecutive integer digit separators are allowed.
- INTEGER_
INTERNAL_ DIGIT_ SEPARATOR - Digit separators are allowed between integer digits.
- INTEGER_
LEADING_ DIGIT_ SEPARATOR - A digit separator is allowed before any integer digits.
- INTEGER_
TRAILING_ DIGIT_ SEPARATOR - A digit separator is allowed after any integer digits.
- INTERNAL_
DIGIT_ SEPARATOR - Digit separators are allowed between digits.
- LEADING_
DIGIT_ SEPARATOR - A digit separator is allowed before any digits.
- MANTISSA_
RADIX - Mask to extract the mantissa radix: the radix for the significant digits.
- MANTISSA_
RADIX_ SHIFT - Shift to convert to and from a mantissa radix as a
u32. - NO_
EXPONENT_ NOTATION - Exponent notation is not allowed.
- NO_
EXPONENT_ WITHOUT_ FRACTION - Exponent without a fraction component is not allowed.
- NO_
FLOAT_ LEADING_ ZEROS - Leading zeros before a float value are not allowed.
- NO_
INTEGER_ LEADING_ ZEROS - Leading zeros before an integer value are not allowed.
- NO_
POSITIVE_ EXPONENT_ SIGN - Positive sign before the exponent is not allowed.
- NO_
POSITIVE_ MANTISSA_ SIGN - Positive sign before the mantissa is not allowed.
- NO_
SPECIAL - Special (non-finite) values are not allowed.
- RADIX
- Alias for
MANTISSA_RADIX. - RADIX_
MASK - Mask to extract the exponent radix: the radix for the exponent digits.
- RADIX_
SHIFT - Alias for
MANTISSA_RADIX_SHIFT. - REQUIRED_
DIGITS - At least 1 digit in the number is required.
- REQUIRED_
EXPONENT_ DIGITS - Digits are required after the exponent character. This check will only occur if the exponent character is present.
- REQUIRED_
EXPONENT_ NOTATION - Exponent notation is required.
- REQUIRED_
EXPONENT_ SIGN - Positive sign before the exponent is required.
- REQUIRED_
FRACTION_ DIGITS - Digits are required after the decimal point. This check will only occur if the decimal point is present.
- REQUIRED_
INTEGER_ DIGITS - Digits are required before the decimal point.
- REQUIRED_
MANTISSA_ DIGITS - Mantissa digits are required (either before or after the decimal point).
- REQUIRED_
MANTISSA_ SIGN - Positive sign before the mantissa is required.
- SPECIAL_
DIGIT_ SEPARATOR - Any digit separators are allowed in special (non-finite) values.
- STANDARD
- Standard number format. This is identical to the Rust string format.
- TRAILING_
DIGIT_ SEPARATOR - A digit separator is allowed after any digits.
Functions§
- format_
error - Get the error type from the format packed struct.
- format_
is_ valid - Determine if the format packed struct is valid.
- is_
valid_ base_ prefix - Determine if the base prefix character is valid.
- is_
valid_ base_ suffix - Determine if the base suffix character is valid.
- is_
valid_ digit_ separator - Determine if the digit separator is valid.
- is_
valid_ exponent_ flags - Determine if the provided exponent flags are valid.
- is_
valid_ punctuation - Determine if all of the “punctuation” characters are valid.
- is_
valid_ radix - Determine if the radix is valid.