2525// //////////////////////////////////////////////////////////////////////////////
2626
2727#include < RX.h>
28+
2829#include < cstring>
30+ #include < regex>
2931#include < string>
3032
3133// //////////////////////////////////////////////////////////////////////////////
@@ -51,11 +53,7 @@ RX::RX (const RX& other)
5153}
5254
5355// //////////////////////////////////////////////////////////////////////////////
54- RX::~RX ()
55- {
56- if (_compiled)
57- regfree (&_regex);
58- }
56+ RX::~RX () = default ;
5957
6058// //////////////////////////////////////////////////////////////////////////////
6159RX& RX::operator = (const RX& other)
@@ -72,23 +70,16 @@ void RX::compile ()
7270{
7371 if (! _compiled)
7472 {
75- memset (&_regex, 0 , sizeof (regex_t ));
76-
77- int result;
78- if ((result = regcomp (&_regex, _pattern.c_str (),
79- #if defined REG_ENHANCED
80- REG_ENHANCED | REG_EXTENDED | REG_NEWLINE |
81- #else
82- REG_EXTENDED | REG_NEWLINE |
83- #endif
84- (_case_sensitive ? 0 : REG_ICASE))) != 0 )
85- {
86- char message[256 ];
87- regerror (result, &_regex, message, 256 );
88- throw std::string (message);
73+ std::regex::flag_type flags = std::regex_constants::ECMAScript;
74+ if (!_case_sensitive) {
75+ flags |= std::regex_constants::icase;
76+ }
77+ try {
78+ _regex.assign (_pattern, flags);
79+ _compiled = true ;
80+ } catch (const std::regex_error& e) {
81+ throw std::runtime_error (e.what ());
8982 }
90-
91- _compiled = true ;
9283 }
9384}
9485
@@ -98,7 +89,7 @@ bool RX::match (const std::string& in)
9889 if (! _compiled)
9990 compile ();
10091
101- return regexec (&_regex, in. c_str (), 0 , nullptr , 0 ) == 0 ;
92+ return std::regex_search (in, _regex) ;
10293}
10394
10495// //////////////////////////////////////////////////////////////////////////////
@@ -109,21 +100,13 @@ bool RX::match (
109100 if (! _compiled)
110101 compile ();
111102
112- regmatch_t rm[2 ];
113- int offset = 0 ;
114- int length = in.length ();
115- while (regexec (&_regex, in.c_str () + offset, 2 , &rm[0 ], 0 ) == 0 &&
116- offset < length)
117- {
118- matches.push_back (in.substr (rm[0 ].rm_so + offset, rm[0 ].rm_eo - rm[0 ].rm_so ));
119- offset += rm[0 ].rm_eo ;
120-
121- // Protection against zero-width patterns causing infinite loops.
122- if (rm[0 ].rm_so == rm[0 ].rm_eo )
123- ++offset;
103+ auto matches_begin = std::sregex_iterator (in.begin (), in.end (), _regex);
104+ auto matches_end = std::sregex_iterator ();
105+ for (std::sregex_iterator it = matches_begin; it != matches_end; ++it) {
106+ matches.push_back (it->str ());
124107 }
125108
126- return !matches.empty ();
109+ return !matches.empty ();
127110}
128111
129112// //////////////////////////////////////////////////////////////////////////////
@@ -135,19 +118,11 @@ bool RX::match (
135118 if (! _compiled)
136119 compile ();
137120
138- regmatch_t rm[2 ];
139- int offset = 0 ;
140- int length = in.length ();
141- while (regexec (&_regex, in.c_str () + offset, 2 , &rm[0 ], 0 ) == 0 &&
142- offset < length)
143- {
144- start.push_back (rm[0 ].rm_so + offset);
145- end.push_back (rm[0 ].rm_eo + offset);
146- offset += rm[0 ].rm_eo ;
147-
148- // Protection against zero-width patterns causing infinite loops.
149- if (rm[0 ].rm_so == rm[0 ].rm_eo )
150- ++offset;
121+ auto matches_begin = std::sregex_iterator (in.begin (), in.end (), _regex);
122+ auto matches_end = std::sregex_iterator ();
123+ for (std::sregex_iterator it = matches_begin; it != matches_end; ++it) {
124+ start.push_back (it->position ());
125+ end.push_back (it->position () + it->length ());
151126 }
152127
153128 return !start.empty ();
0 commit comments