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/Core/RegularExpression.h"
11 #include "llvm/ADT/StringRef.h"
14 using namespace lldb_private;
16 //----------------------------------------------------------------------
17 // Default constructor
18 //----------------------------------------------------------------------
19 RegularExpression::RegularExpression() :
23 m_compile_flags(REG_EXTENDED)
25 memset(&m_preg,0,sizeof(m_preg));
28 //----------------------------------------------------------------------
29 // Constructor that compiles "re" using "flags" and stores the
30 // resulting compiled regular expression into this object.
31 //----------------------------------------------------------------------
32 RegularExpression::RegularExpression(const char* re, int flags) :
36 m_compile_flags(flags)
38 memset(&m_preg,0,sizeof(m_preg));
42 //----------------------------------------------------------------------
43 // Constructor that compiles "re" using "flags" and stores the
44 // resulting compiled regular expression into this object.
45 //----------------------------------------------------------------------
46 RegularExpression::RegularExpression(const char* re) :
50 m_compile_flags(REG_EXTENDED)
52 memset(&m_preg,0,sizeof(m_preg));
56 RegularExpression::RegularExpression(const RegularExpression &rhs)
58 memset(&m_preg,0,sizeof(m_preg));
59 Compile(rhs.GetText(), rhs.GetCompileFlags());
62 const RegularExpression &
63 RegularExpression::operator= (const RegularExpression &rhs)
67 Compile (rhs.GetText(), rhs.GetCompileFlags());
71 //----------------------------------------------------------------------
74 // Any previously compiled regular expression contained in this
75 // object will be freed.
76 //----------------------------------------------------------------------
77 RegularExpression::~RegularExpression()
82 //----------------------------------------------------------------------
83 // Compile a regular expression using the supplied regular
84 // expression text and flags. The compiled regular expression lives
85 // in this object so that it can be readily used for regular
86 // expression matches. Execute() can be called after the regular
87 // expression is compiled. Any previously compiled regular
88 // expression contained in this object will be freed.
91 // True if the regular expression compiles successfully, false
93 //----------------------------------------------------------------------
95 RegularExpression::Compile(const char* re)
97 return Compile (re, m_compile_flags);
101 RegularExpression::Compile(const char* re, int flags)
104 m_compile_flags = flags;
109 m_comp_err = ::regcomp (&m_preg, re, flags);
113 // No valid regular expression
117 return m_comp_err == 0;
120 //----------------------------------------------------------------------
121 // Execute a regular expression match using the compiled regular
122 // expression that is already in this object against the match
123 // string "s". If any parens are used for regular expression
124 // matches "match_count" should indicate the number of regmatch_t
125 // values that are present in "match_ptr". The regular expression
126 // will be executed using the "execute_flags".
127 //---------------------------------------------------------------------
129 RegularExpression::Execute(const char* s, Match *match, int execute_flags) const
132 if (s != NULL && m_comp_err == 0)
136 err = ::regexec (&m_preg,
144 err = ::regexec (&m_preg,
154 // The regular expression didn't compile, so clear the matches
163 RegularExpression::Match::GetMatchAtIndex (const char* s, uint32_t idx, std::string& match_str) const
165 llvm::StringRef match_str_ref;
166 if (GetMatchAtIndex(s, idx, match_str_ref))
168 match_str = std::move(match_str_ref.str());
175 RegularExpression::Match::GetMatchAtIndex (const char* s, uint32_t idx, llvm::StringRef& match_str) const
177 if (idx < m_matches.size())
179 if (m_matches[idx].rm_eo == -1 && m_matches[idx].rm_so == -1)
182 if (m_matches[idx].rm_eo == m_matches[idx].rm_so)
184 // Matched the empty string...
185 match_str = llvm::StringRef();
188 else if (m_matches[idx].rm_eo > m_matches[idx].rm_so)
190 match_str = llvm::StringRef (s + m_matches[idx].rm_so, m_matches[idx].rm_eo - m_matches[idx].rm_so);
198 RegularExpression::Match::GetMatchSpanningIndices (const char* s, uint32_t idx1, uint32_t idx2, llvm::StringRef& match_str) const
200 if (idx1 < m_matches.size() && idx2 < m_matches.size())
202 if (m_matches[idx1].rm_so == m_matches[idx2].rm_eo)
204 // Matched the empty string...
205 match_str = llvm::StringRef();
208 else if (m_matches[idx1].rm_so < m_matches[idx2].rm_eo)
210 match_str = llvm::StringRef (s + m_matches[idx1].rm_so, m_matches[idx2].rm_eo - m_matches[idx1].rm_so);
218 //----------------------------------------------------------------------
219 // Returns true if the regular expression compiled and is ready
221 //----------------------------------------------------------------------
223 RegularExpression::IsValid () const
225 return m_comp_err == 0;
228 //----------------------------------------------------------------------
229 // Returns the text that was used to compile the current regular
231 //----------------------------------------------------------------------
233 RegularExpression::GetText () const
240 //----------------------------------------------------------------------
241 // Free any contained compiled regular expressions.
242 //----------------------------------------------------------------------
244 RegularExpression::Free()
250 // Set a compile error since we no longer have a valid regex
256 RegularExpression::GetErrorAsCString (char *err_str, size_t err_str_max_len) const
260 if (err_str && err_str_max_len)
265 return ::regerror (m_comp_err, &m_preg, err_str, err_str_max_len);
269 RegularExpression::operator < (const RegularExpression& rhs) const
271 return (m_re < rhs.m_re);