Add regular expression matching support, based on OpenBSD regexec()/regcomp()
[oota-llvm.git] / lib / Support / Regex.cpp
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 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/Regex.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include "regex_impl.h"
17 #include <string>
18
19 using namespace llvm;
20 Regex::Regex(const StringRef &regex, unsigned Flags)
21 {
22   unsigned flags = 0;
23   preg = new struct llvm_regex;
24   preg->re_endp = regex.end();
25   if (Flags & IgnoreCase) 
26     flags |= REG_ICASE;
27   if (Flags & NoSub) {
28     flags |= REG_NOSUB;
29     sub = false;
30   } else {
31     sub = true;
32   }
33   if (Flags & Newline)
34     flags |= REG_NEWLINE;
35   error = llvm_regcomp(preg, regex.data(), flags|REG_EXTENDED|REG_PEND);
36 }
37
38 bool Regex::isValid(std::string &Error)
39 {
40   if (!error)
41     return true;
42
43   size_t len = llvm_regerror(error, preg, NULL, 0);
44   char *errbuff = new char[len];
45   llvm_regerror(error, preg, errbuff, len);
46   Error.assign(errbuff);
47   return false;
48 }
49
50 Regex::~Regex()
51 {
52   llvm_regfree(preg);
53   delete preg;
54 }
55
56 bool Regex::match(const StringRef &String, SmallVectorImpl<StringRef> *Matches)
57 {
58   unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
59
60   if (Matches) {
61     assert(sub && "Substring matching requested but pattern compiled without");
62     Matches->clear();
63   }
64
65   // pmatch needs to have at least one element.
66   SmallVector<llvm_regmatch_t, 2> pm;
67   pm.resize(nmatch > 0 ? nmatch : 1);
68   pm[0].rm_so = 0;
69   pm[0].rm_eo = String.size();
70
71   int rc = llvm_regexec(preg, String.data(), nmatch, pm.data(), REG_STARTEND);
72
73   if (rc == REG_NOMATCH)
74     return false;
75   if (rc != 0) {
76     // regexec can fail due to invalid pattern or running out of memory.
77     error = rc;
78     return false;
79   }
80
81   // There was a match.
82
83   if (Matches) { // match position requested
84     for (unsigned i=0;i<nmatch; i++) {
85       if (pm[i].rm_so == -1) {
86         // this group didn't match
87         Matches->push_back(StringRef());
88         continue;
89       }
90       assert(pm[i].rm_eo > pm[i].rm_so);
91       Matches->push_back(StringRef(String.data()+pm[i].rm_so,
92                                    pm[i].rm_eo-pm[i].rm_so));
93     }
94   }
95
96   return true;
97 }