forked from piaw/google-gtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexp.h
66 lines (54 loc) · 1.68 KB
/
regexp.h
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
// Copyright 2007 Google Inc. All Rights Reserved.
// Author: [email protected] (Stephen Chen)
#ifndef TOOLS_TAGS_REGEXP_H__
#define TOOLS_TAGS_REGEXP_H__
#include <regex.h>
#include "tagsutil.h"
class RegExp {
private:
regex_t reg_;
bool error_;
public:
RegExp(const string & re) : error_(false) {
if (regcomp(®_, re.c_str(), REG_EXTENDED)) {
LOG(WARNING) << "Corrupted regular expression: " << re << "\n";
error_ = true;
}
}
~RegExp() {
regfree(®_);
}
bool error() {
return error_;
}
// In PartialMatch and FullMatch:
// nsub is the quantity of sub-expressions (parenthesis) in the regular
// expression.
// pmat is the structure that regexec uses to return the position of the match
// regexec returns 0 if there is a match and != 0 if not
// pmat[0].rm_eo and pmat[0].rm_so are the end and the beginning of the
// regular expression match
bool PartialMatch(const string & str) {
// If any substring of str matches, return true
int nsub = reg_.re_nsub+1;
regmatch_t *pmat;
pmat = (regmatch_t *) malloc(sizeof(regmatch_t)*nsub);
bool match = (regexec(®_, str.c_str(), nsub, pmat, 0) == 0);
free(pmat);
return match;
}
bool FullMatch(const string & str) {
// If str matches, return true
int nsub = reg_.re_nsub+1;
regmatch_t *pmat;
pmat = (regmatch_t *) malloc(sizeof(regmatch_t)*nsub);
// It matches
bool match = (regexec(®_, str.c_str(), nsub, pmat, 0) == 0);
bool ret = false;
// From the begginning to the end of str
if (match) ret = ((pmat[0].rm_eo - pmat[0].rm_so) == str.size());
free(pmat);
return ret;
}
};
#endif // TOOLS_TAGS_REGEXP_H__