remove support for "NoSub" from regex. It seems like a minor optimization
and makes the API more annoying. Add a Regex::getNumMatches() method.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82877 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Support/Regex.cpp b/lib/Support/Regex.cpp
index 285e01f..618ca05 100644
--- a/lib/Support/Regex.cpp
+++ b/lib/Support/Regex.cpp
@@ -25,41 +25,36 @@
preg->re_endp = regex.end();
if (Flags & IgnoreCase)
flags |= REG_ICASE;
- if (Flags & NoSub) {
- flags |= REG_NOSUB;
- sub = false;
- } else {
- sub = true;
- }
if (Flags & Newline)
flags |= REG_NEWLINE;
error = llvm_regcomp(preg, regex.data(), flags|REG_EXTENDED|REG_PEND);
}
-bool Regex::isValid(std::string &Error) {
- if (!error)
- return true;
-
- size_t len = llvm_regerror(error, preg, NULL, 0);
-
- Error.resize(len);
- llvm_regerror(error, preg, &Error[0], len);
- return false;
-}
-
Regex::~Regex() {
llvm_regfree(preg);
delete preg;
}
+bool Regex::isValid(std::string &Error) {
+ if (!error)
+ return true;
+
+ size_t len = llvm_regerror(error, preg, NULL, 0);
+
+ Error.resize(len);
+ llvm_regerror(error, preg, &Error[0], len);
+ return false;
+}
+
+/// getNumMatches - In a valid regex, return the number of parenthesized
+/// matches it contains.
+unsigned Regex::getNumMatches() const {
+ return preg->re_nsub;
+}
+
bool Regex::match(const StringRef &String, SmallVectorImpl<StringRef> *Matches){
unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
- if (Matches) {
- assert(sub && "Substring matching requested but pattern compiled without");
- Matches->clear();
- }
-
// pmatch needs to have at least one element.
SmallVector<llvm_regmatch_t, 8> pm;
pm.resize(nmatch > 0 ? nmatch : 1);
@@ -79,6 +74,8 @@
// There was a match.
if (Matches) { // match position requested
+ Matches->clear();
+
for (unsigned i = 0; i != nmatch; ++i) {
if (pm[i].rm_so == -1) {
// this group didn't match