Skip to content

Commit 8ad3646

Browse files
authored
Merge pull request #97 from tobiolo/regex-stdcpp
Replace UNIX regex API with Standard C++ regex library
2 parents cb078b0 + 7c94a01 commit 8ad3646

File tree

2 files changed

+25
-50
lines changed

2 files changed

+25
-50
lines changed

src/RX.cpp

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
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
////////////////////////////////////////////////////////////////////////////////
6159
RX& 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 ();

src/RX.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#ifndef INCLUDED_RX
2828
#define INCLUDED_RX
2929

30-
#include <regex.h>
30+
#include <regex>
3131
#include <string>
3232
#include <vector>
3333

@@ -51,7 +51,7 @@ class RX
5151
bool _compiled {false};
5252
std::string _pattern {};
5353
bool _case_sensitive {false};
54-
regex_t _regex;
54+
std::regex _regex;
5555
};
5656

5757
#endif

0 commit comments

Comments
 (0)