/rust/registry/src/index.crates.io-1949cf8c6b5b557f/lexical-util-1.0.7/src/format.rs
Line | Count | Source |
1 | | //! The creation and processing of number format packed structs. |
2 | | //! |
3 | | //! This creates the format specification as a 128-bit packed struct, |
4 | | //! represented as a [`u128`] through the [`NumberFormatBuilder`] and |
5 | | //! with helpers to access format options through [`NumberFormat`]. |
6 | | //! |
7 | | //! This has a consistent API whether or not the [`format`][crate#features] |
8 | | //! feature is enabled, however, most functionality will be disabled if the |
9 | | //! feature is not enabled. |
10 | | //! |
11 | | //! # Creating Formats |
12 | | //! |
13 | | //! Formats can be created through [`NumberFormatBuilder`]: |
14 | | //! |
15 | | //! ```rust |
16 | | //! # #[cfg(feature = "format")] { |
17 | | //! use core::num; |
18 | | //! |
19 | | //! use lexical_util::{NumberFormat, NumberFormatBuilder}; |
20 | | //! |
21 | | //! // create the format for literal Rustt floats |
22 | | //! const RUST: u128 = NumberFormatBuilder::new() |
23 | | //! .digit_separator(num::NonZeroU8::new(b'_')) |
24 | | //! .required_digits(true) |
25 | | //! .no_positive_mantissa_sign(true) |
26 | | //! .no_special(true) |
27 | | //! .internal_digit_separator(true) |
28 | | //! .trailing_digit_separator(true) |
29 | | //! .consecutive_digit_separator(true) |
30 | | //! .build_strict(); |
31 | | //! |
32 | | //! // then, access the formats's properties |
33 | | //! let format = NumberFormat::<{ RUST }> {}; |
34 | | //! assert!(format.no_positive_mantissa_sign()); |
35 | | //! assert!(format.no_special()); |
36 | | //! assert!(format.internal_digit_separator()); |
37 | | //! assert!(format.trailing_digit_separator()); |
38 | | //! assert!(format.consecutive_digit_separator()); |
39 | | //! assert!(!format.no_exponent_notation()); |
40 | | //! # } |
41 | | //! ``` |
42 | | //! |
43 | | //! These pre-built formats can then be used for [`FromLexicalWithOptions`] |
44 | | //! and [`ToLexicalWithOptions`] conversions. |
45 | | //! |
46 | | //! # Pre-Defined Formats |
47 | | //! |
48 | | //! These are the pre-defined formats for parsing numbers from various |
49 | | //! programming, markup, and data languages. |
50 | | //! |
51 | | //! - [`STANDARD`]: Standard number format. This is identical to the Rust string |
52 | | //! format. |
53 | | #![cfg_attr( |
54 | | feature = "format", |
55 | | doc = " |
56 | | - [`RUST_LITERAL`]: Number format for a [`Rust`] literal floating-point number. |
57 | | - [`RUST_STRING`]: Number format to parse a [`Rust`] float from string. |
58 | | - [`PYTHON_LITERAL`]: Number format for a [`Python`] literal floating-point number. |
59 | | - [`PYTHON_STRING`]: Number format to parse a [`Python`] float from string. |
60 | | - [`PYTHON3_LITERAL`]: Number format for a [`Python3`] literal floating-point number. |
61 | | - [`PYTHON3_STRING`]: Number format to parse a [`Python3`] float from string. |
62 | | - [`PYTHON36_LITERAL`]: Number format for a [`Python3.6`] or higher literal floating-point number. |
63 | | - [`PYTHON35_LITERAL`]: Number format for a [`Python3.5`] or lower literal floating-point number. |
64 | | - [`PYTHON2_LITERAL`]: Number format for a [`Python2`] literal floating-point number. |
65 | | - [`PYTHON2_STRING`]: Number format to parse a [`Python2`] float from string. |
66 | | - [`CXX_LITERAL`]: Number format for a [`C++`] literal floating-point number. |
67 | | - [`CXX_STRING`]: Number format to parse a [`C++`] float from string. |
68 | | " |
69 | | )] |
70 | | #![cfg_attr( |
71 | | all(feature = "format", feature = "power-of-two"), |
72 | | doc = " |
73 | | - [`CXX_HEX_LITERAL`]: Number format for a [`C++`] literal hexadecimal floating-point number. |
74 | | - [`CXX_HEX_STRING`]: Number format to parse a [`C++`] hexadecimal float from string. |
75 | | " |
76 | | )] |
77 | | #![cfg_attr( |
78 | | feature = "format", |
79 | | doc = " |
80 | | - [`CXX20_LITERAL`]: Number format for a [`C++20`] literal floating-point number. |
81 | | - [`CXX20_STRING`]: Number format for a [`C++20`] string floating-point number. |
82 | | " |
83 | | )] |
84 | | #![cfg_attr( |
85 | | all(feature = "format", feature = "power-of-two"), |
86 | | doc = " |
87 | | - [`CXX20_HEX_LITERAL`]: Number format for a [`C++20`] literal hexadecimal floating-point number. |
88 | | - [`CXX20_HEX_STRING`]: Number format for a [`C++20`] string hexadecimal floating-point number. |
89 | | " |
90 | | )] |
91 | | #![cfg_attr( |
92 | | feature = "format", |
93 | | doc = " |
94 | | - [`CXX17_LITERAL`]: Number format for a [`C++17`] literal floating-point number. |
95 | | - [`CXX17_STRING`]: Number format for a [`C++17`] string floating-point number. |
96 | | " |
97 | | )] |
98 | | #![cfg_attr( |
99 | | all(feature = "format", feature = "power-of-two"), |
100 | | doc = " |
101 | | - [`CXX17_HEX_LITERAL`]: Number format for a [`C++17`] literal hexadecimal floating-point number. |
102 | | - [`CXX17_HEX_STRING`]: Number format for a [`C++17`] string hexadecimal floating-point number. |
103 | | " |
104 | | )] |
105 | | #![cfg_attr( |
106 | | feature = "format", |
107 | | doc = " |
108 | | - [`CXX14_LITERAL`]: Number format for a [`C++14`] literal floating-point number. |
109 | | - [`CXX14_STRING`]: Number format for a [`C++14`] string floating-point number. |
110 | | " |
111 | | )] |
112 | | #![cfg_attr( |
113 | | all(feature = "format", feature = "power-of-two"), |
114 | | doc = " |
115 | | - [`CXX14_HEX_STRING`]: Number format for a [`C++14`] string hexadecimal floating-point number. |
116 | | " |
117 | | )] |
118 | | #![cfg_attr( |
119 | | feature = "format", |
120 | | doc = " |
121 | | - [`CXX11_LITERAL`]: Number format for a [`C++11`] literal floating-point number. |
122 | | - [`CXX11_STRING`]: Number format for a [`C++11`] string floating-point number. |
123 | | " |
124 | | )] |
125 | | #![cfg_attr( |
126 | | all(feature = "format", feature = "power-of-two"), |
127 | | doc = " |
128 | | - [`CXX11_HEX_STRING`]: Number format for a [`C++11`] string hexadecimal floating-point number. |
129 | | " |
130 | | )] |
131 | | #![cfg_attr( |
132 | | feature = "format", |
133 | | doc = " |
134 | | - [`CXX03_LITERAL`]: Number format for a [`C++03`] literal floating-point number. |
135 | | - [`CXX03_STRING`]: Number format for a [`C++03`] string floating-point number. |
136 | | - [`CXX98_LITERAL`]: Number format for a [`C++98`] literal floating-point number. |
137 | | - [`CXX98_STRING`]: Number format for a [`C++98`] string floating-point number. |
138 | | - [`C_LITERAL`]: Number format for a [`C`] literal floating-point number. |
139 | | - [`C_STRING`]: Number format for a [`C`] string floating-point number. |
140 | | " |
141 | | )] |
142 | | #![cfg_attr( |
143 | | all(feature = "format", feature = "power-of-two"), |
144 | | doc = " |
145 | | - [`C_HEX_LITERAL`]: Number format for a [`C`] literal hexadecimal floating-point number. |
146 | | - [`C_HEX_STRING`]: Number format for a [`C`] string hexadecimal floating-point number. |
147 | | " |
148 | | )] |
149 | | #![cfg_attr( |
150 | | feature = "format", |
151 | | doc = " |
152 | | - [`C18_LITERAL`]: Number format for a [`C18`] literal floating-point number. |
153 | | - [`C18_STRING`]: Number format for a [`C18`] string floating-point number. |
154 | | " |
155 | | )] |
156 | | #![cfg_attr( |
157 | | all(feature = "format", feature = "power-of-two"), |
158 | | doc = " |
159 | | - [`C18_HEX_LITERAL`]: Number format for a [`C18`] literal hexadecimal floating-point number. |
160 | | - [`C18_HEX_STRING`]: Number format for a [`C18`] string hexadecimal floating-point number. |
161 | | " |
162 | | )] |
163 | | #![cfg_attr( |
164 | | feature = "format", |
165 | | doc = " |
166 | | - [`C11_LITERAL`]: Number format for a [`C11`] literal floating-point number. |
167 | | - [`C11_STRING`]: Number format for a [`C11`] string floating-point number. |
168 | | " |
169 | | )] |
170 | | #![cfg_attr( |
171 | | all(feature = "format", feature = "power-of-two"), |
172 | | doc = " |
173 | | - [`C11_HEX_LITERAL`]: Number format for a [`C11`] literal hexadecimal floating-point number. |
174 | | - [`C11_HEX_STRING`]: Number format for a [`C11`] string hexadecimal floating-point number. |
175 | | " |
176 | | )] |
177 | | #![cfg_attr( |
178 | | feature = "format", |
179 | | doc = " |
180 | | - [`C99_LITERAL`]: Number format for a [`C99`] literal floating-point number. |
181 | | - [`C99_STRING`]: Number format for a [`C99`] string floating-point number. |
182 | | " |
183 | | )] |
184 | | #![cfg_attr( |
185 | | all(feature = "format", feature = "power-of-two"), |
186 | | doc = " |
187 | | - [`C99_HEX_LITERAL`]: Number format for a [`C99`] literal hexadecimal floating-point number. |
188 | | - [`C99_HEX_STRING`]: Number format for a [`C99`] string hexadecimal floating-point number. |
189 | | " |
190 | | )] |
191 | | #![cfg_attr( |
192 | | feature = "format", |
193 | | doc = " |
194 | | - [`C90_LITERAL`]: Number format for a [`C90`] literal floating-point number. |
195 | | - [`C90_STRING`]: Number format for a [`C90`] string floating-point number. |
196 | | " |
197 | | )] |
198 | | #![cfg_attr( |
199 | | all(feature = "format", feature = "power-of-two"), |
200 | | doc = " |
201 | | - [`C90_HEX_STRING`]: Number format for a [`C90`] string hexadecimal floating-point number. |
202 | | " |
203 | | )] |
204 | | #![cfg_attr( |
205 | | feature = "format", |
206 | | doc = " |
207 | | - [`C89_LITERAL`]: Number format for a [`C89`] literal floating-point number. |
208 | | - [`C89_STRING`]: Number format for a [`C89`] string floating-point number. |
209 | | " |
210 | | )] |
211 | | #![cfg_attr( |
212 | | all(feature = "format", feature = "power-of-two"), |
213 | | doc = " |
214 | | - [`C89_HEX_STRING`]: Number format for a [`C89`] string hexadecimal floating-point number. |
215 | | " |
216 | | )] |
217 | | #![cfg_attr( |
218 | | feature = "format", |
219 | | doc = " |
220 | | - [`RUBY_LITERAL`]: Number format for a [`Ruby`] literal floating-point number. |
221 | | " |
222 | | )] |
223 | | #![cfg_attr( |
224 | | all(feature = "format", feature = "power-of-two"), |
225 | | doc = " |
226 | | - [`RUBY_OCTAL_LITERAL`]: Number format for an octal [`Ruby`] literal floating-point number. |
227 | | " |
228 | | )] |
229 | | #![cfg_attr( |
230 | | feature = "format", |
231 | | doc = " |
232 | | - [`RUBY_STRING`]: Number format to parse a [`Ruby`] float from string. |
233 | | - [`SWIFT_LITERAL`]: Number format for a [`Swift`] literal floating-point number. |
234 | | - [`SWIFT_STRING`]: Number format to parse a [`Swift`] float from string. |
235 | | - [`GO_LITERAL`]: Number format for a [`Golang`] literal floating-point number. |
236 | | - [`GO_STRING`]: Number format to parse a [`Golang`] float from string. |
237 | | - [`HASKELL_LITERAL`]: Number format for a [`Haskell`] literal floating-point number. |
238 | | - [`HASKELL_STRING`]: Number format to parse a [`Haskell`] float from string. |
239 | | - [`JAVASCRIPT_LITERAL`]: Number format for a [`Javascript`] literal floating-point number. |
240 | | - [`JAVASCRIPT_STRING`]: Number format to parse a [`Javascript`] float from string. |
241 | | - [`PERL_LITERAL`]: Number format for a [`Perl`] literal floating-point number. |
242 | | - [`PERL_STRING`]: Number format to parse a [`Perl`] float from string. |
243 | | - [`PHP_LITERAL`]: Number format for a [`PHP`] literal floating-point number. |
244 | | - [`PHP_STRING`]: Number format to parse a [`PHP`] float from string. |
245 | | - [`JAVA_LITERAL`]: Number format for a [`Java`] literal floating-point number. |
246 | | - [`JAVA_STRING`]: Number format to parse a [`Java`] float from string. |
247 | | - [`R_LITERAL`]: Number format for an [`R`] literal floating-point number. |
248 | | - [`R_STRING`]: Number format to parse an [`R`] float from string. |
249 | | - [`KOTLIN_LITERAL`]: Number format for a [`Kotlin`] literal floating-point number. |
250 | | - [`KOTLIN_STRING`]: Number format to parse a [`Kotlin`] float from string. |
251 | | - [`JULIA_LITERAL`]: Number format for a [`Julia`] literal floating-point number. |
252 | | - [`JULIA_STRING`]: Number format to parse a [`Julia`] float from string. |
253 | | " |
254 | | )] |
255 | | #![cfg_attr( |
256 | | all(feature = "format", feature = "power-of-two"), |
257 | | doc = " |
258 | | - [`JULIA_HEX_LITERAL`]: Number format for a [`Julia`] literal floating-point number. |
259 | | - [`JULIA_HEX_STRING`]: Number format to parse a [`Julia`] float from string. |
260 | | " |
261 | | )] |
262 | | #![cfg_attr( |
263 | | feature = "format", |
264 | | doc = " |
265 | | - [`CSHARP_LITERAL`]: Number format for a [`C#`] literal floating-point number. |
266 | | - [`CSHARP_STRING`]: Number format to parse a [`C#`] float from string. |
267 | | - [`CSHARP7_LITERAL`]: Number format for a [`C#7`] literal floating-point number. |
268 | | - [`CSHARP7_STRING`]: Number format to parse a [`C#7`] float from string. |
269 | | - [`CSHARP6_LITERAL`]: Number format for a [`C#6`] literal floating-point number. |
270 | | - [`CSHARP6_STRING`]: Number format to parse a [`C#6`] float from string. |
271 | | - [`CSHARP5_LITERAL`]: Number format for a [`C#5`] literal floating-point number. |
272 | | - [`CSHARP5_STRING`]: Number format to parse a [`C#5`] float from string. |
273 | | - [`CSHARP4_LITERAL`]: Number format for a [`C#4`] literal floating-point number. |
274 | | - [`CSHARP4_STRING`]: Number format to parse a [`C#4`] float from string. |
275 | | - [`CSHARP3_LITERAL`]: Number format for a [`C#3`] literal floating-point number. |
276 | | - [`CSHARP3_STRING`]: Number format to parse a [`C#3`] float from string. |
277 | | - [`CSHARP2_LITERAL`]: Number format for a [`C#2`] literal floating-point number. |
278 | | - [`CSHARP2_STRING`]: Number format to parse a [`C#2`] float from string. |
279 | | - [`CSHARP1_LITERAL`]: Number format for a [`C#1`] literal floating-point number. |
280 | | - [`CSHARP1_STRING`]: Number format to parse a [`C#1`] float from string. |
281 | | - [`KAWA_LITERAL`]: Number format for a [`Kawa`] literal floating-point number. |
282 | | - [`KAWA_STRING`]: Number format to parse a [`Kawa`] float from string. |
283 | | - [`GAMBITC_LITERAL`]: Number format for a [`Gambit-C`] literal floating-point number. |
284 | | - [`GAMBITC_STRING`]: Number format to parse a [`Gambit-C`] float from string. |
285 | | - [`GUILE_LITERAL`]: Number format for a [`Guile`] literal floating-point number. |
286 | | - [`GUILE_STRING`]: Number format to parse a [`Guile`] float from string. |
287 | | - [`CLOJURE_LITERAL`]: Number format for a [`Clojure`] literal floating-point number. |
288 | | - [`CLOJURE_STRING`]: Number format to parse a [`Clojure`] float from string. |
289 | | - [`ERLANG_LITERAL`]: Number format for an [`Erlang`] literal floating-point number. |
290 | | - [`ERLANG_STRING`]: Number format to parse an [`Erlang`] float from string. |
291 | | - [`ELM_LITERAL`]: Number format for an [`Elm`] literal floating-point number. |
292 | | - [`ELM_STRING`]: Number format to parse an [`Elm`] float from string. |
293 | | - [`SCALA_LITERAL`]: Number format for a [`Scala`] literal floating-point number. |
294 | | - [`SCALA_STRING`]: Number format to parse a [`Scala`] float from string. |
295 | | - [`ELIXIR_LITERAL`]: Number format for an [`Elixir`] literal floating-point number. |
296 | | - [`ELIXIR_STRING`]: Number format to parse an [`Elixir`] float from string. |
297 | | - [`FORTRAN_LITERAL`]: Number format for a [`FORTRAN`] literal floating-point number. |
298 | | - [`FORTRAN_STRING`]: Number format to parse a [`FORTRAN`] float from string. |
299 | | - [`D_LITERAL`]: Number format for a [`D`] literal floating-point number. |
300 | | - [`D_STRING`]: Number format to parse a [`D`] float from string. |
301 | | - [`COFFEESCRIPT_LITERAL`]: Number format for a [`Coffeescript`] literal floating-point number. |
302 | | - [`COFFEESCRIPT_STRING`]: Number format to parse a [`Coffeescript`] float from string. |
303 | | - [`COBOL_LITERAL`]: Number format for a [`Cobol`] literal floating-point number. |
304 | | - [`COBOL_STRING`]: Number format to parse a [`Cobol`] float from string. |
305 | | - [`FSHARP_LITERAL`]: Number format for a [`F#`] literal floating-point number. |
306 | | - [`FSHARP_STRING`]: Number format to parse a [`F#`] float from string. |
307 | | - [`VB_LITERAL`]: Number format for a [`Visual Basic`] literal floating-point number. |
308 | | - [`VB_STRING`]: Number format to parse a [`Visual Basic`] float from string. |
309 | | - [`OCAML_LITERAL`]: Number format for an [`OCaml`] literal floating-point number. |
310 | | - [`OCAML_STRING`]: Number format to parse an [`OCaml`] float from string. |
311 | | - [`OBJECTIVEC_LITERAL`]: Number format for an [`Objective-C`] literal floating-point number. |
312 | | - [`OBJECTIVEC_STRING`]: Number format to parse an [`Objective-C`] float from string. |
313 | | - [`REASONML_LITERAL`]: Number format for a [`ReasonML`] literal floating-point number. |
314 | | - [`REASONML_STRING`]: Number format to parse a [`ReasonML`] float from string. |
315 | | - [`OCTAVE_LITERAL`]: Number format for an [`Octave`] literal floating-point number. |
316 | | - [`OCTAVE_STRING`]: Number format to parse an [`Octave`] float from string. |
317 | | - [`MATLAB_LITERAL`]: Number format for an [`Matlab`] literal floating-point number. |
318 | | - [`MATLAB_STRING`]: Number format to parse an [`Matlab`] float from string. |
319 | | - [`ZIG_LITERAL`]: Number format for a [`Zig`] literal floating-point number. |
320 | | - [`ZIG_STRING`]: Number format to parse a [`Zig`] float from string. |
321 | | - [`SAGE_LITERAL`]: Number format for a [`Sage`] literal floating-point number. |
322 | | - [`SAGE_STRING`]: Number format to parse a [`Sage`] float from string. |
323 | | - [`JSON`]: Number format for a [`JSON`][`JSON-REF`] literal floating-point number. |
324 | | - [`TOML`]: Number format for a [`TOML`][`TOML-REF`] literal floating-point number. |
325 | | - [`YAML`]: Number format for a [`YAML`][`YAML-REF`] literal floating-point number. |
326 | | - [`XML`]: Number format for an [`XML`][`XML-REF`] literal floating-point number. |
327 | | - [`SQLITE`]: Number format for a [`SQLite`] literal floating-point number. |
328 | | - [`POSTGRESQL`]: Number format for a [`PostgreSQL`] literal floating-point number. |
329 | | - [`MYSQL`]: Number format for a [`MySQL`] literal floating-point number. |
330 | | - [`MONGODB`]: Number format for a [`MongoDB`] literal floating-point number. |
331 | | " |
332 | | )] |
333 | | //! |
334 | | #![cfg_attr( |
335 | | any(feature = "parse-floats", feature = "parse-integers"), |
336 | | doc = "[`FromLexicalWithOptions`]: crate::from_lexical_with_options" |
337 | | )] |
338 | | #![cfg_attr( |
339 | | not(any(feature = "parse-floats", feature = "parse-integers")), |
340 | | doc = "[`FromLexicalWithOptions`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/api.rs#L45" |
341 | | )] |
342 | | #![cfg_attr( |
343 | | any(feature = "write-floats", feature = "write-integers"), |
344 | | doc = "[`ToLexicalWithOptions`]: crate::to_lexical_with_options" |
345 | | )] |
346 | | #![cfg_attr( |
347 | | not(any(feature = "write-floats", feature = "write-integers")), |
348 | | doc = "[`ToLexicalWithOptions`]: https://github.com/Alexhuszagh/rust-lexical/blob/c6c5052/lexical-util/src/api.rs#L151" |
349 | | )] |
350 | | //! |
351 | | //! # Low-Level Schema |
352 | | //! |
353 | | //! This describes how to directly get and set flags from the [`NumberFormat`] |
354 | | //! packed struct. It is not recommended to use these directly, but for example, |
355 | | //! the following can be done: |
356 | | //! |
357 | | //! ```rust |
358 | | //! # #[cfg(feature = "format")] { |
359 | | //! use lexical_util::format; |
360 | | //! |
361 | | //! assert_eq!( |
362 | | //! format::NumberFormatBuilder::new() |
363 | | //! .required_integer_digits(true) |
364 | | //! .build_strict(), |
365 | | //! format::STANDARD | format::REQUIRED_INTEGER_DIGITS |
366 | | //! ); |
367 | | //! # } |
368 | | //! ``` |
369 | | //! |
370 | | //! ## Syntax Flags |
371 | | //! |
372 | | //! Bitflags to get and set syntax flags for the format packed struct. |
373 | | //! |
374 | | //! - [`REQUIRED_INTEGER_DIGITS`]: If digits are required before the decimal |
375 | | //! point. |
376 | | //! - [`REQUIRED_FRACTION_DIGITS`]: If digits are required after the decimal |
377 | | //! point. |
378 | | //! - [`REQUIRED_EXPONENT_DIGITS`]: If digits are required after the exponent |
379 | | //! character. |
380 | | //! - [`REQUIRED_MANTISSA_DIGITS`]: If significant digits are required. |
381 | | //! - [`REQUIRED_DIGITS`]: If at least 1 digit in the number is required. |
382 | | //! - [`NO_POSITIVE_MANTISSA_SIGN`]: If a positive sign before the mantissa is |
383 | | //! not allowed. |
384 | | //! - [`REQUIRED_MANTISSA_SIGN`]: If a sign symbol before the mantissa is |
385 | | //! required. |
386 | | //! - [`NO_EXPONENT_NOTATION`]: If exponent notation is not allowed. |
387 | | //! - [`NO_POSITIVE_EXPONENT_SIGN`]: If a positive sign before the exponent is |
388 | | //! not allowed. |
389 | | //! - [`REQUIRED_EXPONENT_SIGN`]: If a sign symbol before the exponent is |
390 | | //! required. |
391 | | //! - [`NO_EXPONENT_WITHOUT_FRACTION`]: If an exponent without fraction is not |
392 | | //! allowed. |
393 | | //! - [`NO_SPECIAL`]: If special (non-finite) values are not allowed. |
394 | | //! - [`CASE_SENSITIVE_SPECIAL`]: If special (non-finite) values are |
395 | | //! case-sensitive. |
396 | | //! - [`NO_INTEGER_LEADING_ZEROS`]: If leading zeros before an integer are not |
397 | | //! allowed. |
398 | | //! - [`NO_FLOAT_LEADING_ZEROS`]: If leading zeros before a float are not |
399 | | //! allowed. |
400 | | //! - [`REQUIRED_EXPONENT_NOTATION`]: If exponent notation is required. |
401 | | //! - [`CASE_SENSITIVE_EXPONENT`]: If exponent characters are case-sensitive. |
402 | | //! - [`CASE_SENSITIVE_BASE_PREFIX`]: If base prefixes are case-sensitive. |
403 | | //! - [`CASE_SENSITIVE_BASE_SUFFIX`]: If base suffixes are case-sensitive. |
404 | | //! |
405 | | //! [`REQUIRED_INTEGER_DIGITS`]: NumberFormat::REQUIRED_INTEGER_DIGITS |
406 | | //! [`REQUIRED_FRACTION_DIGITS`]: NumberFormat::REQUIRED_FRACTION_DIGITS |
407 | | //! [`REQUIRED_EXPONENT_DIGITS`]: NumberFormat::REQUIRED_EXPONENT_DIGITS |
408 | | //! [`REQUIRED_MANTISSA_DIGITS`]: NumberFormat::REQUIRED_MANTISSA_DIGITS |
409 | | //! [`REQUIRED_DIGITS`]: NumberFormat::REQUIRED_DIGITS |
410 | | //! [`NO_POSITIVE_MANTISSA_SIGN`]: NumberFormat::NO_POSITIVE_MANTISSA_SIGN |
411 | | //! [`REQUIRED_MANTISSA_SIGN`]: NumberFormat::REQUIRED_MANTISSA_SIGN |
412 | | //! [`NO_EXPONENT_NOTATION`]: NumberFormat::NO_EXPONENT_NOTATION |
413 | | //! [`NO_POSITIVE_EXPONENT_SIGN`]: NumberFormat::NO_POSITIVE_EXPONENT_SIGN |
414 | | //! [`REQUIRED_EXPONENT_SIGN`]: NumberFormat::REQUIRED_EXPONENT_SIGN |
415 | | //! [`NO_EXPONENT_WITHOUT_FRACTION`]: NumberFormat::NO_EXPONENT_WITHOUT_FRACTION |
416 | | //! [`NO_SPECIAL`]: NumberFormat::NO_SPECIAL |
417 | | //! [`CASE_SENSITIVE_SPECIAL`]: NumberFormat::CASE_SENSITIVE_SPECIAL |
418 | | //! [`NO_INTEGER_LEADING_ZEROS`]: NumberFormat::NO_INTEGER_LEADING_ZEROS |
419 | | //! [`NO_FLOAT_LEADING_ZEROS`]: NumberFormat::NO_FLOAT_LEADING_ZEROS |
420 | | //! [`REQUIRED_EXPONENT_NOTATION`]: NumberFormat::REQUIRED_EXPONENT_NOTATION |
421 | | //! [`CASE_SENSITIVE_EXPONENT`]: NumberFormat::CASE_SENSITIVE_EXPONENT |
422 | | //! [`CASE_SENSITIVE_BASE_PREFIX`]: NumberFormat::CASE_SENSITIVE_BASE_PREFIX |
423 | | //! [`CASE_SENSITIVE_BASE_SUFFIX`]: NumberFormat::CASE_SENSITIVE_BASE_SUFFIX |
424 | | //! |
425 | | //! ## Digit Separator Flags |
426 | | //! |
427 | | //! Bitflags to get and set digit separators flags for the format |
428 | | //! packed struct. |
429 | | //! |
430 | | //! - [`INTEGER_INTERNAL_DIGIT_SEPARATOR`]: If digit separators are allowed |
431 | | //! between integer digits. |
432 | | //! - [`FRACTION_INTERNAL_DIGIT_SEPARATOR`]: If digit separators are allowed |
433 | | //! between fraction digits. |
434 | | //! - [`EXPONENT_INTERNAL_DIGIT_SEPARATOR`]: If digit separators are allowed |
435 | | //! between exponent digits. |
436 | | //! - [`INTEGER_LEADING_DIGIT_SEPARATOR`]: If a digit separator is allowed |
437 | | //! before any integer digits. |
438 | | //! - [`FRACTION_LEADING_DIGIT_SEPARATOR`]: If a digit separator is allowed |
439 | | //! before any integer digits. |
440 | | //! - [`EXPONENT_LEADING_DIGIT_SEPARATOR`]: If a digit separator is allowed |
441 | | //! before any exponent digits. |
442 | | //! - [`INTEGER_TRAILING_DIGIT_SEPARATOR`]: If a digit separator is allowed |
443 | | //! after any integer digits. |
444 | | //! - [`FRACTION_TRAILING_DIGIT_SEPARATOR`]: If a digit separator is allowed |
445 | | //! after any fraction digits. |
446 | | //! - [`EXPONENT_TRAILING_DIGIT_SEPARATOR`]: If a digit separator is allowed |
447 | | //! after any exponent digits. |
448 | | //! - [`INTEGER_CONSECUTIVE_DIGIT_SEPARATOR`]: If multiple consecutive integer |
449 | | //! digit separators are allowed. |
450 | | //! - [`FRACTION_CONSECUTIVE_DIGIT_SEPARATOR`]: If multiple consecutive fraction |
451 | | //! digit separators are allowed. |
452 | | //! - [`EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR`]: If multiple consecutive exponent |
453 | | //! digit separators are allowed. |
454 | | //! - [`INTERNAL_DIGIT_SEPARATOR`]: If digit separators are allowed between |
455 | | //! digits. |
456 | | //! - [`LEADING_DIGIT_SEPARATOR`]: Get if a digit separator is allowed before |
457 | | //! any digits. |
458 | | //! - [`TRAILING_DIGIT_SEPARATOR`]: If a digit separator is allowed after any |
459 | | //! digits. |
460 | | //! - [`CONSECUTIVE_DIGIT_SEPARATOR`]: If multiple consecutive digit separators |
461 | | //! are allowed. |
462 | | //! - [`SPECIAL_DIGIT_SEPARATOR`]: If any digit separators are allowed in |
463 | | //! special (non-finite) values. |
464 | | //! |
465 | | //! [`INTEGER_INTERNAL_DIGIT_SEPARATOR`]: NumberFormat::INTEGER_INTERNAL_DIGIT_SEPARATOR |
466 | | //! [`FRACTION_INTERNAL_DIGIT_SEPARATOR`]: NumberFormat::FRACTION_INTERNAL_DIGIT_SEPARATOR |
467 | | //! [`EXPONENT_INTERNAL_DIGIT_SEPARATOR`]: NumberFormat::EXPONENT_INTERNAL_DIGIT_SEPARATOR |
468 | | //! [`INTEGER_LEADING_DIGIT_SEPARATOR`]: NumberFormat::INTEGER_LEADING_DIGIT_SEPARATOR |
469 | | //! [`FRACTION_LEADING_DIGIT_SEPARATOR`]: NumberFormat::FRACTION_LEADING_DIGIT_SEPARATOR |
470 | | //! [`EXPONENT_LEADING_DIGIT_SEPARATOR`]: NumberFormat::EXPONENT_LEADING_DIGIT_SEPARATOR |
471 | | //! [`INTEGER_TRAILING_DIGIT_SEPARATOR`]: NumberFormat::INTEGER_TRAILING_DIGIT_SEPARATOR |
472 | | //! [`FRACTION_TRAILING_DIGIT_SEPARATOR`]: NumberFormat::FRACTION_TRAILING_DIGIT_SEPARATOR |
473 | | //! [`EXPONENT_TRAILING_DIGIT_SEPARATOR`]: NumberFormat::EXPONENT_TRAILING_DIGIT_SEPARATOR |
474 | | //! [`INTEGER_CONSECUTIVE_DIGIT_SEPARATOR`]: NumberFormat::INTEGER_CONSECUTIVE_DIGIT_SEPARATOR |
475 | | //! [`FRACTION_CONSECUTIVE_DIGIT_SEPARATOR`]: NumberFormat::FRACTION_CONSECUTIVE_DIGIT_SEPARATOR |
476 | | //! [`EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR`]: NumberFormat::EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR |
477 | | //! [`INTERNAL_DIGIT_SEPARATOR`]: NumberFormat::INTERNAL_DIGIT_SEPARATOR |
478 | | //! [`LEADING_DIGIT_SEPARATOR`]: NumberFormat::LEADING_DIGIT_SEPARATOR |
479 | | //! [`TRAILING_DIGIT_SEPARATOR`]: NumberFormat::TRAILING_DIGIT_SEPARATOR |
480 | | //! [`CONSECUTIVE_DIGIT_SEPARATOR`]: NumberFormat::CONSECUTIVE_DIGIT_SEPARATOR |
481 | | //! [`SPECIAL_DIGIT_SEPARATOR`]: NumberFormat::SPECIAL_DIGIT_SEPARATOR |
482 | | //! |
483 | | //! ## Character Shifts and Masks |
484 | | //! |
485 | | //! Bitmasks and bit shifts to get and set control characters for the format |
486 | | //! packed struct. |
487 | | //! |
488 | | //! - [`DIGIT_SEPARATOR_SHIFT`]: Shift to convert to and from a digit separator |
489 | | //! as a `u8`. |
490 | | //! - [`DIGIT_SEPARATOR`]: Mask to extract the digit separator character. |
491 | | //! - [`BASE_PREFIX_SHIFT`]: Shift to convert to and from a base prefix as a |
492 | | //! `u8`. |
493 | | //! - [`BASE_PREFIX`]: Mask to extract the base prefix character. |
494 | | //! - [`BASE_SUFFIX_SHIFT`]: Shift to convert to and from a base suffix as a |
495 | | //! `u8`. |
496 | | //! - [`BASE_SUFFIX`]: Mask to extract the base suffix character. |
497 | | //! - [`MANTISSA_RADIX_SHIFT`]: Shift to convert to and from a mantissa radix as |
498 | | //! a `u32`. |
499 | | //! - [`MANTISSA_RADIX`]: Mask to extract the mantissa radix: the radix for the |
500 | | //! significant digits. |
501 | | //! - [`RADIX_SHIFT`]: Alias for [`MANTISSA_RADIX_SHIFT`]. |
502 | | //! - [`RADIX`]: Alias for [`MANTISSA_RADIX`]. |
503 | | //! - [`EXPONENT_BASE_SHIFT`]: Shift to convert to and from an exponent base as |
504 | | //! a `u32`. |
505 | | //! - [`EXPONENT_BASE`]: Mask to extract the exponent base: the base the |
506 | | //! exponent is raised to. |
507 | | //! - [`EXPONENT_RADIX_SHIFT`]: Shift to convert to and from an exponent radix |
508 | | //! as a `u32`. |
509 | | //! - [`EXPONENT_RADIX`]: Mask to extract the exponent radix: the radix for the |
510 | | //! exponent digits. |
511 | | //! |
512 | | //! [`DIGIT_SEPARATOR_SHIFT`]: DIGIT_SEPARATOR_SHIFT |
513 | | //! [`DIGIT_SEPARATOR`]: NumberFormat::DIGIT_SEPARATOR |
514 | | //! [`BASE_PREFIX_SHIFT`]: BASE_PREFIX_SHIFT |
515 | | //! [`BASE_PREFIX`]: NumberFormat::BASE_PREFIX |
516 | | //! [`BASE_SUFFIX_SHIFT`]: BASE_SUFFIX_SHIFT |
517 | | //! [`BASE_SUFFIX`]: NumberFormat::BASE_SUFFIX |
518 | | //! [`MANTISSA_RADIX_SHIFT`]: MANTISSA_RADIX_SHIFT |
519 | | //! [`MANTISSA_RADIX`]: NumberFormat::MANTISSA_RADIX |
520 | | //! [`RADIX_SHIFT`]: RADIX_SHIFT |
521 | | //! [`RADIX`]: NumberFormat::RADIX |
522 | | //! [`EXPONENT_BASE_SHIFT`]: EXPONENT_BASE_SHIFT |
523 | | //! [`EXPONENT_BASE`]: NumberFormat::EXPONENT_BASE |
524 | | //! [`EXPONENT_RADIX_SHIFT`]: EXPONENT_RADIX_SHIFT |
525 | | //! [`EXPONENT_RADIX`]: crate::NumberFormat::EXPONENT_RADIX |
526 | | //! |
527 | | //! ## Character Functions |
528 | | //! |
529 | | //! Functions to get control characters from the format packed struct. |
530 | | //! |
531 | | //! - [`digit_separator`]: Extract the digit separator from the format packed |
532 | | //! struct. |
533 | | //! - [`base_prefix`]: Extract the base prefix character from the format packed |
534 | | //! struct. |
535 | | //! - [`base_suffix`]: Extract the base suffix character from the format packed |
536 | | //! struct. |
537 | | //! - [`mantissa_radix`]: Extract the mantissa radix from the format packed |
538 | | //! struct. |
539 | | //! - [`exponent_base`]: Extract the exponent base from the format packed |
540 | | //! struct. |
541 | | //! - [`exponent_radix`]: Extract the exponent radix from the format packed |
542 | | //! struct. |
543 | | //! |
544 | | //! ## Validators |
545 | | //! |
546 | | //! Functions to validate control characters for the format packed struct. |
547 | | //! |
548 | | //! - [`is_valid_exponent_flags`]: Determine if the provided exponent flags are |
549 | | //! valid. |
550 | | //! - [`is_valid_digit_separator`]: Determine if the digit separator is valid. |
551 | | //! - [`is_valid_base_prefix`]: Determine if the base prefix character is valid. |
552 | | //! - [`is_valid_base_suffix`]: Determine if the base suffix character is valid. |
553 | | //! - [`is_valid_punctuation`]: Determine if all of the "punctuation" characters |
554 | | //! are valid. |
555 | | //! - [`is_valid_radix`]: Determine if the radix is valid. |
556 | | //! |
557 | | //! <!-- References --> |
558 | | #![cfg_attr( |
559 | | feature = "format", |
560 | | doc = " |
561 | | [`Rust`]: https://www.rust-lang.org/ |
562 | | [`Python`]: https://www.python.org/ |
563 | | [`Python3`]: https://www.python.org/ |
564 | | [`Python3.6`]: https://www.python.org/downloads/release/python-360/ |
565 | | [`Python3.5`]: https://www.python.org/downloads/release/python-350/ |
566 | | [`Python2`]: https://www.python.org/downloads/release/python-270/ |
567 | | [`C++`]: https://en.cppreference.com/w/ |
568 | | [`C++20`]: https://en.cppreference.com/w/cpp/20 |
569 | | [`C++17`]: https://en.cppreference.com/w/cpp/17 |
570 | | [`C++14`]: https://en.cppreference.com/w/cpp/14 |
571 | | [`C++11`]: https://en.cppreference.com/w/cpp/11 |
572 | | [`C++03`]: https://en.wikipedia.org/wiki/C%2B%2B03 |
573 | | [`C++98`]: https://en.cppreference.com/w/ |
574 | | [`C`]: https://en.cppreference.com/w/c |
575 | | [`C18`]: https://en.cppreference.com/w/c/17 |
576 | | [`C11`]: https://en.cppreference.com/w/c/11 |
577 | | [`C99`]: https://en.cppreference.com/w/c/99 |
578 | | [`C90`]: https://en.cppreference.com/w/c |
579 | | [`C89`]: https://en.cppreference.com/w/c |
580 | | [`Ruby`]: https://www.ruby-lang.org/en/ |
581 | | [`Swift`]: https://developer.apple.com/swift/ |
582 | | [`Golang`]: https://go.dev/ |
583 | | [`Haskell`]: https://www.haskell.org/ |
584 | | [`Javascript`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript |
585 | | [`Perl`]: https://www.perl.org/ |
586 | | [`PHP`]: https://www.php.net/ |
587 | | [`Java`]: https://www.java.com/en/ |
588 | | [`R`]: https://www.r-project.org/ |
589 | | [`Kotlin`]: https://kotlinlang.org/ |
590 | | [`Julia`]: https://julialang.org/ |
591 | | [`C#`]: https://learn.microsoft.com/en-us/dotnet/csharp/ |
592 | | [`C#7`]: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history#c-version-73 |
593 | | [`C#6`]: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history#c-version-60 |
594 | | [`C#5`]: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history#c-version-50 |
595 | | [`C#4`]: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history#c-version-40 |
596 | | [`C#3`]: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history#c-version-30 |
597 | | [`C#2`]: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history#c-version-20 |
598 | | [`C#1`]: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history#c-version-12-1 |
599 | | [`Kawa`]: https://www.gnu.org/software/kawa/ |
600 | | [`Gambit-C`]: https://gambitscheme.org/ |
601 | | [`Guile`]: https://www.gnu.org/software/guile/ |
602 | | [`Clojure`]: https://clojure.org/ |
603 | | [`Erlang`]: https://www.erlang.org/ |
604 | | [`Elm`]: https://elm-lang.org/ |
605 | | [`Scala`]: https://www.scala-lang.org/ |
606 | | [`Elixir`]: https://elixir-lang.org/ |
607 | | [`FORTRAN`]: https://fortran-lang.org/ |
608 | | [`D`]: https://dlang.org/ |
609 | | [`Coffeescript`]: https://coffeescript.org/ |
610 | | [`Cobol`]: https://www.ibm.com/think/topics/cobol |
611 | | [`F#`]: https://fsharp.org/ |
612 | | [`Visual Basic`]: https://learn.microsoft.com/en-us/dotnet/visual-basic/ |
613 | | [`OCaml`]: https://ocaml.org/ |
614 | | [`Objective-C`]: https://en.wikipedia.org/wiki/Objective-C |
615 | | [`ReasonML`]: https://reasonml.github.io/ |
616 | | [`Octave`]: https://octave.org/ |
617 | | [`Matlab`]: https://www.mathworks.com/products/matlab.html |
618 | | [`Zig`]: https://ziglang.org/ |
619 | | [`Sage`]: https://www.sagemath.org/ |
620 | | [`JSON-REF`]: https://www.json.org/json-en.html |
621 | | [`TOML-REF`]: https://toml.io/en/ |
622 | | [`YAML-REF`]: https://yaml.org/ |
623 | | [`XML-REF`]: https://en.wikipedia.org/wiki/XML |
624 | | [`SQLite`]: https://www.sqlite.org/ |
625 | | [`PostgreSQL`]: https://www.postgresql.org/ |
626 | | [`MySQL`]: https://www.mysql.com/ |
627 | | [`MongoDB`]: https://www.mongodb.com/ |
628 | | " |
629 | | )] |
630 | | |
631 | | use crate::error::Error; |
632 | | #[cfg(feature = "format")] |
633 | | pub use crate::feature_format::*; |
634 | | pub use crate::format_builder::*; |
635 | | pub use crate::format_flags::*; |
636 | | #[cfg(not(feature = "format"))] |
637 | | pub use crate::not_feature_format::*; |
638 | | #[cfg(feature = "format")] |
639 | | pub use crate::prebuilt_formats::*; |
640 | | |
641 | | /// Determine if the format packed struct is valid. |
642 | | #[inline(always)] |
643 | 0 | pub const fn format_is_valid<const FORMAT: u128>() -> bool { |
644 | 0 | NumberFormat::<FORMAT> {}.is_valid() |
645 | 0 | } |
646 | | |
647 | | /// Get the error type from the format packed struct. |
648 | | /// |
649 | | /// An error type of `Error::Success` means the format is valid, any |
650 | | /// other error signifies an invalid format. |
651 | | #[inline(always)] |
652 | 0 | pub const fn format_error<const FORMAT: u128>() -> Error { |
653 | 0 | NumberFormat::<FORMAT> {}.error() |
654 | 0 | } |
655 | | |
656 | | /// Standard number format. This is identical to the Rust string format. |
657 | | pub const STANDARD: u128 = NumberFormatBuilder::new().build_strict(); |