1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
//! Fast lexical conversion routines for a no_std environment. //! //! lexical-core is a low-level API for number-to-string and //! string-to-number conversions, without requiring a system //! allocator. If you would like to use a convenient, high-level //! API, please look at [lexical](https://crates.io/crates/lexical) //! instead. //! //! # Getting Started //! //! ```rust //! extern crate lexical_core; //! //! // String to number using Rust slices. //! // The argument is the byte string parsed. //! let f: f32 = lexical_core::parse(b"3.5").unwrap(); // 3.5 //! let i: i32 = lexical_core::parse(b"15").unwrap(); // 15 //! //! // All lexical_core parsers are checked, they validate the //! // input data is entirely correct, and stop parsing when invalid data //! // is found, or upon numerical overflow. //! let r = lexical_core::parse::<u8>(b"256"); // Err(ErrorCode::Overflow.into()) //! let r = lexical_core::parse::<u8>(b"1a5"); // Err(ErrorCode::InvalidDigit.into()) //! //! // In order to extract and parse a number from a substring of the input //! // data, use `parse_partial`. These functions return the parsed value and //! // the number of processed digits, allowing you to extract and parse the //! // number in a single pass. //! let r = lexical_core::parse_partial::<i8>(b"3a5"); // Ok((3, 1)) //! //! // If an insufficiently long buffer is passed, the serializer will panic. //! // PANICS //! let mut buf = [b'0'; 1]; //! //let slc = lexical_core::write::<i64>(15, &mut buf); //! //! // In order to guarantee the buffer is long enough, always ensure there //! // are at least `T::FORMATTED_SIZE` bytes, which requires the //! // `lexical_core::Number` trait to be in scope. //! use lexical_core::Number; //! let mut buf = [b'0'; f64::FORMATTED_SIZE]; //! let slc = lexical_core::write::<f64>(15.1, &mut buf); //! assert_eq!(slc, b"15.1"); //! //! // When the `radix` feature is enabled, for decimal floats, using //! // `T::FORMATTED_SIZE` may significantly overestimate the space //! // required to format the number. Therefore, the //! // `T::FORMATTED_SIZE_DECIMAL` constants allow you to get a much //! // tighter bound on the space required. //! let mut buf = [b'0'; f64::FORMATTED_SIZE_DECIMAL]; //! let slc = lexical_core::write::<f64>(15.1, &mut buf); //! assert_eq!(slc, b"15.1"); //! ``` //! //! # Conversion API //! //! **To String** //! - [`write`] #![cfg_attr(feature = "radix", doc = " - [`write_radix`]")] //! //! **From String** //! - [`parse`] #![cfg_attr(feature = "radix", doc = " - [`parse_radix`]")] #![cfg_attr(feature = "format", doc = " - [`parse_format`]")] #![cfg_attr(all(feature = "format", feature = "radix"), doc = " - [`parse_format_radix`]")] //! - [`parse_partial`] #![cfg_attr(feature = "radix", doc = " - [`parse_partial_radix`]")] #![cfg_attr(feature = "format", doc = " - [`parse_partial_format`]")] #![cfg_attr(all(feature = "format", feature = "radix"), doc = " - [`parse_partial_format_radix`]")] //! - [`parse_lossy`] #![cfg_attr(feature = "radix", doc = " - [`parse_lossy_radix`]")] #![cfg_attr(feature = "format", doc = " - [`parse_lossy_format`]")] #![cfg_attr(all(feature = "format", feature = "radix"), doc = " - [`parse_lossy_format_radix`]")] //! - [`parse_partial_lossy`] #![cfg_attr(feature = "radix", doc = " - [`parse_partial_lossy_radix`]")] #![cfg_attr(feature = "format", doc = " - [`parse_partial_lossy_format`]")] #![cfg_attr(all(feature = "format", feature = "radix"), doc = " - [`parse_partial_lossy_format_radix`]")] //! //! # Configuration Settings //! //! **Get Configuration** //! - [`get_exponent_default_char`] #![cfg_attr(feature = "radix", doc = " - [`get_exponent_backup_char`]")] #![cfg_attr(all(feature = "correct", feature = "rounding"), doc = " - [`get_float_rounding`]")] //! - [`get_nan_string`] //! - [`get_inf_string`] //! - [`get_infinity_string`] //! //! **Set Configuration** //! - [`set_exponent_default_char`] #![cfg_attr(feature = "radix", doc = " - [`set_exponent_backup_char`]")] #![cfg_attr(all(feature = "correct", feature = "rounding"), doc = " - [`set_float_rounding`]")] //! - [`set_nan_string`] //! - [`set_inf_string`] //! - [`set_infinity_string`] //! //! [`write`]: fn.write.html #![cfg_attr(feature = "radix", doc = " [`write_radix`]: fn.write_radix.html")] //! [`parse`]: fn.parse.html #![cfg_attr(feature = "radix", doc = " [`parse_radix`]: fn.parse_radix.html")] #![cfg_attr(feature = "format", doc = " [`parse_format`]: fn.parse_format.html")] #![cfg_attr(all(feature = "format", feature = "radix"), doc = " [`parse_format_radix`]: fn.parse_format_radix.html")] //! [`parse_partial`]: fn.parse_partial.html #![cfg_attr(feature = "radix", doc = " [`parse_partial_radix`]: fn.parse_partial_radix.html")] #![cfg_attr(feature = "format", doc = " [`parse_partial_format`]: fn.parse_partial_format.html")] #![cfg_attr(all(feature = "format", feature = "radix"), doc = " [`parse_partial_format_radix`]: fn.parse_partial_format_radix.html")] //! [`parse_lossy`]: fn.parse_lossy.html #![cfg_attr(feature = "radix", doc = " [`parse_lossy_radix`]: fn.parse_lossy_radix.html")] #![cfg_attr(feature = "format", doc = " [`parse_lossy_format`]: fn.parse_lossy_format.html")] #![cfg_attr(all(feature = "format", feature = "radix"), doc = " [`parse_lossy_format_radix`]: fn.parse_lossy_format_radix.html")] //! [`parse_partial_lossy`]: fn.parse_partial_lossy.html #![cfg_attr(feature = "radix", doc = " [`parse_partial_lossy_radix`]: fn.parse_partial_lossy_radix.html")] #![cfg_attr(feature = "format", doc = " [`parse_partial_lossy_format`]: fn.parse_partial_lossy_format.html")] #![cfg_attr(all(feature = "format", feature = "radix"), doc = " [`parse_partial_lossy_format_radix`]: fn.parse_partial_lossy_format_radix.html")] //! //! [`get_exponent_default_char`]: fn.get_exponent_default_char.html #![cfg_attr(feature = "radix", doc = " [`get_exponent_backup_char`]: fn.get_exponent_backup_char.html")] #![cfg_attr(all(feature = "correct", feature = "rounding"), doc = " [`get_float_rounding`]: fn.get_float_rounding.html")] //! [`get_nan_string`]: fn.get_nan_string.html //! [`get_inf_string`]: fn.get_inf_string.html //! [`get_infinity_string`]: fn.get_infinity_string.html //! //! [`set_exponent_default_char`]: fn.set_exponent_default_char.html #![cfg_attr(feature = "radix", doc = " [`set_exponent_backup_char`]: fn.set_exponent_backup_char.html")] #![cfg_attr(all(feature = "correct", feature = "rounding"), doc = " [`set_float_rounding`]: fn.set_float_rounding.html")] //! [`set_nan_string`]: fn.set_nan_string.html //! [`set_inf_string`]: fn.set_inf_string.html //! [`set_infinity_string`]: fn.set_infinity_string.html // silence warnings for unused doc comments #![allow(unused_doc_comments)] // FEATURES // Require intrinsics in a no_std context. #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(all(not(feature = "std"), not(feature = "libm")), feature(core_intrinsics))] // DEPENDENCIES #[macro_use] extern crate cfg_if; // Use vec if there is a system allocator, which we require only if // we're using the correct and radix features. #[cfg(all(not(feature = "std"), feature = "correct", feature = "radix"))] #[cfg_attr(test, macro_use)] extern crate alloc; // Use arrayvec for atof. #[cfg(feature = "correct")] extern crate arrayvec; // Ensure only one back-end is enabled. #[cfg(all(feature = "grisu3", feature = "ryu"))] compile_error!("Lexical only accepts one of the following backends: `grisu3` or `ryu`."); // Import the back-end, if applicable. cfg_if! { if #[cfg(feature = "grisu3")] { extern crate dtoa; } else if #[cfg(feature = "ryu")] { extern crate ryu; } } // cfg_if /// Facade around the core features for name mangling. pub(crate) mod lib { #[cfg(feature = "std")] pub(crate) use std::*; #[cfg(not(feature = "std"))] pub(crate) use core::*; cfg_if! { if #[cfg(all(feature = "correct", feature = "radix"))] { #[cfg(feature = "std")] pub(crate) use std::vec::Vec; #[cfg(not(feature = "std"))] pub(crate) use ::alloc::vec::Vec; } } // cfg_if } // lib // API // Hide implementation details #[macro_use] mod util; mod atof; mod atoi; mod float; mod ftoa; mod itoa; // Re-export configuration and utilities globally. pub use util::*; /// Write number to string. /// /// Returns a subslice of the input buffer containing the written bytes, /// starting from the same address in memory as the input slice. /// /// * `value` - Number to serialize. /// * `bytes` - Slice containing a numeric string. /// /// # Panics /// /// Panics if the buffer may not be large enough to hold the serialized /// number. In order to ensure the function will not panic, provide a /// buffer with at least `{integer}::FORMATTED_SIZE_DECIMAL` elements. /// /// # Example /// /// ``` /// // import `Number` trait to get the `FORMATTED_SIZE_DECIMAL` of the number. /// use lexical_core::Number; /// /// let mut buffer = [0u8; f32::FORMATTED_SIZE_DECIMAL]; /// let float = 3.14159265359_f32; /// /// lexical_core::write(float, &mut buffer); /// /// assert_eq!(&buffer[0..9], b"3.1415927"); /// ``` /// /// This will panic, because the buffer is not large enough: /// /// ```should_panic /// // note: the buffer is only one byte large /// let mut buffer = [0u8; 1]; /// let float = 3.14159265359_f32; /// /// lexical_core::write(float, &mut buffer); /// ``` #[inline] pub fn write<'a, N: ToLexical>(n: N, bytes: &'a mut [u8]) -> &'a mut [u8] { n.to_lexical(bytes) } /// Write number to string with a custom radix. /// /// Returns a subslice of the input buffer containing the written bytes, /// starting from the same address in memory as the input slice. /// /// * `value` - Number to serialize. /// * `radix` - Radix for number encoding. /// * `bytes` - Slice containing a numeric string. /// /// # Panics /// /// Panics if the radix is not in the range `[2, 36]`. /// /// Also panics if the buffer may not be large enough to hold the /// serialized number. In order to ensure the function will not panic, /// provide a buffer with at least [`FORMATTED_SIZE`] elements. /// /// [`FORMATTED_SIZE`]: trait.Number.html#associatedconstant.FORMATTED_SIZE #[inline] #[cfg(feature = "radix")] pub fn write_radix<'a, N: ToLexical>(n: N, radix: u8, bytes: &'a mut [u8]) -> &'a mut [u8] { n.to_lexical_radix(radix, bytes) } /// Parse number from string. /// /// This method parses the entire string, returning an error if /// any invalid digits are found during parsing. /// /// * `bytes` - Byte slice containing a numeric string. #[inline] pub fn parse<N: FromLexical>(bytes: &[u8]) -> Result<N> { N::from_lexical(bytes) } /// Parse number from string. /// /// This method parses until an invalid digit is found (or the end /// of the string), returning the number of processed digits /// and the parsed value until that point. /// /// * `bytes` - Byte slice containing a numeric string. #[inline] pub fn parse_partial<N: FromLexical>(bytes: &[u8]) -> Result<(N, usize)> { N::from_lexical_partial(bytes) } /// Lossily parse number from string. /// /// This method parses the entire string, returning an error if /// any invalid digits are found during parsing. This parser is /// lossy, so numerical rounding may occur during parsing. /// /// * `bytes` - Byte slice containing a numeric string. #[inline] pub fn parse_lossy<N: FromLexicalLossy>(bytes: &[u8]) -> Result<N> { N::from_lexical_lossy(bytes) } /// Lossily parse number from string. /// /// This method parses until an invalid digit is found (or the end /// of the string), returning the number of processed digits /// and the parsed value until that point. This parser is /// lossy, so numerical rounding may occur during parsing. /// /// * `bytes` - Byte slice containing a numeric string. #[inline] pub fn parse_partial_lossy<N: FromLexicalLossy>(bytes: &[u8]) -> Result<(N, usize)> { N::from_lexical_partial_lossy(bytes) } /// Parse number from string with a custom radix. /// /// This method parses the entire string, returning an error if /// any invalid digits are found during parsing. /// /// * `radix` - Radix for number decoding. /// * `bytes` - Byte slice containing a numeric string. /// /// # Panics /// /// Panics if the radix is not in the range `[2, 36]`. #[inline] #[cfg(feature = "radix")] pub fn parse_radix<N: FromLexical>(bytes: &[u8], radix: u8) -> Result<N> { N::from_lexical_radix(bytes, radix) } /// Parse number from string with a custom radix. /// /// This method parses until an invalid digit is found (or the end /// of the string), returning the number of processed digits /// and the parsed value until that point. /// /// * `radix` - Radix for number decoding. /// * `bytes` - Byte slice containing a numeric string. /// /// # Panics /// /// Panics if the radix is not in the range `[2, 36]`. #[inline] #[cfg(feature = "radix")] pub fn parse_partial_radix<N: FromLexical>(bytes: &[u8], radix: u8) -> Result<(N, usize)> { N::from_lexical_partial_radix(bytes, radix) } /// Lossily parse number from string with a custom radix. /// /// This method parses the entire string, returning an error if /// any invalid digits are found during parsing. This parser is /// lossy, so numerical rounding may occur during parsing. /// /// * `radix` - Radix for number decoding. /// * `bytes` - Byte slice containing a numeric string. /// /// # Panics /// /// Panics if the radix is not in the range `[2, 36]`. #[inline] #[cfg(feature = "radix")] pub fn parse_lossy_radix<N: FromLexicalLossy>(bytes: &[u8], radix: u8) -> Result<N> { N::from_lexical_lossy_radix(bytes, radix) } /// Lossily parse number from string with a custom radix. /// /// This method parses until an invalid digit is found (or the end /// of the string), returning the number of processed digits /// and the parsed value until that point. This parser is /// lossy, so numerical rounding may occur during parsing. /// /// * `bytes` - Byte slice containing a numeric string. /// * `radix` - Radix for number decoding. /// /// # Panics /// /// Panics if the radix is not in the range `[2, 36]`. #[inline] #[cfg(feature = "radix")] pub fn parse_partial_lossy_radix<N: FromLexicalLossy>(bytes: &[u8], radix: u8) -> Result<(N, usize)> { N::from_lexical_partial_lossy_radix(bytes, radix) } /// Parse number from string with a custom numerical format. /// /// This method parses the entire string, returning an error if /// any invalid digits are found during parsing. The numerical format /// is specified by the format bitflags, which customize the required /// components, digit separators, and other parameters of the number. /// /// * `bytes` - Byte slice containing a numeric string. /// * `format` - Numerical format. #[inline] #[cfg(feature = "format")] pub fn parse_format<N: FromLexicalFormat>(bytes: &[u8], format: NumberFormat) -> Result<N> { N::from_lexical_format(bytes, format) } /// Parse number from string with a custom numerical format. /// /// This method parses until an invalid digit is found (or the end /// of the string), returning the number of processed digits /// and the parsed value until that point. The numerical format /// is specified by the format bitflags, which customize the required /// components, digit separators, and other parameters of the number. /// /// * `bytes` - Byte slice containing a numeric string. /// * `format` - Numerical format. #[inline] #[cfg(feature = "format")] pub fn parse_partial_format<N: FromLexicalFormat>(bytes: &[u8], format: NumberFormat) -> Result<(N, usize)> { N::from_lexical_partial_format(bytes, format) } /// Lossily parse number from string with a custom numerical format. /// /// This method parses the entire string, returning an error if /// any invalid digits are found during parsing. This parser is /// lossy, so numerical rounding may occur during parsing. The /// numerical format is specified by the format bitflags, which /// customize the required components, digit separators, and other /// parameters of the number. /// /// * `bytes` - Byte slice containing a numeric string. /// * `format` - Numerical format. #[inline] #[cfg(feature = "format")] pub fn parse_lossy_format<N: FromLexicalLossyFormat>(bytes: &[u8], format: NumberFormat) -> Result<N> { N::from_lexical_lossy_format(bytes, format) } /// Lossily parse number from string with a custom numerical format. /// /// This method parses until an invalid digit is found (or the end /// of the string), returning the number of processed digits /// and the parsed value until that point. This parser is /// lossy, so numerical rounding may occur during parsing. The /// numerical format is specified by the format bitflags, which /// customize the required components, digit separators, and other /// parameters of the number. /// /// * `bytes` - Byte slice containing a numeric string. /// * `format` - Numerical format. #[inline] #[cfg(feature = "format")] pub fn parse_partial_lossy_format<N: FromLexicalLossyFormat>(bytes: &[u8], format: NumberFormat) -> Result<(N, usize)> { N::from_lexical_partial_lossy_format(bytes, format) } /// Parse number from string with a custom radix and numerical format. /// /// This method parses the entire string, returning an error if /// any invalid digits are found during parsing. The numerical format /// is specified by the format bitflags, which customize the required /// components, digit separators, and other parameters of the number. /// /// * `bytes` - Byte slice containing a numeric string. /// * `radix` - Radix for number decoding. /// * `format` - Numerical format. /// /// # Panics /// /// Panics if the radix is not in the range `[2, 36]`. #[inline] #[cfg(all(feature = "radix", feature = "format"))] pub fn parse_format_radix<N: FromLexicalFormat>(bytes: &[u8], radix: u8, format: NumberFormat) -> Result<N> { N::from_lexical_format_radix(bytes, radix, format) } /// Parse number from string with a custom radix and numerical format. /// /// This method parses until an invalid digit is found (or the end /// of the string), returning the number of processed digits /// and the parsed value until that point. The numerical format /// is specified by the format bitflags, which customize the required /// components, digit separators, and other parameters of the number. /// /// * `bytes` - Byte slice containing a numeric string. /// * `radix` - Radix for number decoding. /// * `format` - Numerical format. /// /// # Panics /// /// Panics if the radix is not in the range `[2, 36]`. #[inline] #[cfg(all(feature = "radix", feature = "format"))] pub fn parse_partial_format_radix<N: FromLexicalFormat>(bytes: &[u8], radix: u8, format: NumberFormat) -> Result<(N, usize)> { N::from_lexical_partial_format_radix(bytes, radix, format) } /// Lossily parse number from string with a custom radix and numerical format. /// /// This method parses the entire string, returning an error if /// any invalid digits are found during parsing. This parser is /// lossy, so numerical rounding may occur during parsing. The /// numerical format is specified by the format bitflags, which /// customize the required components, digit separators, and other /// parameters of the number. /// /// * `bytes` - Byte slice containing a numeric string. /// * `radix` - Radix for number decoding. /// * `format` - Numerical format. /// /// # Panics /// /// Panics if the radix is not in the range `[2, 36]`. #[inline] #[cfg(all(feature = "radix", feature = "format"))] pub fn parse_lossy_format_radix<N: FromLexicalLossyFormat>(bytes: &[u8], radix: u8, format: NumberFormat) -> Result<N> { N::from_lexical_lossy_format_radix(bytes, radix, format) } /// Lossily parse number from string with a custom radix and numerical format. /// /// This method parses until an invalid digit is found (or the end /// of the string), returning the number of processed digits /// and the parsed value until that point. This parser is /// lossy, so numerical rounding may occur during parsing. The /// numerical format is specified by the format bitflags, which /// customize the required components, digit separators, and other /// parameters of the number. /// /// * `bytes` - Byte slice containing a numeric string. /// * `radix` - Radix for number decoding. /// * `format` - Numerical format. /// /// # Panics /// /// Panics if the radix is not in the range `[2, 36]`. #[inline] #[cfg(all(feature = "radix", feature = "format"))] pub fn parse_partial_lossy_format_radix<N: FromLexicalLossyFormat>(bytes: &[u8], radix: u8, format: NumberFormat) -> Result<(N, usize)> { N::from_lexical_partial_lossy_format_radix(bytes, radix, format) }