| 1 | //! Character specific parsers and combinators |
| 2 | //! |
| 3 | //! Functions recognizing specific characters |
| 4 | |
| 5 | #[cfg (test)] |
| 6 | mod tests; |
| 7 | |
| 8 | pub mod complete; |
| 9 | pub mod streaming; |
| 10 | |
| 11 | /// Tests if byte is ASCII alphabetic: A-Z, a-z |
| 12 | /// |
| 13 | /// # Example |
| 14 | /// |
| 15 | /// ``` |
| 16 | /// # use nom::character::is_alphabetic; |
| 17 | /// assert_eq!(is_alphabetic(b'9' ), false); |
| 18 | /// assert_eq!(is_alphabetic(b'a' ), true); |
| 19 | /// ``` |
| 20 | #[inline ] |
| 21 | pub fn is_alphabetic(chr: u8) -> bool { |
| 22 | (chr >= 0x41 && chr <= 0x5A) || (chr >= 0x61 && chr <= 0x7A) |
| 23 | } |
| 24 | |
| 25 | /// Tests if byte is ASCII digit: 0-9 |
| 26 | /// |
| 27 | /// # Example |
| 28 | /// |
| 29 | /// ``` |
| 30 | /// # use nom::character::is_digit; |
| 31 | /// assert_eq!(is_digit(b'a' ), false); |
| 32 | /// assert_eq!(is_digit(b'9' ), true); |
| 33 | /// ``` |
| 34 | #[inline ] |
| 35 | pub fn is_digit(chr: u8) -> bool { |
| 36 | chr >= 0x30 && chr <= 0x39 |
| 37 | } |
| 38 | |
| 39 | /// Tests if byte is ASCII hex digit: 0-9, A-F, a-f |
| 40 | /// |
| 41 | /// # Example |
| 42 | /// |
| 43 | /// ``` |
| 44 | /// # use nom::character::is_hex_digit; |
| 45 | /// assert_eq!(is_hex_digit(b'a' ), true); |
| 46 | /// assert_eq!(is_hex_digit(b'9' ), true); |
| 47 | /// assert_eq!(is_hex_digit(b'A' ), true); |
| 48 | /// assert_eq!(is_hex_digit(b'x' ), false); |
| 49 | /// ``` |
| 50 | #[inline ] |
| 51 | pub fn is_hex_digit(chr: u8) -> bool { |
| 52 | (chr >= 0x30 && chr <= 0x39) || (chr >= 0x41 && chr <= 0x46) || (chr >= 0x61 && chr <= 0x66) |
| 53 | } |
| 54 | |
| 55 | /// Tests if byte is ASCII octal digit: 0-7 |
| 56 | /// |
| 57 | /// # Example |
| 58 | /// |
| 59 | /// ``` |
| 60 | /// # use nom::character::is_oct_digit; |
| 61 | /// assert_eq!(is_oct_digit(b'a' ), false); |
| 62 | /// assert_eq!(is_oct_digit(b'9' ), false); |
| 63 | /// assert_eq!(is_oct_digit(b'6' ), true); |
| 64 | /// ``` |
| 65 | #[inline ] |
| 66 | pub fn is_oct_digit(chr: u8) -> bool { |
| 67 | chr >= 0x30 && chr <= 0x37 |
| 68 | } |
| 69 | |
| 70 | /// Tests if byte is ASCII alphanumeric: A-Z, a-z, 0-9 |
| 71 | /// |
| 72 | /// # Example |
| 73 | /// |
| 74 | /// ``` |
| 75 | /// # use nom::character::is_alphanumeric; |
| 76 | /// assert_eq!(is_alphanumeric(b'-' ), false); |
| 77 | /// assert_eq!(is_alphanumeric(b'a' ), true); |
| 78 | /// assert_eq!(is_alphanumeric(b'9' ), true); |
| 79 | /// assert_eq!(is_alphanumeric(b'A' ), true); |
| 80 | /// ``` |
| 81 | #[inline ] |
| 82 | pub fn is_alphanumeric(chr: u8) -> bool { |
| 83 | is_alphabetic(chr) || is_digit(chr) |
| 84 | } |
| 85 | |
| 86 | /// Tests if byte is ASCII space or tab |
| 87 | /// |
| 88 | /// # Example |
| 89 | /// |
| 90 | /// ``` |
| 91 | /// # use nom::character::is_space; |
| 92 | /// assert_eq!(is_space(b' \n' ), false); |
| 93 | /// assert_eq!(is_space(b' \r' ), false); |
| 94 | /// assert_eq!(is_space(b' ' ), true); |
| 95 | /// assert_eq!(is_space(b' \t' ), true); |
| 96 | /// ``` |
| 97 | #[inline ] |
| 98 | pub fn is_space(chr: u8) -> bool { |
| 99 | chr == b' ' || chr == b' \t' |
| 100 | } |
| 101 | |
| 102 | /// Tests if byte is ASCII newline: \n |
| 103 | /// |
| 104 | /// # Example |
| 105 | /// |
| 106 | /// ``` |
| 107 | /// # use nom::character::is_newline; |
| 108 | /// assert_eq!(is_newline(b' \n' ), true); |
| 109 | /// assert_eq!(is_newline(b' \r' ), false); |
| 110 | /// assert_eq!(is_newline(b' ' ), false); |
| 111 | /// assert_eq!(is_newline(b' \t' ), false); |
| 112 | /// ``` |
| 113 | #[inline ] |
| 114 | pub fn is_newline(chr: u8) -> bool { |
| 115 | chr == b' \n' |
| 116 | } |
| 117 | |