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