Skip to content

Commit

Permalink
updated dependencies and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pacman82 committed Mar 5, 2018
1 parent acace2f commit e2e18bf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "atoi"
version = "0.2.2"
version = "0.2.3"
authors = ["Markus Klein <[email protected]>"]
license = "MIT"
repository = "https://github.com/pacman82/atoi-rs"
Expand All @@ -21,4 +21,7 @@ keywords = ["atoi", "conversion", "integer"]
categories = ["parsing"]

[dependencies]
num-traits="0.1.39"
num-traits="0.2.1"

[dev-dependencies]
checked = "0.5.0"
33 changes: 27 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! (e.g. for performance reasons).

extern crate num_traits;
use num_traits::{Zero, One, Signed};
use num_traits::{One, Signed, Zero};
use std::ops::{AddAssign, MulAssign};

/// Types implementing this trait can be parsed from a positional numeral system with radix 10
Expand Down Expand Up @@ -60,12 +60,16 @@ pub trait FromRadix10: Sized {
/// assert_eq!(None, atoi::<i32>(b"-42"));
/// // Leading zeros are allowed
/// assert_eq!(Some(42), atoi::<u32>(b"0042"));
/// // By default this will panic in debug or overflow in release builds.
/// // assert_eq!(Some(0), atoi::<u8>(b"256")).
/// // use the e.g. the `checked` crate to handle overflows gracefully
/// ```
///
/// # Return
/// Returns a a number if the slice started with a number, otherwise `None` is returned.
pub fn atoi<I>(text: &[u8]) -> Option<I>
where I: FromRadix10
where
I: FromRadix10,
{
match I::from_radix_10(text) {
(_, 0) => None,
Expand All @@ -83,7 +87,8 @@ pub fn atoi<I>(text: &[u8]) -> Option<I>
/// assert_eq!(None, ascii_to_digit::<u32>(b'x'));
/// ```
pub fn ascii_to_digit<I>(character: u8) -> Option<I>
where I: Zero + One
where
I: Zero + One,
{
match character {
b'0' => Some(nth(0)),
Expand All @@ -101,7 +106,8 @@ pub fn ascii_to_digit<I>(character: u8) -> Option<I>
}

impl<I> FromRadix10 for I
where I: Zero + One + AddAssign + MulAssign
where
I: Zero + One + AddAssign + MulAssign,
{
fn from_radix_10(text: &[u8]) -> (Self, usize) {
let mut index = 0;
Expand Down Expand Up @@ -147,7 +153,8 @@ impl Sign {

/// Returns either `+1` or `-1`
pub fn signum<I>(self) -> I
where I: Signed
where
I: Signed,
{
match self {
Sign::Plus => I::one(),
Expand All @@ -159,11 +166,25 @@ impl Sign {
// At least for primitive types this function does not incur runtime costs, since it is only called
// with constants
fn nth<I>(n: u8) -> I
where I: Zero + One
where
I: Zero + One,
{
let mut i = I::zero();
for _ in 0..n {
i = i + I::one();
}
i
}

#[cfg(test)]
mod test {

extern crate checked;
use self::checked::Checked;
use super::*;

#[test]
fn overflow_detection() {
assert_eq!(Some(Checked(None)), atoi::<Checked<u8>>(b"256"));
}
}

0 comments on commit e2e18bf

Please sign in to comment.