middle of porting; unstable, don't checkout; refs #1

This commit is contained in:
Marc Wäckerlin
2011-12-10 11:24:11 +00:00
parent 4357fe3d9f
commit 599af2dbbf
129 changed files with 9474 additions and 0 deletions

54
src/mrw/regexp.cpp Normal file
View File

@@ -0,0 +1,54 @@
/** @file
$Id$
$Date$
$Author$
@copy © Marc Wäckerlin
@license LGPL, see file <a href="license.html">COPYING</a>
$Log$
Revision 1.2 2004/12/16 13:09:31 marc
possibility to evaluate and extract sub expressions
Revision 1.1 2004/12/14 20:20:30 marc
initial version
*/
#include <mrw/string.hpp>
#include <mrw/regexp.hpp>
#include <mrw/exception.hpp>
namespace mrw {
RegExp::RegExp(const std::string& pattern, bool hassub, int flags)
throw(std::exception):
_hassub(hassub) {
if (flags&nosub) throw mrw::invalid_argument("nosub");
if (regcomp(&_regex, pattern.c_str(), (_hassub?flags:(flags|nosub))))
throw mrw::invalid_argument(pattern);
}
RegExp::~RegExp() throw() {
regfree(&_regex);
}
bool RegExp::operator()(const std::string& text) throw(std::bad_exception) {
if (_hassub)
return !regexec(&_regex, (_text=text).c_str(), MAX_SUB, _sub, 0);
else
return !regexec(&_regex, text.c_str(), 0, 0, 0);
}
std::string RegExp::operator[](unsigned int n) const throw(std::exception) {
if (!_hassub)
throw mrw::invalid_argument("initialized with no sub expressions");
if (n>=MAX_SUB || _sub[n].rm_so<0 || _sub[n].rm_eo<0)
throw mrw::invalid_argument(mrw::string(n));
return _text.substr(_sub[n].rm_so, _sub[n].rm_eo-_sub[n].rm_so);
}
}