29 |
// |
// |
30 |
// Author: Sanjay Ghemawat |
// Author: Sanjay Ghemawat |
31 |
|
|
32 |
|
#ifdef HAVE_CONFIG_H |
33 |
|
# include <config.h> |
34 |
|
#endif |
35 |
|
|
36 |
#include <stdlib.h> |
#include <stdlib.h> |
37 |
#include <stdio.h> |
#include <stdio.h> |
38 |
#include <ctype.h> |
#include <ctype.h> |
41 |
#include <errno.h> |
#include <errno.h> |
42 |
#include <string> |
#include <string> |
43 |
#include <algorithm> |
#include <algorithm> |
|
#include "config.h" |
|
44 |
// We need this to compile the proper dll on windows/msys. This is copied |
// We need this to compile the proper dll on windows/msys. This is copied |
45 |
// from pcre_internal.h. It would probably be better just to include that. |
// from pcre_internal.h. It would probably be better just to include that. |
46 |
#define PCRE_DEFINITION /* Win32 __declspec(export) trigger for .dll */ |
#define PCRE_DEFINITION /* Win32 __declspec(export) trigger for .dll */ |
47 |
#include "pcre.h" |
#include <pcre.h> |
48 |
#include "pcre_stringpiece.h" |
#include "pcre_stringpiece.h" |
49 |
#include "pcrecpp.h" |
#include "pcrecpp.h" |
50 |
|
|
64 |
// If the user doesn't ask for any options, we just use this one |
// If the user doesn't ask for any options, we just use this one |
65 |
static RE_Options default_options; |
static RE_Options default_options; |
66 |
|
|
67 |
void RE::Init(const char* pat, const RE_Options* options) { |
void RE::Init(const string& pat, const RE_Options* options) { |
68 |
pattern_ = pat; |
pattern_ = pat; |
69 |
if (options == NULL) { |
if (options == NULL) { |
70 |
options_ = default_options; |
options_ = default_options; |
81 |
// conservative in that it may treat some "simple" patterns |
// conservative in that it may treat some "simple" patterns |
82 |
// as "complex" (e.g., if the vertical bar is in a character |
// as "complex" (e.g., if the vertical bar is in a character |
83 |
// class or is escaped). But it seems good enough. |
// class or is escaped). But it seems good enough. |
84 |
if (strchr(pat, '|') == NULL) { |
if (strchr(pat.c_str(), '|') == NULL) { |
85 |
// Simple pattern: we can use position-based checks to perform |
// Simple pattern: we can use position-based checks to perform |
86 |
// fully anchored matches |
// fully anchored matches |
87 |
re_full_ = re_partial_; |
re_full_ = re_partial_; |
92 |
} |
} |
93 |
} |
} |
94 |
|
|
95 |
RE::~RE() { |
void RE::Cleanup() { |
96 |
if (re_full_ != NULL && re_full_ != re_partial_) (*pcre_free)(re_full_); |
if (re_full_ != NULL && re_full_ != re_partial_) (*pcre_free)(re_full_); |
97 |
if (re_partial_ != NULL) (*pcre_free)(re_partial_); |
if (re_partial_ != NULL) (*pcre_free)(re_partial_); |
98 |
if (error_ != &empty_string) delete error_; |
if (error_ != &empty_string) delete error_; |
99 |
} |
} |
100 |
|
|
101 |
|
|
102 |
|
RE::~RE() { |
103 |
|
Cleanup(); |
104 |
|
} |
105 |
|
|
106 |
|
|
107 |
pcre* RE::Compile(Anchor anchor) { |
pcre* RE::Compile(Anchor anchor) { |
108 |
// First, convert RE_Options into pcre options |
// First, convert RE_Options into pcre options |
109 |
int pcre_options = 0; |
int pcre_options = 0; |
433 |
return Rewrite(out, rewrite, text, vec, matches); |
return Rewrite(out, rewrite, text, vec, matches); |
434 |
} |
} |
435 |
|
|
436 |
|
/*static*/ string RE::QuoteMeta(const StringPiece& unquoted) { |
437 |
|
string result; |
438 |
|
|
439 |
|
// Escape any ascii character not in [A-Za-z_0-9]. |
440 |
|
// |
441 |
|
// Note that it's legal to escape a character even if it has no |
442 |
|
// special meaning in a regular expression -- so this function does |
443 |
|
// that. (This also makes it identical to the perl function of the |
444 |
|
// same name; see `perldoc -f quotemeta`.) |
445 |
|
for (int ii = 0; ii < unquoted.size(); ++ii) { |
446 |
|
// Note that using 'isalnum' here raises the benchmark time from |
447 |
|
// 32ns to 58ns: |
448 |
|
if ((unquoted[ii] < 'a' || unquoted[ii] > 'z') && |
449 |
|
(unquoted[ii] < 'A' || unquoted[ii] > 'Z') && |
450 |
|
(unquoted[ii] < '0' || unquoted[ii] > '9') && |
451 |
|
unquoted[ii] != '_' && |
452 |
|
// If this is the part of a UTF8 or Latin1 character, we need |
453 |
|
// to copy this byte without escaping. Experimentally this is |
454 |
|
// what works correctly with the regexp library. |
455 |
|
!(unquoted[ii] & 128)) { |
456 |
|
result += '\\'; |
457 |
|
} |
458 |
|
result += unquoted[ii]; |
459 |
|
} |
460 |
|
|
461 |
|
return result; |
462 |
|
} |
463 |
|
|
464 |
/***** Actual matching and rewriting code *****/ |
/***** Actual matching and rewriting code *****/ |
465 |
|
|
466 |
int RE::TryMatch(const StringPiece& text, |
int RE::TryMatch(const StringPiece& text, |
846 |
return parse_##name##_radix(str, n, dest, 0); \ |
return parse_##name##_radix(str, n, dest, 0); \ |
847 |
} |
} |
848 |
|
|
849 |
DEFINE_INTEGER_PARSERS(short); |
DEFINE_INTEGER_PARSERS(short) /* */ |
850 |
DEFINE_INTEGER_PARSERS(ushort); |
DEFINE_INTEGER_PARSERS(ushort) /* */ |
851 |
DEFINE_INTEGER_PARSERS(int); |
DEFINE_INTEGER_PARSERS(int) /* Don't use semicolons after these */ |
852 |
DEFINE_INTEGER_PARSERS(uint); |
DEFINE_INTEGER_PARSERS(uint) /* statements because they can cause */ |
853 |
DEFINE_INTEGER_PARSERS(long); |
DEFINE_INTEGER_PARSERS(long) /* compiler warnings if the checking */ |
854 |
DEFINE_INTEGER_PARSERS(ulong); |
DEFINE_INTEGER_PARSERS(ulong) /* level is turned up high enough. */ |
855 |
DEFINE_INTEGER_PARSERS(longlong); |
DEFINE_INTEGER_PARSERS(longlong) /* */ |
856 |
DEFINE_INTEGER_PARSERS(ulonglong); |
DEFINE_INTEGER_PARSERS(ulonglong) /* */ |
857 |
|
|
858 |
#undef DEFINE_INTEGER_PARSERS |
#undef DEFINE_INTEGER_PARSERS |
859 |
|
|