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 previosuly 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 compied 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 previosuly compiled regular
88 // expression contained in this object will be freed.
91 // True of the refular 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 if (idx < m_matches.size())
167 if (m_matches[idx].rm_eo == m_matches[idx].rm_so)
169 // Matched the empty string...
173 else if (m_matches[idx].rm_eo > m_matches[idx].rm_so)
175 match_str.assign (s + m_matches[idx].rm_so,
176 m_matches[idx].rm_eo - m_matches[idx].rm_so);
184 RegularExpression::Match::GetMatchAtIndex (const char* s, uint32_t idx, llvm::StringRef& match_str) const
186 if (idx < m_matches.size())
188 if (m_matches[idx].rm_eo == m_matches[idx].rm_so)
190 // Matched the empty string...
191 match_str = llvm::StringRef();
194 else if (m_matches[idx].rm_eo > m_matches[idx].rm_so)
196 match_str = llvm::StringRef (s + m_matches[idx].rm_so, m_matches[idx].rm_eo - m_matches[idx].rm_so);
204 RegularExpression::Match::GetMatchSpanningIndices (const char* s, uint32_t idx1, uint32_t idx2, llvm::StringRef& match_str) const
206 if (idx1 < m_matches.size() && idx2 < m_matches.size())
208 if (m_matches[idx1].rm_so == m_matches[idx2].rm_eo)
210 // Matched the empty string...
211 match_str = llvm::StringRef();
214 else if (m_matches[idx1].rm_so < m_matches[idx2].rm_eo)
216 match_str = llvm::StringRef (s + m_matches[idx1].rm_so, m_matches[idx2].rm_eo - m_matches[idx1].rm_so);
224 //----------------------------------------------------------------------
225 // Returns true if the regular expression compiled and is ready
227 //----------------------------------------------------------------------
229 RegularExpression::IsValid () const
231 return m_comp_err == 0;
234 //----------------------------------------------------------------------
235 // Returns the text that was used to compile the current regular
237 //----------------------------------------------------------------------
239 RegularExpression::GetText () const
246 //----------------------------------------------------------------------
247 // Free any contained compiled regular expressions.
248 //----------------------------------------------------------------------
250 RegularExpression::Free()
256 // Set a compile error since we no longer have a valid regex
262 RegularExpression::GetErrorAsCString (char *err_str, size_t err_str_max_len) const
266 if (err_str && err_str_max_len)
271 return ::regerror (m_comp_err, &m_preg, err_str, err_str_max_len);
275 RegularExpression::operator < (const RegularExpression& rhs) const
277 return (m_re < rhs.m_re);