一种hyperscan API使用(1)

hyperscan:

  编译和安装:http://www.cnblogs.com/zzqcn/p/4900643.html

  简单介绍:http://www.cnblogs.com/zzqcn/p/4898620.html

使用:正则编译-》匹配

1.编译

本次使用 

hs_error_t hs_compile_ext_multi(const char * const *expressions,      // 正则表达式数组

                const unsigned *flags,             // 每个正则的flag选项,本次使用HS_FLAG_SINGLEMATCH ,可用 | 相加

                const unsigned *ids,              // 每个正则表达式的唯一编号,匹配正则后会返回这个id
                const hs_expr_ext * const *ext,        // 不知道干啥的
                unsigned elements,               // 个数

                unsigned mode,                 // 模式,按上面第二个链接介绍,本次使用HS_MODE_BLOCK
                const hs_platform_info_t *platform,      // NULL 也不知道干啥的
                hs_database_t **db,              // 编译后输出 hs_dababase_t
                hs_compile_error_t **error);          // 错误信息

hs_error_t hs_alloc_scratch(const hs_database_t *db, hs_scratch_t **scratch) ;

生成scratch临时数据,编译的时候好像没用到。根据db生成。

2.序列化和反序列化

可以把db生成文件供外部使用

API:

1)序列化:

hs_error_t hs_serialize_database(const hs_database_t *db,  //db数据

                char **bytes,          // 序列化后的数据(输出)

                size_t *serialized_length)    // 序列化后的长度(输出)

2)反序列化

hs_error_t hs_deserialize_database(const char *bytes,   //从文件中读到的序列化数据

                const size_t length,    //长度
                hs_database_t **db)   // 反序列化得到的db数据(输出)

3.匹配

生成临时数据

hs_scratch_t* g_scratch = NULL;

hs_error_t hs_err = hs_alloc_scratch(hs_db, &g_scratch); //hs_db是反序列化得到的db数据

匹配配API:

hs_error_t hs_scan(const hs_database_t *db,    //db数据

           const char *data,       //匹配母串

          unsigned length,        //长度
          unsigned flags,        // 0

           hs_scratch_t *scratch,    // 临时数据
          match_event_handler onEvent,  // 回调函数,匹配到会进入此函数

          void *userCtx)        //  传给回调函数的变量

回调函数:

typedef int (*match_event_handler)(unsigned int id,    // 匹配到的id,由编译的时候控制,用到此id。
                unsigned long long from,  // 
                unsigned long long to,   //  from和to可能是匹配到的位置信息
                unsigned int flags,     //  传入的flag吧?
                void *context);      //  参数

如果匹配到了,会得到正则的id,后边在进行操作。

 

转载于:https://www.cnblogs.com/yaoyuanfeixing/p/6394355.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值