1
/*************************************************************************/
3
/* Regexx - Regular Expressions C++ solution. */
5
/* http://projects.nn.com.br/ */
7
/* Copyright (C) 2000 Gustavo Niemeyer <gustavo@nn.com.br> */
9
/* This library is free software; you can redistribute it and/or */
10
/* modify it under the terms of the GNU Library General Public */
11
/* License as published by the Free Software Foundation; either */
12
/* version 2 of the License, or (at your option) any later version. */
14
/* This library is distributed in the hope that it will be useful, */
15
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
17
/* Library General Public License for more details. */
19
/* You should have received a copy of the GNU Library General Public */
20
/* License along with this library; if not, write to the */
21
/* Free Software Foundation, Inc., 59 Temple Place - Suite 330, */
22
/* Boston, MA 02111-1307, USA. */
24
/*************************************************************************/
27
// $Date: 2003/08/04 03:54:01 $
32
regexx::Regexx::exec(int _flags)
33
throw(CompileException)
39
((_flags&nocase)?PCRE_CASELESS:0)
40
| ((_flags&newline)?PCRE_MULTILINE:0);
41
m_preg = pcre_compile(m_expr.c_str(),cflags,&errptr,&erroffset,0);
43
throw CompileException(errptr);
45
pcre_fullinfo(m_preg, NULL, PCRE_INFO_CAPTURECOUNT, (void*)&m_capturecount);
49
if(!m_study && (_flags&study)) {
51
m_extra = pcre_study(m_preg, 0, &errptr);
53
throw CompileException(errptr);
59
int eflags = ((_flags¬bol)?PCRE_NOTBOL:0) | ((_flags¬eol)?PCRE_NOTEOL:0);
65
ssc = pcre_exec(m_preg,m_extra,m_str.c_str(),m_str.length(),0,eflags,ssv,33);
72
ret = (pcre_exec(m_preg,m_extra,m_str.c_str(),m_str.length(),ssv[1],eflags,ssv,33) > 0);
74
else if(_flags&noatom)
77
match.push_back(RegexxMatch(m_str,ssv[0],ssv[1]-ssv[0]));
78
ret = (pcre_exec(m_preg,m_extra,m_str.c_str(),m_str.length(),ssv[1],eflags,ssv,33) > 0);
83
match.push_back(RegexxMatch(m_str,ssv[0],ssv[1]-ssv[0]));
84
match.back().atom.reserve(m_capturecount);
85
for(int i = 1; i < ssc; i++) {
87
match.back().atom.push_back(RegexxMatchAtom(m_str,ssv[i*2],ssv[(i*2)+1]-ssv[i*2]));
89
match.back().atom.push_back(RegexxMatchAtom(m_str,0,0));
91
ret = (pcre_exec(m_preg,m_extra,m_str.c_str(),m_str.length(),ssv[1],eflags,ssv,33) > 0);
99
else if(_flags&noatom) {
102
match.push_back(RegexxMatch(m_str,ssv[0],ssv[1]-ssv[0]));
108
match.push_back(RegexxMatch(m_str,ssv[0],ssv[1]-ssv[0]));
109
match.back().atom.reserve(m_capturecount);
110
for(int i = 1; i < ssc; i++) {
112
match.back().atom.push_back(RegexxMatchAtom(m_str,ssv[i*2],ssv[(i*2)+1]-ssv[i*2]));
114
match.back().atom.push_back(RegexxMatchAtom(m_str,0,0));
116
ret = (pcre_exec(m_preg,m_extra,m_str.c_str(),m_str.length(),ssv[1],eflags,ssv,33) > 0);
124
regexx::Regexx::replace(const std::string& _repstr, int _flags)
125
throw(CompileException)
127
exec(_flags&~nomatch);
128
std::vector< std::pair<unsigned int,std::string::size_type> > v;
129
v.reserve(m_capturecount);
130
std::string::size_type pos = _repstr.find("%");
131
while(pos != std::string::npos) {
132
if(_repstr[pos-1] != '%'
133
&& _repstr[pos+1] >= '0'
134
&& _repstr[pos+1] <= '9') {
135
v.push_back(std::pair<unsigned int,std::string::size_type>(_repstr[pos+1]-'0',pos));
137
pos = _repstr.find("%",pos+1);
140
std::vector<RegexxMatch>::reverse_iterator m;
141
std::vector< std::pair<unsigned int,std::string::size_type> >::reverse_iterator i;
142
for(m = match.rbegin(); m != match.rend(); m++) {
143
std::string tmprep = _repstr;
144
for(i = v.rbegin(); i != v.rend(); i++) {
145
if(i->first < m->atom.size())
146
tmprep.replace(i->second,2,m->atom[i->first]);
148
tmprep.erase(i->second,2);
150
m_replaced.replace(m->start(),m->length(),tmprep);