File: posix.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (97 lines) | stat: -rw-r--r-- 2,078 bytes parent folder | download | duplicates (17)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
///////////////////////////////////////////////////////////////
//  Copyright 2015 John Maddock. Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
//

#ifdef TEST_POSIX

#include "performance.hpp"
#include <boost/lexical_cast.hpp>

#include <regex.h>

struct posix_regex : public abstract_regex
{
private:
   regex_t pe, pe2;
   bool init;
public:
   posix_regex() : init(false) {}
   ~posix_regex()
   {
      if(init)
      {
         regfree(&pe);
         regfree(&pe2);
      }
   }
   virtual bool set_expression(const char* pat, bool isperl)
   {
      if(isperl)
         return false;
      if(init)
      {
         regfree(&pe);
         regfree(&pe2);
      }
      else
         init = true;
      int r = regcomp(&pe, pat, REG_EXTENDED);
      std::string s(pat);
      if(s.size() && (s[0] != '^'))
         s.insert(0, 1, '^');
      if(s.size() && (*s.rbegin() != '$'))
         s.append("$");
      r |= regcomp(&pe2, s.c_str(), REG_EXTENDED);
      return r ? false : true;
   }
   virtual bool match_test(const char* text);
   virtual unsigned find_all(const char* text);
   virtual std::string name();

   struct initializer
   {
      initializer()
      {
         posix_regex::register_instance(boost::shared_ptr<abstract_regex>(new posix_regex));
      }
      void do_nothing()const {}
   };
   static const initializer init2;
};

const posix_regex::initializer posix_regex::init2;


bool posix_regex::match_test(const char * text)
{
   regmatch_t m[30];
   int r = regexec(&pe2, text, 30, m, 0);
   return r == 0;
}

unsigned posix_regex::find_all(const char * text)
{
   unsigned count = 0;
   regmatch_t m[30];
   int flags = 0;
   while(regexec(&pe, text, 30, m, flags) == 0)
   {
      ++count;
      text += m[0].rm_eo;
      if(m[0].rm_eo - m[0].rm_so)
         flags = *(text - 1) == '\n' ? 0 : REG_NOTBOL;
      else
         flags = 0;
   }
   return 0;
}

std::string posix_regex::name()
{
   init2.do_nothing();
   return "POSIX";
}

#endif