aboutsummaryrefslogtreecommitdiff
path: root/src/regexp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/regexp.cpp')
-rw-r--r--src/regexp.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/regexp.cpp b/src/regexp.cpp
new file mode 100644
index 0000000..ff2d09a
--- /dev/null
+++ b/src/regexp.cpp
@@ -0,0 +1,71 @@
1#include "regexp.h"
2#include "builder.h" // For BuildException
3#include "staticstring.h"
4
5RegExp::RegExp() :
6 bCompiled( false ),
7 aSubStr( NULL )
8{
9}
10
11RegExp::RegExp( const char *sSrc ) :
12 bCompiled( false ),
13 aSubStr( NULL )
14{
15 compile( sSrc );
16}
17
18RegExp::~RegExp()
19{
20 regfree( &re );
21 delete[] aSubStr;
22}
23
24void RegExp::compile( const char *sSrc )
25{
26 if( bCompiled )
27 throw BuildException("Already compiled.");
28
29 int nErr = regcomp( &re, sSrc, REG_EXTENDED|REG_NEWLINE );
30 if( nErr )
31 {
32 size_t length = regerror( nErr, &re, NULL, 0 );
33 char *buffer = new char[length];
34 (void) regerror( nErr, &re, buffer, length );
35 StaticString s( buffer );
36 delete[] buffer;
37 throw BuildException( s.getString() );
38 }
39 bCompiled = true;
40 this->sSrc = sSrc;
41
42 nSubStr = re.re_nsub+1;
43 aSubStr = new regmatch_t[nSubStr];
44}
45
46int RegExp::getNumSubStrings()
47{
48 return nSubStr;
49}
50
51bool RegExp::execute( const char *sSrc )
52{
53 sTest = sSrc;
54 if( regexec( &re, sSrc, nSubStr, aSubStr, 0 ) )
55 return false;
56 return true;
57}
58
59std::pair<int,int> RegExp::getSubStringRange( int nIndex )
60{
61 return std::pair<int,int>( aSubStr[nIndex].rm_so, aSubStr[nIndex].rm_eo );
62}
63
64std::string RegExp::getSubString( int nIndex )
65{
66 return std::string(
67 sTest.getString()+aSubStr[nIndex].rm_so,
68 aSubStr[nIndex].rm_eo - aSubStr[nIndex].rm_so
69 );
70}
71