From 10700c615179f07d4832d459e6453eed736cfaef Mon Sep 17 00:00:00 2001 From: Anders Bakken Date: Sun, 1 Mar 2015 12:32:27 -0800 Subject: [PATCH] switch to std::regex --- rct/Rct.h | 22 ++++++++++ rct/RegExp.h | 122 --------------------------------------------------- 2 files changed, 22 insertions(+), 122 deletions(-) delete mode 100644 rct/RegExp.h diff --git a/rct/Rct.h b/rct/Rct.h index a11bb07..78682ed 100644 --- a/rct/Rct.h +++ b/rct/Rct.h @@ -8,6 +8,7 @@ #include #include #include +#include namespace Rct { @@ -21,6 +22,27 @@ constexpr size_t countof(T(&)[N]) enum { Max_USec = 1000000 }; +inline int indexIn(const String &string, const std::regex &rx) +{ + std::cmatch match; + if (std::regex_match(string.constData(), match, rx) && !match.empty()) + return match.position(0); + return -1; +} + +inline bool contains(const char *str, const std::regex &rx, std::cmatch *match = 0) +{ + std::cmatch null; + std::cmatch &m = match ? *match : null; + return std::regex_match(str, m, rx); +} + +inline bool contains(const String &str, const std::regex &rx, std::cmatch *match = 0) +{ + return contains(str.constData(), rx, match); +} + + String shortOptions(const option *longOptions); int readLine(FILE *f, char *buf = 0, int max = -1); String readAll(FILE *f, int max = -1); diff --git a/rct/RegExp.h b/rct/RegExp.h deleted file mode 100644 index 211dbb7..0000000 --- a/rct/RegExp.h +++ /dev/null @@ -1,122 +0,0 @@ -#ifndef RegExp_h -#define RegExp_h - -#include -#include -#include -#include - -class RegExp -{ -public: - RegExp(const String &pattern = String(), uint32_t flags = 0) - : mPattern(pattern), mFlags(flags), mState(Unset) - {} - - ~RegExp() - { - if (mState == Success) { - regfree(&mRegex); - } - } - - bool isValid() const - { - if (mState == Unset) { - if (mPattern.isEmpty()) { - mState = Error; - } else { - mState = regcomp(&mRegex, mPattern.constData(), mFlags) ? Error : Success; - } - } - return mState == Success; - } - - bool isEmpty() const - { - return mPattern.isEmpty() || !isValid(); - } - - String pattern() const - { - return mPattern; - } - - RegExp &operator=(const String &pattern) - { - clear(); - mPattern = pattern; - return *this; - } - - void clear() - { - mPattern.clear(); - mFlags = 0; - mState = Unset; - } - - struct Capture { - Capture() - : index(-1) - {} - int index; - String capture; - }; - - int indexIn(const String &string, int offset = 0, List *caps = 0, uint32_t flags = 0) const - { - if (!isValid()) - return -1; - regmatch_t captures[10]; - if (regexec(&mRegex, string.constData() + offset, caps ? sizeof(captures) / sizeof(regmatch_t) : 1, captures, flags)) { - return -1; - } - if (caps) { - caps->clear(); - for (unsigned i=0; iappend(capture); - } else { - break; - } - } - } else { - captures[0].rm_so += offset; - } - return captures[0].rm_so; - } - - int indexIn(const List &strings, int listOffset = 0, List *caps = 0, uint32_t flags = 0) const - { - for (int i=listOffset; i