btoi/lib.rs
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 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
// Copyright 2017-2023 Niklas Fiekas <niklas.fiekas@backscattering.de>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Parse integers from ASCII byte slices.
//!
//! Provides functions similar to [`from_str_radix`], but is faster when
//! parsing directly from byte slices instead of strings.
//!
//! Supports `#![no_std]`.
//!
//! # Examples
//!
//! ```
//! use btoi::btoi;
//!
//! assert_eq!(Ok(42), btoi(b"42"));
//! assert_eq!(Ok(-1000), btoi(b"-1000"));
//! ```
//!
//! All functions are [generic](https://docs.rs/num-traits/) over integer
//! types. Use the turbofish syntax if a type cannot be inferred from the
//! context.
//!
//! ```
//! # use btoi::btoi;
//! // overflows the selected target type
//! assert!(btoi::<u32>(b"9876543210").is_err());
//!
//! // underflows the selected target type (an unsigned integer)
//! assert!(btoi::<u32>(b"-1").is_err());
//! ```
//!
//! It is possible to use saturating arithmetic for overflow handling.
//!
//! ```
//! use btoi::btoi_saturating;
//!
//! assert_eq!(Ok(0xffff_ffff), btoi_saturating::<u32>(b"9876543210"));
//! assert_eq!(Ok(0), btoi_saturating::<u32>(b"-1"));
//! ```
//!
//! # Errors
//!
//! All functions return [`ParseIntegerError`] for these error conditions:
//!
//! * The byte slice does not contain any digits.
//! * Not all characters are `0-9`, `a-z`, `A-Z`. Leading or trailing
//! whitespace is not allowed. The `btoi*` functions accept an optional
//! leading `+` or `-` sign. The `btou*` functions respectively do not
//! allow signs.
//! * Not all digits are valid in the given radix.
//! * The number overflows or underflows the target type, but saturating
//! arithmetic is not used.
//!
//! # Panics
//!
//! Just like `from_str_radix` functions will panic if the given radix is
//! not in the range `2..=36` (or in the pathological case that there is
//! no representation of the radix in the target integer type).
//!
//! [`ParseIntegerError`]: struct.ParseIntegerError.html
//! [`from_str_radix`]: https://doc.rust-lang.org/std/primitive.u32.html#method.from_str_radix
#![doc(html_root_url = "https://docs.rs/btoi/0.4.3")]
#![forbid(unsafe_code)]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![cfg_attr(not(feature = "std"), no_std)]
extern crate num_traits;
#[cfg(test)]
#[macro_use]
extern crate quickcheck;
#[cfg(feature = "std")]
extern crate std as core;
use core::fmt;
#[cfg(feature = "std")]
use std::error::Error;
use num_traits::{Bounded, CheckedAdd, CheckedMul, CheckedSub, FromPrimitive, Saturating, Zero};
/// An error that can occur when parsing an integer.
///
/// * No digits
/// * Invalid digit
/// * Overflow
/// * Underflow
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseIntegerError {
kind: ErrorKind,
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum ErrorKind {
Empty,
InvalidDigit,
Overflow,
Underflow,
}
impl ParseIntegerError {
fn desc(&self) -> &str {
match self.kind {
ErrorKind::Empty => "cannot parse integer without digits",
ErrorKind::InvalidDigit => "invalid digit found in slice",
ErrorKind::Overflow => "number too large to fit in target type",
ErrorKind::Underflow => "number too small to fit in target type",
}
}
}
impl fmt::Display for ParseIntegerError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.desc().fmt(f)
}
}
#[cfg(feature = "std")]
impl Error for ParseIntegerError {
fn description(&self) -> &str {
self.desc()
}
}
/// Converts a byte slice in a given base to an integer. Signs are not allowed.
///
/// # Errors
///
/// Returns [`ParseIntegerError`] for any of the following conditions:
///
/// * `bytes` is empty
/// * not all characters of `bytes` are `0-9`, `a-z` or `A-Z`
/// * not all characters refer to digits in the given `radix`
/// * the number overflows `I`
///
/// # Panics
///
/// Panics if `radix` is not in the range `2..=36` (or in the pathological
/// case that there is no representation of `radix` in `I`).
///
/// # Examples
///
/// ```
/// # use btoi::btou_radix;
/// assert_eq!(Ok(255), btou_radix(b"ff", 16));
/// assert_eq!(Ok(42), btou_radix(b"101010", 2));
/// ```
///
/// [`ParseIntegerError`]: struct.ParseIntegerError.html
#[track_caller]
pub fn btou_radix<I>(bytes: &[u8], radix: u32) -> Result<I, ParseIntegerError>
where
I: FromPrimitive + Zero + CheckedAdd + CheckedMul,
{
assert!(
(2..=36).contains(&radix),
"radix must lie in the range 2..=36, found {}",
radix
);
let base = I::from_u32(radix).expect("radix can be represented as integer");
if bytes.is_empty() {
return Err(ParseIntegerError {
kind: ErrorKind::Empty,
});
}
let mut result = I::zero();
for &digit in bytes {
let x = match char::from(digit).to_digit(radix).and_then(I::from_u32) {
Some(x) => x,
None => {
return Err(ParseIntegerError {
kind: ErrorKind::InvalidDigit,
})
}
};
result = match result.checked_mul(&base) {
Some(result) => result,
None => {
return Err(ParseIntegerError {
kind: ErrorKind::Overflow,
})
}
};
result = match result.checked_add(&x) {
Some(result) => result,
None => {
return Err(ParseIntegerError {
kind: ErrorKind::Overflow,
})
}
};
}
Ok(result)
}
/// Converts a byte slice to an integer. Signs are not allowed.
///
/// # Errors
///
/// Returns [`ParseIntegerError`] for any of the following conditions:
///
/// * `bytes` is empty
/// * not all characters of `bytes` are `0-9`
/// * the number overflows `I`
///
/// # Panics
///
/// Panics in the pathological case that there is no representation of `10`
/// in `I`.
///
/// # Examples
///
/// ```
/// # use btoi::btou;
/// assert_eq!(Ok(12345), btou(b"12345"));
/// assert!(btou::<u8>(b"+1").is_err()); // only btoi allows signs
/// assert!(btou::<u8>(b"256").is_err()); // overflow
/// ```
///
/// [`ParseIntegerError`]: struct.ParseIntegerError.html
#[track_caller]
pub fn btou<I>(bytes: &[u8]) -> Result<I, ParseIntegerError>
where
I: FromPrimitive + Zero + CheckedAdd + CheckedMul,
{
btou_radix(bytes, 10)
}
/// Converts a byte slice in a given base to an integer.
///
/// Like [`btou_radix`], but numbers may optionally start with a sign
/// (`-` or `+`).
///
/// # Errors
///
/// Returns [`ParseIntegerError`] for any of the following conditions:
///
/// * `bytes` has no digits
/// * not all characters of `bytes` are `0-9`, `a-z`, `A-Z`, exluding an
/// optional leading sign
/// * not all characters refer to digits in the given `radix`, exluding an
/// optional leading sign
/// * the number overflows or underflows `I`
///
/// # Panics
///
/// Panics if `radix` is not in the range `2..=36` (or in the pathological
/// case that there is no representation of `radix` in `I`).
///
/// # Examples
///
/// ```
/// # use btoi::btoi_radix;
/// assert_eq!(Ok(10), btoi_radix(b"a", 16));
/// assert_eq!(Ok(10), btoi_radix(b"+a", 16));
/// assert_eq!(Ok(-42), btoi_radix(b"-101010", 2));
/// ```
///
/// [`btou_radix`]: fn.btou_radix.html
/// [`ParseIntegerError`]: struct.ParseIntegerError.html
#[track_caller]
pub fn btoi_radix<I>(bytes: &[u8], radix: u32) -> Result<I, ParseIntegerError>
where
I: FromPrimitive + Zero + CheckedAdd + CheckedSub + CheckedMul,
{
assert!(
(2..=36).contains(&radix),
"radix must lie in the range 2..=36, found {}",
radix
);
let base = I::from_u32(radix).expect("radix can be represented as integer");
if bytes.is_empty() {
return Err(ParseIntegerError {
kind: ErrorKind::Empty,
});
}
let digits = match bytes[0] {
b'+' => return btou_radix(&bytes[1..], radix),
b'-' => &bytes[1..],
_ => return btou_radix(bytes, radix),
};
if digits.is_empty() {
return Err(ParseIntegerError {
kind: ErrorKind::Empty,
});
}
let mut result = I::zero();
for &digit in digits {
let x = match char::from(digit).to_digit(radix).and_then(I::from_u32) {
Some(x) => x,
None => {
return Err(ParseIntegerError {
kind: ErrorKind::InvalidDigit,
})
}
};
result = match result.checked_mul(&base) {
Some(result) => result,
None => {
return Err(ParseIntegerError {
kind: ErrorKind::Underflow,
})
}
};
result = match result.checked_sub(&x) {
Some(result) => result,
None => {
return Err(ParseIntegerError {
kind: ErrorKind::Underflow,
})
}
};
}
Ok(result)
}
/// Converts a byte slice to an integer.
///
/// Like [`btou`], but numbers may optionally start with a sign (`-` or `+`).
///
/// # Errors
///
/// Returns [`ParseIntegerError`] for any of the following conditions:
///
/// * `bytes` has no digits
/// * not all characters of `bytes` are `0-9`, excluding an optional leading
/// sign
/// * the number overflows or underflows `I`
///
/// # Panics
///
/// Panics in the pathological case that there is no representation of `10`
/// in `I`.
///
/// # Examples
///
/// ```
/// # use btoi::btoi;
/// assert_eq!(Ok(123), btoi(b"123"));
/// assert_eq!(Ok(123), btoi(b"+123"));
/// assert_eq!(Ok(-123), btoi(b"-123"));
///
/// assert!(btoi::<i16>(b"123456789").is_err()); // overflow
/// assert!(btoi::<u32>(b"-1").is_err()); // underflow
///
/// assert!(btoi::<i32>(b" 42").is_err()); // leading space
/// ```
///
/// [`btou`]: fn.btou.html
/// [`ParseIntegerError`]: struct.ParseIntegerError.html
#[track_caller]
pub fn btoi<I>(bytes: &[u8]) -> Result<I, ParseIntegerError>
where
I: FromPrimitive + Zero + CheckedAdd + CheckedSub + CheckedMul,
{
btoi_radix(bytes, 10)
}
/// Converts a byte slice in a given base to the closest possible integer.
/// Signs are not allowed.
///
/// # Errors
///
/// Returns [`ParseIntegerError`] for any of the following conditions:
///
/// * `bytes` is empty
/// * not all characters of `bytes` are `0-9`, `a-z`, `A-Z`
/// * not all characters refer to digits in the given `radix`
///
/// # Panics
///
/// Panics if `radix` is not in the range `2..=36` (or in the pathological
/// case that there is no representation of `radix` in `I`).
///
/// # Examples
///
/// ```
/// # use btoi::btou_saturating_radix;
/// assert_eq!(Ok(255), btou_saturating_radix::<u8>(b"00ff", 16));
/// assert_eq!(Ok(255), btou_saturating_radix::<u8>(b"0100", 16)); // u8 saturated
/// assert_eq!(Ok(255), btou_saturating_radix::<u8>(b"0101", 16)); // u8 saturated
/// ```
///
/// [`ParseIntegerError`]: struct.ParseIntegerError.html
#[track_caller]
pub fn btou_saturating_radix<I>(bytes: &[u8], radix: u32) -> Result<I, ParseIntegerError>
where
I: FromPrimitive + Zero + CheckedMul + Saturating + Bounded,
{
assert!(
(2..=36).contains(&radix),
"radix must lie in the range 2..=36, found {}",
radix
);
let base = I::from_u32(radix).expect("radix can be represented as integer");
if bytes.is_empty() {
return Err(ParseIntegerError {
kind: ErrorKind::Empty,
});
}
let mut result = I::zero();
for &digit in bytes {
let x = match char::from(digit).to_digit(radix).and_then(I::from_u32) {
Some(x) => x,
None => {
return Err(ParseIntegerError {
kind: ErrorKind::InvalidDigit,
})
}
};
result = match result.checked_mul(&base) {
Some(result) => result,
None => return Ok(I::max_value()),
};
result = result.saturating_add(x);
}
Ok(result)
}
/// Converts a byte slice to the closest possible integer.
/// Signs are not allowed.
///
/// # Errors
///
/// Returns [`ParseIntegerError`] for any of the following conditions:
///
/// * `bytes` is empty
/// * not all characters of `bytes` are `0-9`
///
/// # Panics
///
/// Panics in the pathological case that there is no representation of `10`
/// in `I`.
///
/// # Examples
///
/// ```
/// # use btoi::btou_saturating;
/// assert_eq!(Ok(65535), btou_saturating::<u16>(b"65535"));
/// assert_eq!(Ok(65535), btou_saturating::<u16>(b"65536")); // u16 saturated
/// assert_eq!(Ok(65535), btou_saturating::<u16>(b"65537")); // u16 saturated
/// ```
///
/// [`ParseIntegerError`]: struct.ParseIntegerError.html
#[track_caller]
pub fn btou_saturating<I>(bytes: &[u8]) -> Result<I, ParseIntegerError>
where
I: FromPrimitive + Zero + CheckedMul + Saturating + Bounded,
{
btou_saturating_radix(bytes, 10)
}
/// Converts a byte slice in a given base to the closest possible integer.
///
/// Like [`btou_saturating_radix`], but numbers may optionally start with a
/// sign (`-` or `+`).
///
/// # Errors
///
/// Returns [`ParseIntegerError`] for any of the following conditions:
///
/// * `bytes` has no digits
/// * not all characters of `bytes` are `0-9`, `a-z`, `A-Z`, excluding an
/// optional leading sign
/// * not all characters refer to digits in the given `radix`, excluding an
/// optional leading sign
///
/// # Panics
///
/// Panics if `radix` is not in the range `2..=36` (or in the pathological
/// case that there is no representation of `radix` in `I`).
///
/// # Examples
///
/// ```
/// # use btoi::btoi_saturating_radix;
/// assert_eq!(Ok(127), btoi_saturating_radix::<i8>(b"7f", 16));
/// assert_eq!(Ok(127), btoi_saturating_radix::<i8>(b"ff", 16)); // no overflow
/// assert_eq!(Ok(-128), btoi_saturating_radix::<i8>(b"-ff", 16)); // no underflow
/// ```
///
/// [`btou_saturating_radix`]: fn.btou_saturating_radix.html
/// [`ParseIntegerError`]: struct.ParseIntegerError.html
#[track_caller]
pub fn btoi_saturating_radix<I>(bytes: &[u8], radix: u32) -> Result<I, ParseIntegerError>
where
I: FromPrimitive + Zero + CheckedMul + Saturating + Bounded,
{
assert!(
(2..=36).contains(&radix),
"radix must lie in the range 2..=36, found {}",
radix
);
let base = I::from_u32(radix).expect("radix can be represented as integer");
if bytes.is_empty() {
return Err(ParseIntegerError {
kind: ErrorKind::Empty,
});
}
let digits = match bytes[0] {
b'+' => return btou_saturating_radix(&bytes[1..], radix),
b'-' => &bytes[1..],
_ => return btou_saturating_radix(bytes, radix),
};
if digits.is_empty() {
return Err(ParseIntegerError {
kind: ErrorKind::Empty,
});
}
let mut result = I::zero();
for &digit in digits {
let x = match char::from(digit).to_digit(radix).and_then(I::from_u32) {
Some(x) => x,
None => {
return Err(ParseIntegerError {
kind: ErrorKind::InvalidDigit,
})
}
};
result = match result.checked_mul(&base) {
Some(result) => result,
None => return Ok(I::min_value()),
};
result = result.saturating_sub(x);
}
Ok(result)
}
/// Converts a byte slice to the closest possible integer.
///
/// Like [`btou_saturating`], but numbers may optionally start with a sign
/// (`-` or `+`).
///
/// # Errors
///
/// Returns [`ParseIntegerError`] for any of the following conditions:
///
/// * `bytes` has no digits
/// * not all characters of `bytes` are `0-9`, excluding an optional leading
/// sign
///
/// # Panics
///
/// Panics in the pathological case that there is no representation of `10`
/// in `I`.
///
/// # Examples
///
/// ```
/// # use btoi::btoi_saturating;
/// assert_eq!(Ok(127), btoi_saturating::<i8>(b"127"));
/// assert_eq!(Ok(127), btoi_saturating::<i8>(b"128")); // i8 saturated
/// assert_eq!(Ok(127), btoi_saturating::<i8>(b"+1024")); // i8 saturated
/// assert_eq!(Ok(-128), btoi_saturating::<i8>(b"-128"));
/// assert_eq!(Ok(-128), btoi_saturating::<i8>(b"-129")); // i8 saturated
///
/// assert_eq!(Ok(0), btoi_saturating::<u32>(b"-123")); // unsigned integer saturated
/// ```
///
/// [`btou_saturating`]: fn.btou_saturating.html
/// [`ParseIntegerError`]: struct.ParseIntegerError.html
#[track_caller]
pub fn btoi_saturating<I>(bytes: &[u8]) -> Result<I, ParseIntegerError>
where
I: FromPrimitive + Zero + CheckedMul + Saturating + Bounded,
{
btoi_saturating_radix(bytes, 10)
}
#[cfg(test)]
mod tests {
use std::str;
use super::*;
quickcheck! {
fn btou_identity(n: u32) -> bool {
Ok(n) == btou(n.to_string().as_bytes())
}
fn btou_binary_identity(n: u64) -> bool {
Ok(n) == btou_radix(format!("{:b}", n).as_bytes(), 2)
}
fn btou_octal_identity(n: u64) -> bool {
Ok(n) == btou_radix(format!("{:o}", n).as_bytes(), 8)
}
fn btou_lower_hex_identity(n: u64) -> bool {
Ok(n) == btou_radix(format!("{:x}", n).as_bytes(), 16)
}
fn btou_upper_hex_identity(n: u64) -> bool {
Ok(n) == btou_radix(format!("{:X}", n).as_bytes(), 16)
}
fn btoi_identity(n: i32) -> bool {
Ok(n) == btoi(n.to_string().as_bytes())
}
fn btou_saturating_identity(n: u32) -> bool {
Ok(n) == btou_saturating(n.to_string().as_bytes())
}
fn btoi_saturating_identity(n: i32) -> bool {
Ok(n) == btoi_saturating(n.to_string().as_bytes())
}
fn btoi_radix_std(bytes: Vec<u8>, radix: u32) -> bool {
let radix = radix % 35 + 2; // panic unless 2 <= radix <= 36
str::from_utf8(&bytes).ok().and_then(|src| i32::from_str_radix(src, radix).ok()) ==
btoi_radix(&bytes, radix).ok()
}
}
#[test]
fn test_lone_minus() {
assert!(btoi::<isize>(b"-").is_err());
assert!(btoi_radix::<isize>(b"-", 16).is_err());
assert!(btoi_saturating::<isize>(b"-").is_err());
assert!(btoi_saturating_radix::<isize>(b"-", 8).is_err());
assert!(btou::<isize>(b"-").is_err());
assert!(btou_radix::<isize>(b"-", 16).is_err());
assert!(btou_saturating::<isize>(b"-").is_err());
assert!(btou_saturating_radix::<isize>(b"-", 8).is_err());
}
}