4 个版本

0.2.3 2023年4月15日
0.2.2 2021年4月16日
0.2.1 2021年4月16日
0.2.0 2021年3月8日

77解析工具

Download history 137/week @ 2024-03-13 31/week @ 2024-03-20 52/week @ 2024-03-27 141/week @ 2024-04-03 202/week @ 2024-04-10 232/week @ 2024-04-17 198/week @ 2024-04-24 11/week @ 2024-05-01 11/week @ 2024-05-08 70/week @ 2024-05-15 76/week @ 2024-05-22 9/week @ 2024-05-29 17/week @ 2024-06-05 93/week @ 2024-06-12 11/week @ 2024-06-19 15/week @ 2024-06-26

137 每月下载量
3 个 crate 中使用 (2 直接)

MIT/Apache

43KB
1K SLoC

CI crates.io docs.rs

字节解析器

一个提供以函数方式轻松解析字符串或切片的库。

基本示例

use byte_parser::{StrParser, ParseIterator};

let mut parser = StrParser::new("\
    key: value\n\
    other key: more : value\n\
    also valid\
");

let lines: Vec<(&str, &str)> = parser
    .split_on_byte(b'\n')
    .map_and_collect(|line| {

        let key = line
            .record()
            .consume_while_byte_fn(|&b| b != b':')
            .to_str();

        let has_colon = line.advance().is_some();
        if !has_colon {
            return ("", key.trim_start());
        }

        let value = line
            .record()
            .consume_to_str();

        (key, value.trim_start())
    });

assert_eq!(lines[0], ("key", "value"));
assert_eq!(lines[1], ("other key", "more : value"));
assert_eq!(lines[2], ("", "also valid"));

示例:解析数字

# use std::str::FromStr;
use byte_parser::{StrParser, ParseIterator};

#[derive(Debug, PartialEq)]
pub enum Number {
    Uint(usize),
    Integer(isize),
    Float(f32)
}

impl Number {
    /// # Panics
    /// Panics if invalid utf8 is found.
    /// Or if the digit is to large.
    pub fn from_parser<'s, I>(iter: &mut I) -> Option<Self>
    where I: ParseIterator<'s> {
        let mut iter = iter.record();

        // there could be a leading minus -
        let is_negative = iter
            .next_if(|&b| b == b'-')
            .is_some();

            // consume first digits
        iter
            .while_byte_fn(u8::is_ascii_digit)
            .consume_at_least(1)
                .ok()?;
            
        // there could be a dot
        let has_dot = iter
            .next_if(|&b| b == b'.')
            .is_some();

        if !has_dot {
            let s = iter.to_str();
            let num = match is_negative {
                true => Self::Integer(
                    s.parse().expect("digit to large")
                ),
                false => Self::Uint(
                    s.parse().expect("digit to large")
                )
            };

            return Some(num)
            }

            // consume next digits
        iter.consume_while_byte_fn(u8::is_ascii_digit);

        Some(Self::Float(
            iter.to_str().parse().expect("digit to large")
        ))
    }
}

impl FromStr for Number {
    type Err = ();
    fn from_str(s: &str) -> Result<Self, ()> {
        let mut parser = StrParser::new(s);
            let num = Self::from_parser(&mut parser)
            .ok_or(())?;

        // parser not exhausted
        if parser.advance().is_some() {
            return Err(())
        }

        Ok(num)
    }
}

assert_eq!(Number::Float(1.23), "1.23".parse().unwrap());
assert_eq!(Number::Float(-32.1), "-32.1".parse().unwrap());
assert_eq!(Number::Uint(42), "42".parse().unwrap());
assert_eq!(Number::Integer(-42), "-42".parse().unwrap());
assert!(".42".parse::<Number>().is_err());
assert!("5.42 ".parse::<Number>().is_err());

无运行时依赖

功能