1 //===-- RegularExpression.cpp -----------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/RegularExpression.h"
11 #include "llvm/ADT/StringRef.h"
15 // Enable enhanced mode if it is available. This allows for things like \d for
16 // digit, \s for space, and many more, but it isn't available everywhere.
17 #if defined(REG_ENHANCED)
18 #define DEFAULT_COMPILE_FLAGS (REG_ENHANCED | REG_EXTENDED)
20 #define DEFAULT_COMPILE_FLAGS (REG_EXTENDED)
23 using namespace lldb_private;
25 RegularExpression::RegularExpression() : m_re(), m_comp_err(1), m_preg() {
26 memset(&m_preg, 0, sizeof(m_preg));
29 // Constructor that compiles "re" using "flags" and stores the resulting
30 // compiled regular expression into this object.
31 RegularExpression::RegularExpression(llvm::StringRef str)
32 : RegularExpression() {
36 RegularExpression::RegularExpression(const RegularExpression &rhs)
37 : RegularExpression() {
38 Compile(rhs.GetText());
41 const RegularExpression &RegularExpression::
42 operator=(const RegularExpression &rhs) {
44 Compile(rhs.GetText());
50 // Any previously compiled regular expression contained in this object will be
52 RegularExpression::~RegularExpression() { Free(); }
54 // Compile a regular expression using the supplied regular expression text and
55 // flags. The compiled regular expression lives in this object so that it can
56 // be readily used for regular expression matches. Execute() can be called
57 // after the regular expression is compiled. Any previously compiled regular
58 // expression contained in this object will be freed.
61 // True if the regular expression compiles successfully, false
63 bool RegularExpression::Compile(llvm::StringRef str) {
66 // regcomp() on darwin does not recognize "" as a valid regular expression,
67 // so we substitute it with an equivalent non-empty one.
68 m_re = str.empty() ? "()" : str;
69 m_comp_err = ::regcomp(&m_preg, m_re.c_str(), DEFAULT_COMPILE_FLAGS);
70 return m_comp_err == 0;
73 // Execute a regular expression match using the compiled regular expression
74 // that is already in this object against the match string "s". If any parens
75 // are used for regular expression matches "match_count" should indicate the
76 // number of regmatch_t values that are present in "match_ptr". The regular
77 // expression will be executed using the "execute_flags".
78 bool RegularExpression::Execute(llvm::StringRef str, Match *match) const {
80 if (m_comp_err == 0) {
81 // Argument to regexec must be null-terminated.
82 std::string reg_str = str;
84 err = ::regexec(&m_preg, reg_str.c_str(), match->GetSize(),
87 err = ::regexec(&m_preg, reg_str.c_str(), 0, nullptr, 0);
92 // The regular expression didn't compile, so clear the matches
100 bool RegularExpression::Match::GetMatchAtIndex(llvm::StringRef s, uint32_t idx,
101 std::string &match_str) const {
102 llvm::StringRef match_str_ref;
103 if (GetMatchAtIndex(s, idx, match_str_ref)) {
104 match_str = match_str_ref.str();
110 bool RegularExpression::Match::GetMatchAtIndex(
111 llvm::StringRef s, uint32_t idx, llvm::StringRef &match_str) const {
112 if (idx < m_matches.size()) {
113 if (m_matches[idx].rm_eo == -1 && m_matches[idx].rm_so == -1)
116 if (m_matches[idx].rm_eo == m_matches[idx].rm_so) {
117 // Matched the empty string...
118 match_str = llvm::StringRef();
120 } else if (m_matches[idx].rm_eo > m_matches[idx].rm_so) {
121 match_str = s.substr(m_matches[idx].rm_so,
122 m_matches[idx].rm_eo - m_matches[idx].rm_so);
129 bool RegularExpression::Match::GetMatchSpanningIndices(
130 llvm::StringRef s, uint32_t idx1, uint32_t idx2,
131 llvm::StringRef &match_str) const {
132 if (idx1 < m_matches.size() && idx2 < m_matches.size()) {
133 if (m_matches[idx1].rm_so == m_matches[idx2].rm_eo) {
134 // Matched the empty string...
135 match_str = llvm::StringRef();
137 } else if (m_matches[idx1].rm_so < m_matches[idx2].rm_eo) {
138 match_str = s.substr(m_matches[idx1].rm_so,
139 m_matches[idx2].rm_eo - m_matches[idx1].rm_so);
146 // Returns true if the regular expression compiled and is ready for execution.
147 bool RegularExpression::IsValid() const { return m_comp_err == 0; }
149 // Returns the text that was used to compile the current regular expression.
150 llvm::StringRef RegularExpression::GetText() const { return m_re; }
152 // Free any contained compiled regular expressions.
153 void RegularExpression::Free() {
154 if (m_comp_err == 0) {
157 // Set a compile error since we no longer have a valid regex
162 size_t RegularExpression::GetErrorAsCString(char *err_str,
163 size_t err_str_max_len) const {
164 if (m_comp_err == 0) {
165 if (err_str && err_str_max_len)
170 return ::regerror(m_comp_err, &m_preg, err_str, err_str_max_len);
173 bool RegularExpression::operator<(const RegularExpression &rhs) const {
174 return (m_re < rhs.m_re);