diff options
Diffstat (limited to 'src/regexp.cpp')
-rw-r--r-- | src/regexp.cpp | 78 |
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 | |||
5 | RegExp::RegExp() : | ||
6 | bCompiled( false ), | ||
7 | aSubStr( NULL ) | ||
8 | { | ||
9 | } | ||
10 | |||
11 | RegExp::RegExp( const char *sSrc ) : | ||
12 | bCompiled( false ), | ||
13 | aSubStr( NULL ) | ||
14 | { | ||
15 | compile( sSrc ); | ||
16 | } | ||
17 | |||
18 | RegExp::~RegExp() | ||
19 | { | ||
20 | if( bCompiled ) | ||
21 | { | ||
22 | regfree( &re ); | ||
23 | delete[] aSubStr; | ||
24 | } | ||
25 | } | ||
26 | |||
27 | void 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 | |||
53 | int RegExp::getNumSubStrings() | ||
54 | { | ||
55 | return nSubStr; | ||
56 | } | ||
57 | |||
58 | bool 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 | |||
66 | std::pair<int,int> RegExp::getSubStringRange( int nIndex ) | ||
67 | { | ||
68 | return std::pair<int,int>( aSubStr[nIndex].rm_so, aSubStr[nIndex].rm_eo ); | ||
69 | } | ||
70 | |||
71 | std::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 | |||