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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/* matcher.cpp
*
* Copyright (c) 1999-2008 Daniel Burrows
* Copyright (c) 2009-2016 Daniel Nicoletti <dantti12@gmail.com>
* 2012 Matthias Klumpp <matthias@tenstral.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "matcher.h"
#include <stdio.h>
#include <iostream>
Matcher::Matcher(const string &matchers) :
m_hasError(false)
{
string::const_iterator start = matchers.begin();
parse_pattern(start, matchers.end());
if (m_hasError) {
cerr << "ERROR: " << m_error << endl;
}
}
Matcher::~Matcher()
{
for (regex_t &rx : m_matches) {
regfree(&rx);
}
}
bool do_compile(const string &_pattern,
regex_t &pattern,
int cflags)
{
return !regcomp(&pattern, _pattern.c_str(), cflags);
}
bool string_matches(const char *s, regex_t &pattern_nogroup)
{
return !regexec(&pattern_nogroup, s, 0, NULL, 0);
}
bool Matcher::matches(const string &s)
{
int matchesCount = 0;
for (regex_t &rx : m_matches) {
if (string_matches(s.c_str(), rx)) {
matchesCount++;
}
}
return m_matches.size() == matchesCount;
}
bool Matcher::parse_pattern(string::const_iterator &start,
const std::string::const_iterator &end)
{
// Just filter blank strings out immediately.
while (start != end && isspace(*start)) {
++start;
}
if (start == end) {
return false;
}
while (start != end && *start != '|' && *start != ')') {
string subString = parse_substr(start, end);
if (subString.empty()) {
continue;
}
regex_t pattern_nogroup;
if (do_compile(subString, pattern_nogroup, REG_ICASE|REG_EXTENDED|REG_NOSUB)) {
m_matches.push_back(pattern_nogroup);
} else {
regfree(&pattern_nogroup);
m_error = string("Regex compilation error");
m_hasError = true;
return false;
}
}
return true;
}
string Matcher::parse_literal_string_tail(string::const_iterator &start,
const string::const_iterator end)
{
std::string rval;
while (start != end && *start != '"') {
if (*start == '\\') {
++start;
if (start != end) {
switch (*start) {
case 'n':
rval += '\n';
break;
case 't':
rval += '\t';
break;
default:
rval += *start;
break;
}
++start;
}
} else {
rval += *start;
++start;
}
}
if (start == end || *start != '"') {
m_error = string("Unterminated literal string after " + rval);
m_hasError = true;
return string();
}
++start;
return rval;
}
// Returns a substring up to the first metacharacter, including escaped
// metacharacters (parentheses, ~, |, and !)
//
// Advances loc to the first character of 's' following the escaped string.
string Matcher::parse_substr(string::const_iterator &start,
const string::const_iterator &end)
{
std::string rval;
bool done=false;
// Strip leading whitespace.
while (start != end && isspace(*start))
++start;
do {
while (start != end &&
*start != '(' &&
*start != ')' &&
*start != '!' &&
*start != '~' &&
*start != '|' &&
*start != '"' &&
!isspace(*start)) {
rval += *start;
++start;
}
if (start != end && *start == '"') {
++start;
rval += parse_literal_string_tail(start, end);
if (m_hasError) {
return string();
}
}
// We quit because we ran off the end of the string or saw a
// metacharacter. If the latter case and it was a tilde-escape,
// add the escaped character to the string and continue.
if (start != end && start+1 != end && *start == '~') {
const char next = *(start+1);
if (next == '(' || next == ')' ||
next == '!' || next == '~' ||
next == '|' || next == '"' ||
isspace(next)) {
rval += next;
start += 2;
} else {
done = true;
}
} else {
done = true;
}
} while(!done);
return rval;
}
bool Matcher::hasError() const
{
return m_hasError;
}
|