Torok Edwin | ce0c81e | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 1 | //===-- Regex.cpp - Regular Expression matcher implementation -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a POSIX regular expression matcher. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Chris Lattner | 48ba9ff | 2009-09-24 20:15:51 +0000 | [diff] [blame] | 13 | |
Torok Edwin | ce0c81e | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Regex.h" |
| 15 | #include "llvm/Support/ErrorHandling.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Torok Edwin | ce0c81e | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 18 | #include "regex_impl.h" |
| 19 | #include <string> |
Torok Edwin | ce0c81e | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Chris Lattner | 48ba9ff | 2009-09-24 20:15:51 +0000 | [diff] [blame] | 21 | |
| 22 | Regex::Regex(const StringRef ®ex, unsigned Flags) { |
Torok Edwin | ce0c81e | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 23 | unsigned flags = 0; |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 24 | preg = new llvm_regex(); |
Torok Edwin | ce0c81e | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 25 | preg->re_endp = regex.end(); |
| 26 | if (Flags & IgnoreCase) |
| 27 | flags |= REG_ICASE; |
Torok Edwin | ce0c81e | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 28 | if (Flags & Newline) |
| 29 | flags |= REG_NEWLINE; |
| 30 | error = llvm_regcomp(preg, regex.data(), flags|REG_EXTENDED|REG_PEND); |
| 31 | } |
| 32 | |
Chris Lattner | 48ba9ff | 2009-09-24 20:15:51 +0000 | [diff] [blame] | 33 | Regex::~Regex() { |
Torok Edwin | ce0c81e | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 34 | llvm_regfree(preg); |
| 35 | delete preg; |
| 36 | } |
| 37 | |
Chris Lattner | 81f46d9 | 2009-09-26 21:27:04 +0000 | [diff] [blame] | 38 | bool Regex::isValid(std::string &Error) { |
| 39 | if (!error) |
| 40 | return true; |
| 41 | |
| 42 | size_t len = llvm_regerror(error, preg, NULL, 0); |
| 43 | |
| 44 | Error.resize(len); |
| 45 | llvm_regerror(error, preg, &Error[0], len); |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | /// getNumMatches - In a valid regex, return the number of parenthesized |
| 50 | /// matches it contains. |
| 51 | unsigned Regex::getNumMatches() const { |
| 52 | return preg->re_nsub; |
| 53 | } |
| 54 | |
Chris Lattner | 48ba9ff | 2009-09-24 20:15:51 +0000 | [diff] [blame] | 55 | bool Regex::match(const StringRef &String, SmallVectorImpl<StringRef> *Matches){ |
Torok Edwin | ce0c81e | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 56 | unsigned nmatch = Matches ? preg->re_nsub+1 : 0; |
| 57 | |
Torok Edwin | ce0c81e | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 58 | // pmatch needs to have at least one element. |
Chris Lattner | 5287008 | 2009-09-24 21:47:32 +0000 | [diff] [blame] | 59 | SmallVector<llvm_regmatch_t, 8> pm; |
Torok Edwin | ce0c81e | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 60 | pm.resize(nmatch > 0 ? nmatch : 1); |
| 61 | pm[0].rm_so = 0; |
| 62 | pm[0].rm_eo = String.size(); |
| 63 | |
| 64 | int rc = llvm_regexec(preg, String.data(), nmatch, pm.data(), REG_STARTEND); |
| 65 | |
| 66 | if (rc == REG_NOMATCH) |
| 67 | return false; |
| 68 | if (rc != 0) { |
| 69 | // regexec can fail due to invalid pattern or running out of memory. |
| 70 | error = rc; |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | // There was a match. |
| 75 | |
| 76 | if (Matches) { // match position requested |
Chris Lattner | 81f46d9 | 2009-09-26 21:27:04 +0000 | [diff] [blame] | 77 | Matches->clear(); |
| 78 | |
Chris Lattner | 48ba9ff | 2009-09-24 20:15:51 +0000 | [diff] [blame] | 79 | for (unsigned i = 0; i != nmatch; ++i) { |
Torok Edwin | ce0c81e | 2009-08-30 08:24:09 +0000 | [diff] [blame] | 80 | if (pm[i].rm_so == -1) { |
| 81 | // this group didn't match |
| 82 | Matches->push_back(StringRef()); |
| 83 | continue; |
| 84 | } |
| 85 | assert(pm[i].rm_eo > pm[i].rm_so); |
| 86 | Matches->push_back(StringRef(String.data()+pm[i].rm_so, |
| 87 | pm[i].rm_eo-pm[i].rm_so)); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return true; |
| 92 | } |