aboutsummaryrefslogtreecommitdiff
path: root/source/Core/RegularExpression.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/Core/RegularExpression.cpp')
-rw-r--r--source/Core/RegularExpression.cpp261
1 files changed, 104 insertions, 157 deletions
diff --git a/source/Core/RegularExpression.cpp b/source/Core/RegularExpression.cpp
index bdef3ced94d4..54e0231556db 100644
--- a/source/Core/RegularExpression.cpp
+++ b/source/Core/RegularExpression.cpp
@@ -25,46 +25,37 @@
// everywhere.
//----------------------------------------------------------------------
#if defined(REG_ENHANCED)
-#define DEFAULT_COMPILE_FLAGS (REG_ENHANCED|REG_EXTENDED)
+#define DEFAULT_COMPILE_FLAGS (REG_ENHANCED | REG_EXTENDED)
#else
#define DEFAULT_COMPILE_FLAGS (REG_EXTENDED)
#endif
using namespace lldb_private;
-RegularExpression::RegularExpression() :
- m_re(),
- m_comp_err (1),
- m_preg()
-{
- memset(&m_preg, 0, sizeof(m_preg));
+RegularExpression::RegularExpression() : m_re(), m_comp_err(1), m_preg() {
+ memset(&m_preg, 0, sizeof(m_preg));
}
//----------------------------------------------------------------------
// Constructor that compiles "re" using "flags" and stores the
// resulting compiled regular expression into this object.
//----------------------------------------------------------------------
-RegularExpression::RegularExpression(const char* re) :
- m_re(),
- m_comp_err (1),
- m_preg()
-{
- memset(&m_preg,0,sizeof(m_preg));
- Compile(re);
+RegularExpression::RegularExpression(llvm::StringRef str)
+ : m_re(), m_comp_err(1), m_preg() {
+ memset(&m_preg, 0, sizeof(m_preg));
+ Compile(str);
}
-RegularExpression::RegularExpression(const RegularExpression &rhs)
-{
- memset(&m_preg,0,sizeof(m_preg));
- Compile(rhs.GetText());
+RegularExpression::RegularExpression(const RegularExpression &rhs) {
+ memset(&m_preg, 0, sizeof(m_preg));
+ Compile(rhs.GetText());
}
-const RegularExpression &
-RegularExpression::operator= (const RegularExpression &rhs)
-{
- if (&rhs != this)
- Compile (rhs.GetText());
- return *this;
+const RegularExpression &RegularExpression::
+operator=(const RegularExpression &rhs) {
+ if (&rhs != this)
+ Compile(rhs.GetText());
+ return *this;
}
//----------------------------------------------------------------------
@@ -73,10 +64,7 @@ RegularExpression::operator= (const RegularExpression &rhs)
// Any previously compiled regular expression contained in this
// object will be freed.
//----------------------------------------------------------------------
-RegularExpression::~RegularExpression()
-{
- Free();
-}
+RegularExpression::~RegularExpression() { Free(); }
//----------------------------------------------------------------------
// Compile a regular expression using the supplied regular
@@ -90,23 +78,18 @@ RegularExpression::~RegularExpression()
// True if the regular expression compiles successfully, false
// otherwise.
//----------------------------------------------------------------------
-bool
-RegularExpression::Compile(const char* re)
-{
- Free();
-
- if (re && re[0])
- {
- m_re = re;
- m_comp_err = ::regcomp (&m_preg, re, DEFAULT_COMPILE_FLAGS);
- }
- else
- {
- // No valid regular expression
- m_comp_err = 1;
- }
-
- return m_comp_err == 0;
+bool RegularExpression::Compile(llvm::StringRef str) {
+ Free();
+
+ if (!str.empty()) {
+ m_re = str;
+ m_comp_err = ::regcomp(&m_preg, m_re.c_str(), DEFAULT_COMPILE_FLAGS);
+ } else {
+ // No valid regular expression
+ m_comp_err = 1;
+ }
+
+ return m_comp_err == 0;
}
//----------------------------------------------------------------------
@@ -117,145 +100,109 @@ RegularExpression::Compile(const char* re)
// values that are present in "match_ptr". The regular expression
// will be executed using the "execute_flags".
//---------------------------------------------------------------------
-bool
-RegularExpression::Execute (const char* s, Match *match) const
-{
- int err = 1;
- if (s != nullptr && m_comp_err == 0)
- {
- if (match)
- {
- err = ::regexec (&m_preg,
- s,
- match->GetSize(),
- match->GetData(),
- 0);
- }
- else
- {
- err = ::regexec(&m_preg,
- s,
- 0,
- nullptr,
- 0);
- }
- }
-
- if (err != 0)
- {
- // The regular expression didn't compile, so clear the matches
- if (match)
- match->Clear();
- return false;
+bool RegularExpression::Execute(llvm::StringRef str, Match *match) const {
+ int err = 1;
+ if (m_comp_err == 0) {
+ // Argument to regexec must be null-terminated.
+ std::string reg_str = str;
+ if (match) {
+ err = ::regexec(&m_preg, reg_str.c_str(), match->GetSize(),
+ match->GetData(), 0);
+ } else {
+ err = ::regexec(&m_preg, reg_str.c_str(), 0, nullptr, 0);
}
- return true;
-}
+ }
-bool
-RegularExpression::Match::GetMatchAtIndex (const char* s, uint32_t idx, std::string& match_str) const
-{
- llvm::StringRef match_str_ref;
- if (GetMatchAtIndex(s, idx, match_str_ref))
- {
- match_str = match_str_ref.str();
- return true;
- }
+ if (err != 0) {
+ // The regular expression didn't compile, so clear the matches
+ if (match)
+ match->Clear();
return false;
+ }
+ return true;
}
-bool
-RegularExpression::Match::GetMatchAtIndex (const char* s, uint32_t idx, llvm::StringRef& match_str) const
-{
- if (idx < m_matches.size())
- {
- if (m_matches[idx].rm_eo == -1 && m_matches[idx].rm_so == -1)
- return false;
+bool RegularExpression::Match::GetMatchAtIndex(llvm::StringRef s, uint32_t idx,
+ std::string &match_str) const {
+ llvm::StringRef match_str_ref;
+ if (GetMatchAtIndex(s, idx, match_str_ref)) {
+ match_str = match_str_ref.str();
+ return true;
+ }
+ return false;
+}
- if (m_matches[idx].rm_eo == m_matches[idx].rm_so)
- {
- // Matched the empty string...
- match_str = llvm::StringRef();
- return true;
- }
- else if (m_matches[idx].rm_eo > m_matches[idx].rm_so)
- {
- match_str = llvm::StringRef (s + m_matches[idx].rm_so, m_matches[idx].rm_eo - m_matches[idx].rm_so);
- return true;
- }
+bool RegularExpression::Match::GetMatchAtIndex(
+ llvm::StringRef s, uint32_t idx, llvm::StringRef &match_str) const {
+ if (idx < m_matches.size()) {
+ if (m_matches[idx].rm_eo == -1 && m_matches[idx].rm_so == -1)
+ return false;
+
+ if (m_matches[idx].rm_eo == m_matches[idx].rm_so) {
+ // Matched the empty string...
+ match_str = llvm::StringRef();
+ return true;
+ } else if (m_matches[idx].rm_eo > m_matches[idx].rm_so) {
+ match_str = s.substr(m_matches[idx].rm_so,
+ m_matches[idx].rm_eo - m_matches[idx].rm_so);
+ return true;
}
- return false;
+ }
+ return false;
}
-bool
-RegularExpression::Match::GetMatchSpanningIndices (const char* s, uint32_t idx1, uint32_t idx2, llvm::StringRef& match_str) const
-{
- if (idx1 < m_matches.size() && idx2 < m_matches.size())
- {
- if (m_matches[idx1].rm_so == m_matches[idx2].rm_eo)
- {
- // Matched the empty string...
- match_str = llvm::StringRef();
- return true;
- }
- else if (m_matches[idx1].rm_so < m_matches[idx2].rm_eo)
- {
- match_str = llvm::StringRef (s + m_matches[idx1].rm_so, m_matches[idx2].rm_eo - m_matches[idx1].rm_so);
- return true;
- }
+bool RegularExpression::Match::GetMatchSpanningIndices(
+ llvm::StringRef s, uint32_t idx1, uint32_t idx2,
+ llvm::StringRef &match_str) const {
+ if (idx1 < m_matches.size() && idx2 < m_matches.size()) {
+ if (m_matches[idx1].rm_so == m_matches[idx2].rm_eo) {
+ // Matched the empty string...
+ match_str = llvm::StringRef();
+ return true;
+ } else if (m_matches[idx1].rm_so < m_matches[idx2].rm_eo) {
+ match_str = s.substr(m_matches[idx1].rm_so,
+ m_matches[idx2].rm_eo - m_matches[idx1].rm_so);
+ return true;
}
- return false;
+ }
+ return false;
}
//----------------------------------------------------------------------
// Returns true if the regular expression compiled and is ready
// for execution.
//----------------------------------------------------------------------
-bool
-RegularExpression::IsValid () const
-{
- return m_comp_err == 0;
-}
+bool RegularExpression::IsValid() const { return m_comp_err == 0; }
//----------------------------------------------------------------------
// Returns the text that was used to compile the current regular
// expression.
//----------------------------------------------------------------------
-const char*
-RegularExpression::GetText () const
-{
- return (m_re.empty() ? nullptr : m_re.c_str());
-}
+llvm::StringRef RegularExpression::GetText() const { return m_re; }
//----------------------------------------------------------------------
// Free any contained compiled regular expressions.
//----------------------------------------------------------------------
-void
-RegularExpression::Free()
-{
- if (m_comp_err == 0)
- {
- m_re.clear();
- regfree(&m_preg);
- // Set a compile error since we no longer have a valid regex
- m_comp_err = 1;
- }
+void RegularExpression::Free() {
+ if (m_comp_err == 0) {
+ m_re.clear();
+ regfree(&m_preg);
+ // Set a compile error since we no longer have a valid regex
+ m_comp_err = 1;
+ }
}
-size_t
-RegularExpression::GetErrorAsCString (char *err_str, size_t err_str_max_len) const
-{
- if (m_comp_err == 0)
- {
- if (err_str && err_str_max_len)
- *err_str = '\0';
- return 0;
- }
-
- return ::regerror (m_comp_err, &m_preg, err_str, err_str_max_len);
+size_t RegularExpression::GetErrorAsCString(char *err_str,
+ size_t err_str_max_len) const {
+ if (m_comp_err == 0) {
+ if (err_str && err_str_max_len)
+ *err_str = '\0';
+ return 0;
+ }
+
+ return ::regerror(m_comp_err, &m_preg, err_str, err_str_max_len);
}
-bool
-RegularExpression::operator < (const RegularExpression& rhs) const
-{
- return (m_re < rhs.m_re);
+bool RegularExpression::operator<(const RegularExpression &rhs) const {
+ return (m_re < rhs.m_re);
}