Module format

Module format 

Source
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.

§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.

§Digit Separator Flags

Bitflags to get and set digit separators flags for the format packed struct.

§Character Shifts and Masks

Bitmasks and bit shifts to get and set control characters for the format packed struct.

§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.

Structs§

NumberFormatformat
Helper to access features from the packed format struct.
NumberFormatBuilder
Validating builder for NumberFormat from 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.
C11_HEX_LITERALformat and power-of-two
Number format for a C11 literal hexadecimal floating-point number.
C11_HEX_STRINGformat and power-of-two
Number format for a C11 string hexadecimal floating-point number.
C11_LITERALformat
Number format for a C11 literal floating-point number.
C11_STRINGformat
Number format for a C11 string floating-point number.
C18_HEX_LITERALformat and power-of-two
Number format for a C18 literal hexadecimal floating-point number.
C18_HEX_STRINGformat and power-of-two
Number format for a C18 string hexadecimal floating-point number.
C18_LITERALformat
Number format for a C18 literal floating-point number.
C18_STRINGformat
Number format for a C18 string floating-point number.
C89_HEX_STRINGformat and power-of-two
Number format for a C89 string hexadecimal floating-point number.
C89_LITERALformat
Number format for a C89 literal floating-point number.
C89_STRINGformat
Number format for a C89 string floating-point number.
C90_HEX_STRINGformat and power-of-two
Number format for a C90 string hexadecimal floating-point number.
C90_LITERALformat
Number format for a C90 literal floating-point number.
C90_STRINGformat
Number format for a C90 string floating-point number.
C99_HEX_LITERALformat and power-of-two
Number format for a C99 literal hexadecimal floating-point number.
C99_HEX_STRINGformat and power-of-two
Number format for a C99 string hexadecimal floating-point number.
C99_LITERALformat
Number format for a C99 literal floating-point number.
C99_STRINGformat
Number format for a C99 string floating-point number.
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.
CLOJURE_LITERALformat
Number format for a Clojure literal floating-point number.
CLOJURE_STRINGformat
Number format to parse a Clojure float from string.
COBOL_LITERALformat
Number format for a Cobol literal floating-point number.
COBOL_STRINGformat
Number format to parse a Cobol float from string.
COFFEESCRIPT_LITERALformat
Number format for a Coffeescript literal floating-point number.
COFFEESCRIPT_STRINGformat
Number format to parse a Coffeescript float from string.
CONSECUTIVE_DIGIT_SEPARATOR
Multiple consecutive digit separators are allowed.
CSHARP1_LITERALformat
Number format for a C#1 literal floating-point number.
CSHARP1_STRINGformat
Number format to parse a C#1 float from string.
CSHARP2_LITERALformat
Number format for a C#2 literal floating-point number.
CSHARP2_STRINGformat
Number format to parse a C#2 float from string.
CSHARP3_LITERALformat
Number format for a C#3 literal floating-point number.
CSHARP3_STRINGformat
Number format to parse a C#3 float from string.
CSHARP4_LITERALformat
Number format for a C#4 literal floating-point number.
CSHARP4_STRINGformat
Number format to parse a C#4 float from string.
CSHARP5_LITERALformat
Number format for a C#5 literal floating-point number.
CSHARP5_STRINGformat
Number format to parse a C#5 float from string.
CSHARP6_LITERALformat
Number format for a C#6 literal floating-point number.
CSHARP6_STRINGformat
Number format to parse a C#6 float from string.
CSHARP7_LITERALformat
Number format for a C#7 literal floating-point number.
CSHARP7_STRINGformat
Number format to parse a C#7 float from string.
CSHARP_LITERALformat
Number format for a C# literal floating-point number.
CSHARP_STRINGformat
Number format to parse a C# float from string.
CXX03_LITERALformat
Number format for a C++03 literal floating-point number.
CXX03_STRINGformat
Number format for a C++03 string floating-point number.
CXX11_HEX_STRINGformat and power-of-two
Number format for a C++11 string hexadecimal floating-point number.
CXX11_LITERALformat
Number format for a C++11 literal floating-point number.
CXX11_STRINGformat
Number format for a C++11 string floating-point number.
CXX14_HEX_STRINGformat and power-of-two
Number format for a C++14 string hexadecimal floating-point number.
CXX14_LITERALformat
Number format for a C++14 literal floating-point number.
CXX14_STRINGformat
Number format for a C++14 string floating-point number.
CXX17_HEX_LITERALformat and power-of-two
Number format for a C++17 literal hexadecimal floating-point number.
CXX17_HEX_STRINGformat and power-of-two
Number format for a C++17 string hexadecimal floating-point number.
CXX17_LITERALformat
Number format for a C++17 literal floating-point number.
CXX17_STRINGformat
Number format for a C++17 string floating-point number.
CXX20_HEX_LITERALformat and power-of-two
Number format for a C++20 literal hexadecimal floating-point number.
CXX20_HEX_STRINGformat and power-of-two
Number format for a C++20 string hexadecimal floating-point number.
CXX20_LITERALformat
Number format for a C++20 literal floating-point number.
CXX20_STRINGformat
Number format for a C++20 string floating-point number.
CXX98_LITERALformat
Number format for a C++98 literal floating-point number.
CXX98_STRINGformat
Number format for a C++98 string floating-point number.
CXX_HEX_LITERALformat and power-of-two
Number format for a C++ literal hexadecimal floating-point number.
CXX_HEX_STRINGformat and power-of-two
Number format to parse a C++ hexadecimal float from string.
CXX_LITERALformat
Number format for a C++ literal floating-point number.
CXX_STRINGformat
Number format to parse a C++ float from string.
C_HEX_LITERALformat and power-of-two
Number format for a C literal hexadecimal floating-point number.
C_HEX_STRINGformat and power-of-two
Number format to parse a C hexadecimal float from string.
C_LITERALformat
Number format for a C literal floating-point number.
C_STRINGformat
Number format to parse a C float from string.
DIGIT_SEPARATOR
Mask to extract the digit separator character.
DIGIT_SEPARATOR_SHIFT
Shift to convert to and from a digit separator as a u8.
D_LITERALformat
Number format for a D literal floating-point number.
D_STRINGformat
Number format to parse a D float from string.
ELIXIR_LITERALformat
Number format for an Elixir literal floating-point number.
ELIXIR_STRINGformat
Number format to parse an Elixir float from string.
ELM_LITERALformat
Number format for an Elm literal floating-point number.
ELM_STRINGformat
Number format to parse an Elm float from string.
ERLANG_LITERALformat
Number format for an Erlang literal floating-point number.
ERLANG_STRINGformat
Number format to parse an Erlang float from string.
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.
FORTRAN_LITERALformat
Number format for a FORTRAN literal floating-point number.
FORTRAN_STRINGformat
Number format to parse a FORTRAN float from string.
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.
FSHARP_LITERALformat
Number format for a F# literal floating-point number.
FSHARP_STRINGformat
Number format to parse a F# float from string.
GAMBITC_LITERALformat
Number format for a Gambit-C literal floating-point number.
GAMBITC_STRINGformat
Number format to parse a Gambit-C float from string.
GO_LITERALformat
Number format for a Golang literal floating-point number.
GO_STRINGformat
Number format to parse a Golang float from string.
GUILE_LITERALformat
Number format for a Guile literal floating-point number.
GUILE_STRINGformat
Number format to parse a Guile float from string.
HASKELL_LITERALformat
Number format for a Haskell literal floating-point number.
HASKELL_STRINGformat
Number format to parse a Haskell float from string.
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.
JAVASCRIPT_LITERALformat
Number format for a Javascript literal floating-point number.
JAVASCRIPT_STRINGformat
Number format to parse a Javascript float from string.
JAVA_LITERALformat
Number format for a Java literal floating-point number.
JAVA_STRINGformat
Number format to parse a Java float from string.
JSONformat
Number format for a JSON literal floating-point number.
JULIA_HEX_LITERALformat and power-of-two
Number format for a Julia literal floating-point number.
JULIA_HEX_STRINGformat and power-of-two
Number format to parse a Julia float from string.
JULIA_LITERALformat
Number format for a Julia literal floating-point number.
JULIA_STRINGformat
Number format to parse a Julia float from string.
KAWA_LITERALformat
Number format for a Kawa literal floating-point number.
KAWA_STRINGformat
Number format to parse a Kawa float from string.
KOTLIN_LITERALformat
Number format for a Kotlin literal floating-point number.
KOTLIN_STRINGformat
Number format to parse a Kotlin float from string.
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.
MATLAB_LITERALformat
Number format for an Matlab literal floating-point number.
MATLAB_STRINGformat
Number format to parse an Matlab float from string.
MONGODBformat
Number format for a MongoDB literal floating-point number.
MYSQLformat
Number format for a MySQL literal floating-point number.
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.
OBJECTIVEC_LITERALformat
Number format for an Objective-C literal floating-point number.
OBJECTIVEC_STRINGformat
Number format to parse an Objective-C float from string.
OCAML_LITERALformat
Number format for an OCaml literal floating-point number.
OCAML_STRINGformat
Number format to parse an OCaml float from string.
OCTAVE_LITERALformat
Number format for an Octave literal floating-point number.
OCTAVE_STRINGformat
Number format to parse an Octave float from string.
PERL_LITERALformat
Number format for a Perl literal floating-point number.
PERL_STRINGformat
Number format to parse a Perl float from string.
PHP_LITERALformat
Number format for a PHP literal floating-point number.
PHP_STRINGformat
Number format to parse a PHP float from string.
POSTGRESQLformat
Number format for a PostgreSQL literal floating-point number.
PYTHON2_LITERALformat
Number format for a Python2 literal floating-point number.
PYTHON2_STRINGformat
Number format to parse a Python2 float from string.
PYTHON3_LITERALformat
Number format for a Python3 literal floating-point number.
PYTHON3_STRINGformat
Number format to parse a Python3 float from string.
PYTHON35_LITERALformat
Number format for a Python3.5 or lower literal floating-point number.
PYTHON36_LITERALformat
Number format for a Python3.6 or higher literal floating-point number.
PYTHON_LITERALformat
Number format for a Python literal floating-point number.
PYTHON_STRINGformat
Number format to parse a Python float from string.
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.
REASONML_LITERALformat
Number format for a ReasonML literal floating-point number.
REASONML_STRINGformat
Number format to parse a ReasonML float from string.
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.
RUBY_LITERALformat
Number format for a Ruby literal floating-point number.
RUBY_OCTAL_LITERALformat and power-of-two
Number format for an octal Ruby literal floating-point number.
RUBY_STRINGformat
Number format to parse a Ruby float from string.
RUST_LITERALformat
Number format for a Rust literal floating-point number.
RUST_STRINGformat
Number format to parse a Rust float from string.
R_LITERALformat
Number format for an R literal floating-point number.
R_STRINGformat
Number format to parse an R float from string.
SAGE_LITERALformat
Number format for a Sage literal floating-point number.
SAGE_STRINGformat
Number format to parse a Sage float from string.
SCALA_LITERALformat
Number format for a Scala literal floating-point number.
SCALA_STRINGformat
Number format to parse a Scala float from string.
SPECIAL_DIGIT_SEPARATOR
Any digit separators are allowed in special (non-finite) values.
SQLITEformat
Number format for a SQLite literal floating-point number.
STANDARD
Standard number format. This is identical to the Rust string format.
SWIFT_LITERALformat
Number format for a Swift literal floating-point number.
SWIFT_STRINGformat
Number format to parse a Swift float from string.
TOMLformat
Number format for a TOML literal floating-point number.
TRAILING_DIGIT_SEPARATOR
A digit separator is allowed after any digits.
VB_LITERALformat
Number format for a Visual Basic literal floating-point number.
VB_STRINGformat
Number format to parse a Visual Basic float from string.
XMLformat
Number format for an XML literal floating-point number.
YAMLformat
Number format for a YAML literal floating-point number.
ZIG_LITERALformat
Number format for a Zig literal floating-point number.
ZIG_STRINGformat
Number format to parse a Zig float from string.

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.