• 一种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,后边在进行操作。

  • 相关阅读:
    Median of Two Sorted Arrays
    Two Sum
    C# Socket服务端和客户端互相send和receive
    C++ 虚函数和虚函数表
    C++ 类型转换
    C# 几种退出程序的方式
    进程判断及简单操作
    C#根据函数名称执行对应的函数
    C# DateTime格式化
    nginx常用代理配置
  • 原文地址:https://www.cnblogs.com/yaoyuanfeixing/p/6394355.html
Copyright © 2020-2023  润新知