Skip to content

Commit

Permalink
fix!: Remove character::is_* in favor of AsChar
Browse files Browse the repository at this point in the history
This removes duplication and opens the way for changing complete vs
streaming parsers.

The problem is we still have something in `number` and `bits`
  • Loading branch information
epage committed Oct 11, 2022
1 parent d0a397b commit 3897121
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 184 deletions.
4 changes: 2 additions & 2 deletions src/_tutorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
//! # use nom::bytes::complete::tag;
//! # use nom::sequence::preceded;
//! # use nom::sequence::tuple;
//! # use nom::character::is_alphabetic;
//! # use nom::AsChar;
//! struct Request<'s> {
//! method: &'s [u8],
//! url: &'s [u8],
Expand All @@ -115,7 +115,7 @@
//! // combine all previous parsers in one function
//! fn request_line(i: &[u8]) -> IResult<&[u8], Request> {
//! // first implement the basic parsers
//! let method = take_while1(is_alphabetic);
//! let method = take_while1(AsChar::is_alpha);
//! let space = take_while1(|c| c == b' ');
//! let url = take_while1(|c| c != b' ');
//! let is_version = |c| c >= b'0' && c <= b'9' || c == b'.';
Expand Down
12 changes: 6 additions & 6 deletions src/bytes/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ where
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed, IResult};
/// use nom::bytes::complete::take_while;
/// use nom::character::is_alphabetic;
/// use nom::AsChar;
///
/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
/// take_while(is_alphabetic)(s)
/// take_while(AsChar::is_alpha)(s)
/// }
///
/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
Expand Down Expand Up @@ -200,10 +200,10 @@ where
/// ```rust
/// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult};
/// use nom::bytes::complete::take_while1;
/// use nom::character::is_alphabetic;
/// use nom::AsChar;
///
/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
/// take_while1(is_alphabetic)(s)
/// take_while1(AsChar::is_alpha)(s)
/// }
///
/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
Expand Down Expand Up @@ -234,10 +234,10 @@ where
/// ```rust
/// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult};
/// use nom::bytes::complete::take_while_m_n;
/// use nom::character::is_alphabetic;
/// use nom::AsChar;
///
/// fn short_alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
/// take_while_m_n(3, 6, is_alphabetic)(s)
/// take_while_m_n(3, 6, AsChar::is_alpha)(s)
/// }
///
/// assert_eq!(short_alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
Expand Down
12 changes: 6 additions & 6 deletions src/bytes/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ where
/// ```rust
/// # use nom::{Err, error::ErrorKind, Needed, IResult};
/// use nom::bytes::streaming::take_while;
/// use nom::character::is_alphabetic;
/// use nom::AsChar;
///
/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
/// take_while(is_alphabetic)(s)
/// take_while(AsChar::is_alpha)(s)
/// }
///
/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
Expand Down Expand Up @@ -209,10 +209,10 @@ where
/// ```rust
/// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult};
/// use nom::bytes::streaming::take_while1;
/// use nom::character::is_alphabetic;
/// use nom::AsChar;
///
/// fn alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
/// take_while1(is_alphabetic)(s)
/// take_while1(AsChar::is_alpha)(s)
/// }
///
/// assert_eq!(alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
Expand Down Expand Up @@ -245,10 +245,10 @@ where
/// ```rust
/// # use nom::{Err, error::{Error, ErrorKind}, Needed, IResult};
/// use nom::bytes::streaming::take_while_m_n;
/// use nom::character::is_alphabetic;
/// use nom::AsChar;
///
/// fn short_alpha(s: &[u8]) -> IResult<&[u8], &[u8]> {
/// take_while_m_n(3, 6, is_alphabetic)(s)
/// take_while_m_n(3, 6, AsChar::is_alpha)(s)
/// }
///
/// assert_eq!(short_alpha(b"latin123"), Ok((&b"123"[..], &b"latin"[..])));
Expand Down
15 changes: 7 additions & 8 deletions src/bytes/tests.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::character::is_alphabetic;
use crate::character::streaming::{
alpha1 as alpha, alphanumeric1 as alphanumeric, digit1 as digit, hex_digit1 as hex_digit,
multispace1 as multispace, oct_digit1 as oct_digit, space1 as space,
};
use crate::error::ErrorKind;
use crate::internal::{Err, IResult, Needed};
use crate::AsChar;
#[cfg(feature = "alloc")]
use crate::{
branch::alt,
Expand Down Expand Up @@ -355,7 +355,7 @@ fn take_while() {
use crate::bytes::streaming::take_while;

fn f(i: &[u8]) -> IResult<&[u8], &[u8]> {
take_while(is_alphabetic)(i)
take_while(AsChar::is_alpha)(i)
}
let a = b"";
let b = b"abcd";
Expand All @@ -373,7 +373,7 @@ fn take_while1() {
use crate::bytes::streaming::take_while1;

fn f(i: &[u8]) -> IResult<&[u8], &[u8]> {
take_while1(is_alphabetic)(i)
take_while1(AsChar::is_alpha)(i)
}
let a = b"";
let b = b"abcd";
Expand All @@ -394,7 +394,7 @@ fn take_while_m_n() {
use crate::bytes::streaming::take_while_m_n;

fn x(i: &[u8]) -> IResult<&[u8], &[u8]> {
take_while_m_n(2, 4, is_alphabetic)(i)
take_while_m_n(2, 4, AsChar::is_alpha)(i)
}
let a = b"";
let b = b"a";
Expand All @@ -419,7 +419,7 @@ fn take_till() {
use crate::bytes::streaming::take_till;

fn f(i: &[u8]) -> IResult<&[u8], &[u8]> {
take_till(is_alphabetic)(i)
take_till(AsChar::is_alpha)(i)
}
let a = b"";
let b = b"abcd";
Expand All @@ -437,7 +437,7 @@ fn take_till1() {
use crate::bytes::streaming::take_till1;

fn f(i: &[u8]) -> IResult<&[u8], &[u8]> {
take_till1(is_alphabetic)(i)
take_till1(AsChar::is_alpha)(i)
}
let a = b"";
let b = b"abcd";
Expand Down Expand Up @@ -546,11 +546,10 @@ fn take_while_m_n_utf8_full_match() {
#[cfg(feature = "std")]
fn recognize_take_while() {
use crate::bytes::streaming::take_while;
use crate::character::is_alphanumeric;
use crate::combinator::recognize;

fn x(i: &[u8]) -> IResult<&[u8], &[u8]> {
take_while(is_alphanumeric)(i)
take_while(AsChar::is_alphanum)(i)
}
fn y(i: &[u8]) -> IResult<&[u8], &[u8]> {
recognize(x)(i)
Expand Down
52 changes: 26 additions & 26 deletions src/character/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,23 +414,23 @@ where
/// assert_eq!(parser("c1"), Err(Err::Error(Error::new("c1", ErrorKind::Digit))));
/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Digit))));
/// ```
///
///
/// ## Parsing an integer
/// You can use `digit1` in combination with [`map_res`] to parse an integer:
///
///
/// ```
/// # use nom::{Err, error::{Error, ErrorKind}, IResult, Needed};
/// # use nom::combinator::map_res;
/// # use nom::character::complete::digit1;
/// fn parser(input: &str) -> IResult<&str, u32> {
/// map_res(digit1, str::parse)(input)
/// }
///
///
/// assert_eq!(parser("416"), Ok(("", 416)));
/// assert_eq!(parser("12b"), Ok(("b", 12)));
/// assert!(parser("b").is_err());
/// ```
///
///
/// [`map_res`]: crate::combinator::map_res
pub fn digit1<T, E: ParseError<T>>(input: T) -> IResult<T, T, E>
where
Expand Down Expand Up @@ -1043,18 +1043,18 @@ mod tests {
Err(Err::Error(error_position!(i, ErrorKind::HexDigit)))
);

assert!(crate::character::is_hex_digit(b'0'));
assert!(crate::character::is_hex_digit(b'9'));
assert!(crate::character::is_hex_digit(b'a'));
assert!(crate::character::is_hex_digit(b'f'));
assert!(crate::character::is_hex_digit(b'A'));
assert!(crate::character::is_hex_digit(b'F'));
assert!(!crate::character::is_hex_digit(b'g'));
assert!(!crate::character::is_hex_digit(b'G'));
assert!(!crate::character::is_hex_digit(b'/'));
assert!(!crate::character::is_hex_digit(b':'));
assert!(!crate::character::is_hex_digit(b'@'));
assert!(!crate::character::is_hex_digit(b'\x60'));
assert!(AsChar::is_hex_digit(b'0'));
assert!(AsChar::is_hex_digit(b'9'));
assert!(AsChar::is_hex_digit(b'a'));
assert!(AsChar::is_hex_digit(b'f'));
assert!(AsChar::is_hex_digit(b'A'));
assert!(AsChar::is_hex_digit(b'F'));
assert!(!AsChar::is_hex_digit(b'g'));
assert!(!AsChar::is_hex_digit(b'G'));
assert!(!AsChar::is_hex_digit(b'/'));
assert!(!AsChar::is_hex_digit(b':'));
assert!(!AsChar::is_hex_digit(b'@'));
assert!(!AsChar::is_hex_digit(b'\x60'));
}

#[test]
Expand All @@ -1068,16 +1068,16 @@ mod tests {
Err(Err::Error(error_position!(i, ErrorKind::OctDigit)))
);

assert!(crate::character::is_oct_digit(b'0'));
assert!(crate::character::is_oct_digit(b'7'));
assert!(!crate::character::is_oct_digit(b'8'));
assert!(!crate::character::is_oct_digit(b'9'));
assert!(!crate::character::is_oct_digit(b'a'));
assert!(!crate::character::is_oct_digit(b'A'));
assert!(!crate::character::is_oct_digit(b'/'));
assert!(!crate::character::is_oct_digit(b':'));
assert!(!crate::character::is_oct_digit(b'@'));
assert!(!crate::character::is_oct_digit(b'\x60'));
assert!(AsChar::is_oct_digit(b'0'));
assert!(AsChar::is_oct_digit(b'7'));
assert!(!AsChar::is_oct_digit(b'8'));
assert!(!AsChar::is_oct_digit(b'9'));
assert!(!AsChar::is_oct_digit(b'a'));
assert!(!AsChar::is_oct_digit(b'A'));
assert!(!AsChar::is_oct_digit(b'/'));
assert!(!AsChar::is_oct_digit(b':'));
assert!(!AsChar::is_oct_digit(b'@'));
assert!(!AsChar::is_oct_digit(b'\x60'));
}

#[test]
Expand Down
107 changes: 0 additions & 107 deletions src/character/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,110 +7,3 @@ mod tests;

pub mod complete;
pub mod streaming;

/// Tests if byte is ASCII alphabetic: A-Z, a-z
///
/// # Example
///
/// ```
/// # use nom::character::is_alphabetic;
/// assert_eq!(is_alphabetic(b'9'), false);
/// assert_eq!(is_alphabetic(b'a'), true);
/// ```
#[inline]
pub fn is_alphabetic(chr: u8) -> bool {
(chr >= 0x41 && chr <= 0x5A) || (chr >= 0x61 && chr <= 0x7A)
}

/// Tests if byte is ASCII digit: 0-9
///
/// # Example
///
/// ```
/// # use nom::character::is_digit;
/// assert_eq!(is_digit(b'a'), false);
/// assert_eq!(is_digit(b'9'), true);
/// ```
#[inline]
pub fn is_digit(chr: u8) -> bool {
chr >= 0x30 && chr <= 0x39
}

/// Tests if byte is ASCII hex digit: 0-9, A-F, a-f
///
/// # Example
///
/// ```
/// # use nom::character::is_hex_digit;
/// assert_eq!(is_hex_digit(b'a'), true);
/// assert_eq!(is_hex_digit(b'9'), true);
/// assert_eq!(is_hex_digit(b'A'), true);
/// assert_eq!(is_hex_digit(b'x'), false);
/// ```
#[inline]
pub fn is_hex_digit(chr: u8) -> bool {
(chr >= 0x30 && chr <= 0x39) || (chr >= 0x41 && chr <= 0x46) || (chr >= 0x61 && chr <= 0x66)
}

/// Tests if byte is ASCII octal digit: 0-7
///
/// # Example
///
/// ```
/// # use nom::character::is_oct_digit;
/// assert_eq!(is_oct_digit(b'a'), false);
/// assert_eq!(is_oct_digit(b'9'), false);
/// assert_eq!(is_oct_digit(b'6'), true);
/// ```
#[inline]
pub fn is_oct_digit(chr: u8) -> bool {
chr >= 0x30 && chr <= 0x37
}

/// Tests if byte is ASCII alphanumeric: A-Z, a-z, 0-9
///
/// # Example
///
/// ```
/// # use nom::character::is_alphanumeric;
/// assert_eq!(is_alphanumeric(b'-'), false);
/// assert_eq!(is_alphanumeric(b'a'), true);
/// assert_eq!(is_alphanumeric(b'9'), true);
/// assert_eq!(is_alphanumeric(b'A'), true);
/// ```
#[inline]
pub fn is_alphanumeric(chr: u8) -> bool {
is_alphabetic(chr) || is_digit(chr)
}

/// Tests if byte is ASCII space or tab
///
/// # Example
///
/// ```
/// # use nom::character::is_space;
/// assert_eq!(is_space(b'\n'), false);
/// assert_eq!(is_space(b'\r'), false);
/// assert_eq!(is_space(b' '), true);
/// assert_eq!(is_space(b'\t'), true);
/// ```
#[inline]
pub fn is_space(chr: u8) -> bool {
chr == b' ' || chr == b'\t'
}

/// Tests if byte is ASCII newline: \n
///
/// # Example
///
/// ```
/// # use nom::character::is_newline;
/// assert_eq!(is_newline(b'\n'), true);
/// assert_eq!(is_newline(b'\r'), false);
/// assert_eq!(is_newline(b' '), false);
/// assert_eq!(is_newline(b'\t'), false);
/// ```
#[inline]
pub fn is_newline(chr: u8) -> bool {
chr == b'\n'
}

0 comments on commit 3897121

Please sign in to comment.