1 //===-- RegularExpression.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/RegularExpression.h"
12 #include "llvm/ADT/StringRef.h"
16 //----------------------------------------------------------------------
17 // Enable enhanced mode if it is available. This allows for things like
18 // \d for digit, \s for space, and many more, but it isn't available
20 //----------------------------------------------------------------------
21 #if defined(REG_ENHANCED)
22 #define DEFAULT_COMPILE_FLAGS (REG_ENHANCED | REG_EXTENDED)
24 #define DEFAULT_COMPILE_FLAGS (REG_EXTENDED)
27 using namespace lldb_private;
29 RegularExpression::RegularExpression() : m_re(), m_comp_err(1), m_preg() {
30 memset(&m_preg, 0, sizeof(m_preg));
33 //----------------------------------------------------------------------
34 // Constructor that compiles "re" using "flags" and stores the
35 // resulting compiled regular expression into this object.
36 //----------------------------------------------------------------------
37 RegularExpression::RegularExpression(llvm::StringRef str)
38 : m_re(), m_comp_err(1), m_preg() {
39 memset(&m_preg, 0, sizeof(m_preg));
43 RegularExpression::RegularExpression(const RegularExpression &rhs) {
44 memset(&m_preg, 0, sizeof(m_preg));
45 Compile(rhs.GetText());
48 const RegularExpression &RegularExpression::
49 operator=(const RegularExpression &rhs) {
51 Compile(rhs.GetText());
55 //----------------------------------------------------------------------
58 // Any previously compiled regular expression contained in this
59 // object will be freed.
60 //----------------------------------------------------------------------
61 RegularExpression::~RegularExpression() { Free(); }
63 //----------------------------------------------------------------------
64 // Compile a regular expression using the supplied regular
65 // expression text and flags. The compiled regular expression lives
66 // in this object so that it can be readily used for regular
67 // expression matches. Execute() can be called after the regular
68 // expression is compiled. Any previously compiled regular
69 // expression contained in this object will be freed.
72 // True if the regular expression compiles successfully, false
74 //----------------------------------------------------------------------
75 bool RegularExpression::Compile(llvm::StringRef str) {
78 // regcomp() on darwin does not recognize "" as a valid regular expression, so
79 // we substitute it with an equivalent non-empty one.
80 m_re = str.empty() ? "()" : str;
81 m_comp_err = ::regcomp(&m_preg, m_re.c_str(), DEFAULT_COMPILE_FLAGS);
82 return m_comp_err == 0;
85 //----------------------------------------------------------------------
86 // Execute a regular expression match using the compiled regular
87 // expression that is already in this object against the match
88 // string "s". If any parens are used for regular expression
89 // matches "match_count" should indicate the number of regmatch_t
90 // values that are present in "match_ptr". The regular expression
91 // will be executed using the "execute_flags".
92 //---------------------------------------------------------------------
93 bool RegularExpression::Execute(llvm::StringRef str, Match *match) const {
95 if (m_comp_err == 0) {
96 // Argument to regexec must be null-terminated.
97 std::string reg_str = str;
99 err = ::regexec(&m_preg, reg_str.c_str(), match->GetSize(),
100 match->GetData(), 0);
102 err = ::regexec(&m_preg, reg_str.c_str(), 0, nullptr, 0);
107 // The regular expression didn't compile, so clear the matches
115 bool RegularExpression::Match::GetMatchAtIndex(llvm::StringRef s, uint32_t idx,
116 std::string &match_str) const {
117 llvm::StringRef match_str_ref;
118 if (GetMatchAtIndex(s, idx, match_str_ref)) {
119 match_str = match_str_ref.str();
125 bool RegularExpression::Match::GetMatchAtIndex(
126 llvm::StringRef s, uint32_t idx, llvm::StringRef &match_str) const {
127 if (idx < m_matches.size()) {
128 if (m_matches[idx].rm_eo == -1 && m_matches[idx].rm_so == -1)
131 if (m_matches[idx].rm_eo == m_matches[idx].rm_so) {
132 // Matched the empty string...
133 match_str = llvm::StringRef();
135 } else if (m_matches[idx].rm_eo > m_matches[idx].rm_so) {
136 match_str = s.substr(m_matches[idx].rm_so,
137 m_matches[idx].rm_eo - m_matches[idx].rm_so);
144 bool RegularExpression::Match::GetMatchSpanningIndices(
145 llvm::StringRef s, uint32_t idx1, uint32_t idx2,
146 llvm::StringRef &match_str) const {
147 if (idx1 < m_matches.size() && idx2 < m_matches.size()) {
148 if (m_matches[idx1].rm_so == m_matches[idx2].rm_eo) {
149 // Matched the empty string...
150 match_str = llvm::StringRef();
152 } else if (m_matches[idx1].rm_so < m_matches[idx2].rm_eo) {
153 match_str = s.substr(m_matches[idx1].rm_so,
154 m_matches[idx2].rm_eo - m_matches[idx1].rm_so);
161 //----------------------------------------------------------------------
162 // Returns true if the regular expression compiled and is ready
164 //----------------------------------------------------------------------
165 bool RegularExpression::IsValid() const { return m_comp_err == 0; }
167 //----------------------------------------------------------------------
168 // Returns the text that was used to compile the current regular
170 //----------------------------------------------------------------------
171 llvm::StringRef RegularExpression::GetText() const { return m_re; }
173 //----------------------------------------------------------------------
174 // Free any contained compiled regular expressions.
175 //----------------------------------------------------------------------
176 void RegularExpression::Free() {
177 if (m_comp_err == 0) {
180 // Set a compile error since we no longer have a valid regex
185 size_t RegularExpression::GetErrorAsCString(char *err_str,
186 size_t err_str_max_len) const {
187 if (m_comp_err == 0) {
188 if (err_str && err_str_max_len)
193 return ::regerror(m_comp_err, &m_preg, err_str, err_str_max_len);
196 bool RegularExpression::operator<(const RegularExpression &rhs) const {
197 return (m_re < rhs.m_re);