• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 ///////////////////////////////////////////////////////////////
2 //  Copyright 2015 John Maddock. Distributed under the Boost
3 //  Software License, Version 1.0. (See accompanying file
4 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
5 //
6 
7 #ifdef TEST_PCRE2
8 
9 #define PCRE2_STATIC
10 #define PCRE2_CODE_UNIT_WIDTH 8
11 
12 #include "performance.hpp"
13 #include <pcre2.h>
14 #include <boost/version.hpp>
15 #include <boost/lexical_cast.hpp>
16 
17 struct pcre_regex : public abstract_regex
18 {
19 private:
20    pcre2_code* pe;
21    pcre2_match_data* pdata;
22 public:
pcre_regexpcre_regex23    pcre_regex()
24       : pe(0)
25    {
26       pdata = pcre2_match_data_create(30, NULL);
27    }
~pcre_regexpcre_regex28    ~pcre_regex()
29    {
30       if(pe)
31          pcre2_code_free(pe);
32       pcre2_match_data_free(pdata);
33    }
set_expressionpcre_regex34    virtual bool set_expression(const char* pat, bool isperl)
35    {
36       if(!isperl)
37          return false;
38       if(pe)
39          pcre2_code_free(pe);
40       int errorcode = 0;
41       PCRE2_SIZE erroroffset;
42       pe = pcre2_compile((PCRE2_SPTR)pat, std::strlen(pat), PCRE2_MULTILINE, &errorcode, &erroroffset, NULL);
43       return pe ? true : false;
44    }
45    virtual bool match_test(const char* text);
46    virtual unsigned find_all(const char* text);
47    virtual std::string name();
48 
49    struct initializer
50    {
initializerpcre_regex::initializer51       initializer()
52       {
53          pcre_regex::register_instance(boost::shared_ptr<abstract_regex>(new pcre_regex));
54       }
do_nothingpcre_regex::initializer55       void do_nothing()const {}
56    };
57    static const initializer init;
58 };
59 
60 const pcre_regex::initializer pcre_regex::init;
61 
62 
match_test(const char * text)63 bool pcre_regex::match_test(const char * text)
64 {
65    int r = pcre2_match(pe, (PCRE2_SPTR)text, std::strlen(text), 0, PCRE2_ANCHORED, pdata, NULL);
66    return r >= 0;
67 }
68 
find_all(const char * text)69 unsigned pcre_regex::find_all(const char * text)
70 {
71    unsigned count = 0;
72    int flags = 0;
73    const char* end = text + std::strlen(text);
74    while(pcre2_match(pe, (PCRE2_SPTR)text, end - text, 0, flags, pdata, NULL) >= 0)
75    {
76       ++count;
77       PCRE2_SIZE* v = pcre2_get_ovector_pointer(pdata);
78       text += v[1];
79       if(v[0] == v[1])
80          ++text;
81       if(*text)
82       {
83          flags = *(text - 1) == '\n' ? 0 : PCRE2_NOTBOL;
84       }
85    }
86    return count;
87 }
88 
name()89 std::string pcre_regex::name()
90 {
91    init.do_nothing();
92    return std::string("PCRE-") + boost::lexical_cast<std::string>(PCRE2_MAJOR) + "." + boost::lexical_cast<std::string>(PCRE2_MINOR);
93 }
94 
95 #endif
96