summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/pattern.cc
diff options
context:
space:
mode:
authorAdam Borowski <kilobyte@angband.pl>2012-10-29 05:57:36 +0100
committerAdam Borowski <kilobyte@angband.pl>2012-10-31 22:59:39 +0100
commit6bd4f176038ec9b3704ffe4b1522eff582a0b144 (patch)
tree061d8363896691282274e0be8e3152ce287790ac /crawl-ref/source/pattern.cc
parentf73d0cfe86df169f25b19233024fa3e1acced18a (diff)
downloadcrawl-ref-6bd4f176038ec9b3704ffe4b1522eff582a0b144.tar.gz
crawl-ref-6bd4f176038ec9b3704ffe4b1522eff582a0b144.zip
Detemplatize text_pattern.
It is included everywhere, complex templates drag down compilation. And what's the point of a template that's instantiated exactly once?
Diffstat (limited to 'crawl-ref/source/pattern.cc')
-rw-r--r--crawl-ref/source/pattern.cc79
1 files changed, 73 insertions, 6 deletions
diff --git a/crawl-ref/source/pattern.cc b/crawl-ref/source/pattern.cc
index af4297da48..e2296f9e9f 100644
--- a/crawl-ref/source/pattern.cc
+++ b/crawl-ref/source/pattern.cc
@@ -1,12 +1,25 @@
#include "AppHdr.h"
+#ifdef REGEX_PCRE
+ // Statically link pcre on Windows
+ #if defined(TARGET_OS_WINDOWS)
+ #define PCRE_STATIC
+ #endif
+
+ #include <pcre.h>
+#endif
+
+#ifdef REGEX_POSIX
+ #include <regex.h>
+#endif
+
#include "pattern.h"
#if defined(REGEX_PCRE)
////////////////////////////////////////////////////////////////////
// Perl Compatible Regular Expressions
-void *compile_pattern(const char *pattern, bool icase)
+static void *_compile_pattern(const char *pattern, bool icase)
{
const char *error;
int erroffset;
@@ -18,13 +31,13 @@ void *compile_pattern(const char *pattern, bool icase)
NULL);
}
-void free_compiled_pattern(void *cp)
+static void _free_compiled_pattern(void *cp)
{
if (cp)
pcre_free(cp);
}
-bool pattern_match(void *compiled_pattern, const char *text, int length)
+static bool _pattern_match(void *compiled_pattern, const char *text, int length)
{
int ovector[42];
int pcre_rc = pcre_exec(static_cast<pcre *>(compiled_pattern),
@@ -39,7 +52,7 @@ bool pattern_match(void *compiled_pattern, const char *text, int length)
////////////////////////////////////////////////////////////////////
// POSIX regular expressions
-void *compile_pattern(const char *pattern, bool icase)
+static void *_compile_pattern(const char *pattern, bool icase)
{
regex_t *re = new regex_t;
if (!re)
@@ -58,7 +71,7 @@ void *compile_pattern(const char *pattern, bool icase)
return re;
}
-void free_compiled_pattern(void *cp)
+static void _free_compiled_pattern(void *cp)
{
if (cp)
{
@@ -68,7 +81,7 @@ void free_compiled_pattern(void *cp)
}
}
-bool pattern_match(void *compiled_pattern, const char *text, int length)
+static bool _pattern_match(void *compiled_pattern, const char *text, int length)
{
regex_t *re = static_cast<regex_t *>(compiled_pattern);
return !regexec(re, text, 0, NULL, 0);
@@ -76,3 +89,57 @@ bool pattern_match(void *compiled_pattern, const char *text, int length)
////////////////////////////////////////////////////////////////////
#endif
+
+text_pattern::~text_pattern()
+{
+ if (compiled_pattern)
+ _free_compiled_pattern(compiled_pattern);
+}
+
+const text_pattern &text_pattern::operator= (const text_pattern &tp)
+{
+ if (this == &tp)
+ return tp;
+
+ if (compiled_pattern)
+ _free_compiled_pattern(compiled_pattern);
+ pattern = tp.pattern;
+ compiled_pattern = NULL;
+ isvalid = tp.isvalid;
+ ignore_case = tp.ignore_case;
+ return *this;
+}
+
+const text_pattern &text_pattern::operator= (const string &spattern)
+{
+ if (pattern == spattern)
+ return *this;
+
+ if (compiled_pattern)
+ _free_compiled_pattern(compiled_pattern);
+ pattern = spattern;
+ compiled_pattern = NULL;
+ isvalid = true;
+ // We don't change ignore_case
+ return *this;
+}
+
+bool text_pattern::operator== (const text_pattern &tp) const
+{
+ if (this == &tp)
+ return true;
+
+ return (pattern == tp.pattern && ignore_case == tp.ignore_case);
+}
+
+bool text_pattern::compile() const
+{
+ return !empty()?
+ !!(compiled_pattern = _compile_pattern(pattern.c_str(), ignore_case))
+ : false;
+}
+
+bool text_pattern::matches(const char *s, int length) const
+{
+ return valid() && _pattern_match(compiled_pattern, s, length);
+}