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 \d for
18 // digit, \s for space, and many more, but it isn't available everywhere.
19 //----------------------------------------------------------------------
20 #if defined(REG_ENHANCED)
21 #define DEFAULT_COMPILE_FLAGS (REG_ENHANCED | REG_EXTENDED)
23 #define DEFAULT_COMPILE_FLAGS (REG_EXTENDED)
26 using namespace lldb_private;
28 RegularExpression::RegularExpression() : m_re(), m_comp_err(1), m_preg() {
29 memset(&m_preg, 0, sizeof(m_preg));
32 //----------------------------------------------------------------------
33 // Constructor that compiles "re" using "flags" and stores the resulting
34 // compiled regular expression into this object.
35 //----------------------------------------------------------------------
36 RegularExpression::RegularExpression(llvm::StringRef str)
37 : m_re(), m_comp_err(1), m_preg() {
38 memset(&m_preg, 0, sizeof(m_preg));
42 RegularExpression::RegularExpression(const RegularExpression &rhs) {
43 memset(&m_preg, 0, sizeof(m_preg));
44 Compile(rhs.GetText());
47 const RegularExpression &RegularExpression::
48 operator=(const RegularExpression &rhs) {
50 Compile(rhs.GetText());
54 //----------------------------------------------------------------------
57 // Any previously compiled regular expression contained in this object will be
59 //----------------------------------------------------------------------
60 RegularExpression::~RegularExpression() { Free(); }
62 //----------------------------------------------------------------------
63 // Compile a regular expression using the supplied regular expression text and
64 // flags. The compiled regular expression lives in this object so that it can
65 // be readily used for regular expression matches. Execute() can be called
66 // after the regular expression is compiled. Any previously compiled regular
67 // expression contained in this object will be freed.
70 // True if the regular expression compiles successfully, false
72 //----------------------------------------------------------------------
73 bool RegularExpression::Compile(llvm::StringRef str) {
76 // regcomp() on darwin does not recognize "" as a valid regular expression,
77 // so we substitute it with an equivalent non-empty one.
78 m_re = str.empty() ? "()" : str;
79 m_comp_err = ::regcomp(&m_preg, m_re.c_str(), DEFAULT_COMPILE_FLAGS);
80 return m_comp_err == 0;
83 //----------------------------------------------------------------------
84 // Execute a regular expression match using the compiled regular expression
85 // that is already in this object against the match string "s". If any parens
86 // are used for regular expression matches "match_count" should indicate the
87 // number of regmatch_t values that are present in "match_ptr". The regular
88 // expression will be executed using the "execute_flags".
89 //---------------------------------------------------------------------
90 bool RegularExpression::Execute(llvm::StringRef str, Match *match) const {
92 if (m_comp_err == 0) {
93 // Argument to regexec must be null-terminated.
94 std::string reg_str = str;
96 err = ::regexec(&m_preg, reg_str.c_str(), match->GetSize(),
99 err = ::regexec(&m_preg, reg_str.c_str(), 0, nullptr, 0);
104 // The regular expression didn't compile, so clear the matches
112 bool RegularExpression::Match::GetMatchAtIndex(llvm::StringRef s, uint32_t idx,
113 std::string &match_str) const {
114 llvm::StringRef match_str_ref;
115 if (GetMatchAtIndex(s, idx, match_str_ref)) {
116 match_str = match_str_ref.str();
122 bool RegularExpression::Match::GetMatchAtIndex(
123 llvm::StringRef s, uint32_t idx, llvm::StringRef &match_str) const {
124 if (idx < m_matches.size()) {
125 if (m_matches[idx].rm_eo == -1 && m_matches[idx].rm_so == -1)
128 if (m_matches[idx].rm_eo == m_matches[idx].rm_so) {
129 // Matched the empty string...
130 match_str = llvm::StringRef();
132 } else if (m_matches[idx].rm_eo > m_matches[idx].rm_so) {
133 match_str = s.substr(m_matches[idx].rm_so,
134 m_matches[idx].rm_eo - m_matches[idx].rm_so);
141 bool RegularExpression::Match::GetMatchSpanningIndices(
142 llvm::StringRef s, uint32_t idx1, uint32_t idx2,
143 llvm::StringRef &match_str) const {
144 if (idx1 < m_matches.size() && idx2 < m_matches.size()) {
145 if (m_matches[idx1].rm_so == m_matches[idx2].rm_eo) {
146 // Matched the empty string...
147 match_str = llvm::StringRef();
149 } else if (m_matches[idx1].rm_so < m_matches[idx2].rm_eo) {
150 match_str = s.substr(m_matches[idx1].rm_so,
151 m_matches[idx2].rm_eo - m_matches[idx1].rm_so);
158 //----------------------------------------------------------------------
159 // Returns true if the regular expression compiled and is ready for execution.
160 //----------------------------------------------------------------------
161 bool RegularExpression::IsValid() const { return m_comp_err == 0; }
163 //----------------------------------------------------------------------
164 // Returns the text that was used to compile the current regular expression.
165 //----------------------------------------------------------------------
166 llvm::StringRef RegularExpression::GetText() const { return m_re; }
168 //----------------------------------------------------------------------
169 // Free any contained compiled regular expressions.
170 //----------------------------------------------------------------------
171 void RegularExpression::Free() {
172 if (m_comp_err == 0) {
175 // Set a compile error since we no longer have a valid regex
180 size_t RegularExpression::GetErrorAsCString(char *err_str,
181 size_t err_str_max_len) const {
182 if (m_comp_err == 0) {
183 if (err_str && err_str_max_len)
188 return ::regerror(m_comp_err, &m_preg, err_str, err_str_max_len);
191 bool RegularExpression::operator<(const RegularExpression &rhs) const {
192 return (m_re < rhs.m_re);