blob: aca1671c181bfefa9108d3c4cc00b691ed2d4674 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- RegularExpression.cpp -----------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/RegularExpression.h"
11
12using namespace lldb_private;
13
14//----------------------------------------------------------------------
15// Default constructor
16//----------------------------------------------------------------------
17RegularExpression::RegularExpression() :
18 m_re(),
19 m_comp_err (1),
20 m_preg(),
21 m_matches()
22{
23 memset(&m_preg,0,sizeof(m_preg));
24}
25
26//----------------------------------------------------------------------
27// Constructor that compiles "re" using "flags" and stores the
28// resulting compiled regular expression into this object.
29//----------------------------------------------------------------------
30RegularExpression::RegularExpression(const char* re, int flags) :
31 m_re(),
32 m_comp_err (1),
33 m_preg()
34{
35 memset(&m_preg,0,sizeof(m_preg));
36 Compile(re, flags);
37}
38
39//----------------------------------------------------------------------
40// Destructor
41//
42// Any previosuly compiled regular expression contained in this
43// object will be freed.
44//----------------------------------------------------------------------
45RegularExpression::~RegularExpression()
46{
47 Free();
48}
49
50//----------------------------------------------------------------------
51// Compile a regular expression using the supplied regular
52// expression text and flags. The compied regular expression lives
53// in this object so that it can be readily used for regular
54// expression matches. Execute() can be called after the regular
55// expression is compiled. Any previosuly compiled regular
56// expression contained in this object will be freed.
57//
58// RETURNS
59// True of the refular expression compiles successfully, false
60// otherwise.
61//----------------------------------------------------------------------
62bool
63RegularExpression::Compile(const char* re, int flags)
64{
65 Free();
66 if (re && re[0])
67 {
68 m_re = re;
69 m_comp_err = ::regcomp (&m_preg, re, flags);
70 }
71 else
72 {
73 // No valid regular expression
74 m_comp_err = 1;
75 }
76
77 return m_comp_err == 0;
78}
79
80//----------------------------------------------------------------------
81// Execute a regular expression match using the compiled regular
82// expression that is already in this object against the match
83// string "s". If any parens are used for regular expression
84// matches "match_count" should indicate the number of regmatch_t
85// values that are present in "match_ptr". The regular expression
86// will be executed using the "execute_flags".
87//----------------------------------------------------------------------
88bool
89RegularExpression::Execute(const char* s, size_t num_matches, int execute_flags) const
90{
91 int match_result = 1;
92 if (m_comp_err == 0)
93 {
94 if (num_matches > 0)
95 m_matches.resize(num_matches + 1);
96 else
97 m_matches.clear();
98
99 match_result = ::regexec (&m_preg,
100 s,
101 m_matches.size(),
102 const_cast<regmatch_t *>(m_matches.data()),
103 execute_flags);
104 }
105 return match_result == 0;
106}
107
108bool
109RegularExpression::GetMatchAtIndex (const char* s, uint32_t idx, std::string& match_str) const
110{
111 if (idx <= m_preg.re_nsub && idx < m_matches.size())
112 {
113 match_str.assign (s + m_matches[idx].rm_so,
114 m_matches[idx].rm_eo - m_matches[idx].rm_so);
115 return true;
116 }
117 return false;
118}
119
120
121//----------------------------------------------------------------------
122// Returns true if the regular expression compiled and is ready
123// for execution.
124//----------------------------------------------------------------------
125bool
126RegularExpression::IsValid () const
127{
128 return m_comp_err == 0;
129}
130
131//----------------------------------------------------------------------
132// Returns the text that was used to compile the current regular
133// expression.
134//----------------------------------------------------------------------
135const char*
136RegularExpression::GetText () const
137{
138 return m_re.c_str();
139}
140
141//----------------------------------------------------------------------
142// Free any contained compiled regular expressions.
143//----------------------------------------------------------------------
144void
145RegularExpression::Free()
146{
147 if (m_comp_err == 0)
148 {
149 m_re.clear();
150 regfree(&m_preg);
151 // Set a compile error since we no longer have a valid regex
152 m_comp_err = 1;
153 }
154}