neure是一個簡單小巧的字符串解析庫, 我在開發(fā)aopt時為了優(yōu)化編譯時間而開發(fā)的替代regex的庫. 目前代碼架構非常簡單, 性能上比regex更快, 和nom的速度不相上下. 設計上參考了regex.Readme有一個和regex比較的代碼,可以嘗試一下.
https://github.com/araraloren/neure
補充一個和nom例子比較的代碼,開啟lto=fat時性能不相上下
use neure::*;
use nom::{
    bytes::{tag, take_while_m_n},
    combinator::map_res,
    sequence::tuple,
    IResult,
};
#[derive(Debug, PartialEq)]
pub struct Color {
    pub red: u8,
    pub green: u8,
    pub blue: u8,
}
fn from_hex(input: &str) -> Result {
    u8::from_str_radix(input, 16)
}
fn is_hex_digit(c: char) -> bool {
    c.is_digit(16)
}
fn hex_primary(input: &str) -> IResult<&str, u8> {
    map_res(take_while_m_n(2, 2, is_hex_digit), from_hex)(input)
}
fn hex_color(input: &str) -> IResult<&str, Color> {
    let (input, _) = tag("#")(input)?;
    let (input, (red, green, blue)) = tuple((hex_primary, hex_primary, hex_primary))(input)?;
    Ok((input, Color { red, green, blue }))
}
fn main() -> Result<(), Box> {
    let mut storer = SpanStorer::new(1);
    let color_str = "#2F14DF";
    let parser = |storer: &mut SpanStorer, str: &str| -> Result<(), neure::Error> {
        let pound = neure!('#');
        let hex = neure!(['0' - '9' 'A' - 'F']{2});
        let mut ctx = CharsCtx::default().with_str(str);
        ctx.reset();
        ctx.try_mat(£)?;
        ctx.try_cap(0, storer, &hex)?;
        ctx.try_cap(0, storer, &hex)?;
        ctx.try_cap(0, storer, &hex)?;
        Ok(())
    };
    measure(100000, 100000, || {
        if parser(storer.reset(), color_str).is_ok() {
            let mut strs = storer.substrs(color_str, 0).unwrap();
            assert_eq!(
                Color {
                    red: u8::from_str_radix(strs.next().unwrap(), 16).unwrap(),
                    green: u8::from_str_radix(strs.next().unwrap(), 16).unwrap(),
                    blue: u8::from_str_radix(strs.next().unwrap(), 16).unwrap(),
                },
                Color {
                    red: 47,
                    green: 20,
                    blue: 223,
                }
            );
            1
        } else {
            0
        }
    });
    measure(100000, 100000, || {
        if hex_color("#2F14DF").unwrap()
            == (
                "",
                Color {
                    red: 47,
                    green: 20,
                    blue: 223,
                },
            )
        {
            1
        } else {
            0
        }
    });
    Ok(())
}
pub fn measure(n: usize, size: usize, mut f: impl FnMut() -> i32) {
    use std::Instant;
    let start = Instant::now();
    let mut sum = 0;
    for _ in 0..n {
        sum += f();
    }
    let time = start.elapsed();
    println!(
        "Size = {size}, Cost time {} with test {} times: {} --> {}",
        time.as_secs_f64(),
        n,
        time.as_secs_f64() / n as f64,
        sum,
    );
}  
審核編輯:湯梓紅
- 
                                字符串
                                +關注
關注
1文章
594瀏覽量
22983 - 
                                代碼
                                +關注
關注
30文章
4930瀏覽量
72801 - 
                                編譯
                                +關注
關注
0文章
682瀏覽量
34885 - 
                                GitHub
                                +關注
關注
3文章
484瀏覽量
18361 
原文標題:【大家的項目】nuere - 簡單小巧快速的字符串解析庫
文章出處:【微信號:Rust語言中文社區(qū),微信公眾號:Rust語言中文社區(qū)】歡迎添加關注!文章轉載請注明出處。
發(fā)布評論請先 登錄
字符串移位包含的問題解決方案
什么是復制字符串?Python如何復制字符串
strtok拆分字符串
    
          
        
        
nuere-簡單小巧快速的字符串解析庫
                
 
    
           
            
            
                
            
評論