| 1 |
/* |
| 2 |
* Contributed to the public domain by James K. Lowden |
| 3 |
* Tuesday October 17, 2023 |
| 4 |
* |
| 5 |
* This stand-in for std::regex was written because the implementation provided |
| 6 |
* by the GCC libstdc++ in GCC 11 proved too slow, where "slow" means "appears |
| 7 |
* not to terminate". Some invocations of std::regex_search took over 5 |
| 8 |
* seconds (or minutes) and used over 1900 stack frames, and "never" returned. |
| 9 |
* Because the same patterns and input presented no difficulty to the C standad |
| 10 |
* library regex functions, I recast the C++ implementation in terms of |
| 11 |
* regex(3). |
| 12 |
* |
| 13 |
* Unlike std::regex, this dts version supports only Posix EREs, and requires |
| 14 |
* the input to be NUL-terminated. |
| 15 |
* |
| 16 |
* It is my hope and expectation to replace this implementation with the |
| 17 |
* standard one when it is improved. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <stdexcept> |
| 21 |
#include <vector> |
| 22 |
|
| 23 |
#include <regex.h> |
| 24 |
|
| 25 |
namespace dts { |
| 26 |
class csub_match : public regmatch_t { |
| 27 |
const char *input; |
| 28 |
public: |
| 29 |
const char *first, *second; |
| 30 |
bool matched; |
| 31 |
|
| 32 |
explicit csub_match( const char *input = NULL) |
| 33 |
: input(input) |
| 34 |
, first(NULL), second(NULL), matched(false) |
| 35 |
{ |
| 36 |
static regmatch_t empty; |
| 37 |
empty.rm_so = empty.rm_eo = -1; |
| 38 |
regmatch_t& self(*this); // cppcheck-suppress constVariableReference |
| 39 |
self = empty; |
| 40 |
} |
| 41 |
csub_match( const char input[], const regmatch_t& m ) |
| 42 |
: input(input) |
| 43 |
{ |
| 44 |
regmatch_t& self(*this); // cppcheck-suppress constVariableReference |
| 45 |
self = m; |
| 46 |
matched = rm_so != -1; |
| 47 |
first = rm_so == -1? NULL : input + rm_so; |
| 48 |
second = rm_eo == -1? NULL : input + rm_eo; |
| 49 |
} |
| 50 |
|
| 51 |
int length() const { return rm_eo - rm_so; } |
| 52 |
}; |
| 53 |
|
| 54 |
typedef std::vector<csub_match> cmatch; |
| 55 |
|
| 56 |
class regex : public regex_t { |
| 57 |
size_t nsubexpr; |
| 58 |
const char *pattern; |
| 59 |
public: |
| 60 |
enum cflag_t { extended = REG_EXTENDED, icase = REG_ICASE }; |
| 61 |
|
| 62 |
regex( const char pattern[], int flags ) : pattern(pattern) { |
| 63 |
nsubexpr = 1 + std::count(pattern, pattern + strlen(pattern), '('); |
| 64 |
int erc = regcomp(this, pattern, flags); |
| 65 |
if( erc != 0 ) { |
| 66 |
char msg[80]; |
| 67 |
regerror(erc, this, msg, sizeof msg); |
| 68 |
#if __cpp_exceptions |
| 69 |
throw std::logic_error(msg); |
| 70 |
#else |
| 71 |
cbl_errx("%s", msg); |
| 72 |
#endif |
| 73 |
} |
| 74 |
} |
| 75 |
~regex() { regfree(this); } |
| 76 |
|
| 77 |
size_t size() const { return nsubexpr; } |
| 78 |
bool ready() const { return pattern != NULL; } |
| 79 |
private: |
| 80 |
regex( const regex& ) = default; |
| 81 |
}; |
| 82 |
|
| 83 |
inline bool regex_search( const char input[], const char *eoinput, |
| 84 |
cmatch& cm, regex& re ) { |
| 85 |
if( eoinput != NULL && *eoinput != '\0' ) { |
| 86 |
#if __cpp_exceptions |
| 87 |
static const char msg[] = "input not NUL-terminated"; |
| 88 |
throw std::domain_error( msg ); |
| 89 |
#endif |
| 90 |
} |
| 91 |
auto ncm = re.size(); |
| 92 |
cm.resize(ncm); |
| 93 |
std::vector <regmatch_t> cms(ncm); |
| 94 |
|
| 95 |
int erc = regexec( &re, input, ncm, cms.data(), 0 ); |
| 96 |
if( erc != 0 ) return false; |
| 97 |
#if __cpp_exceptions |
| 98 |
// This is not correct at all, but current use depends on current behavior. |
| 99 |
// The following line is excluded from the GCC build, which is compiled |
| 100 |
// without __cpp_exceptions. parse_copy_directive (for one) depends on |
| 101 |
// regex_search returning true even if the match is beyond eoinput. |
| 102 |
if( eoinput < cm[0].second ) return false; |
| 103 |
// Correct behavior would return match only between input and eoinput. |
| 104 |
// Because regex(3) uses a NUL terminator, it may match text between |
| 105 |
// eoinput and the NUL. |
| 106 |
#endif |
| 107 |
std::transform( cms.begin(), cms.end(), cm.begin(), |
| 108 |
[input]( const regmatch_t& m ) { |
| 109 |
return csub_match( input, m ); |
| 110 |
} ); |
| 111 |
return true; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|