regexp.h
Go to the documentation of this file.
#ifndef WIBBLE_REGEXP_H
#define WIBBLE_REGEXP_H
/*
* OO wrapper for regular expression functions
*
* Copyright (C) 2003--2006 Enrico Zini <enrico@debian.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <wibble/exception.h>
#include <sys/types.h>
#include <regex.h>
namespace wibble {
namespace exception {
class Regexp : public wibble::exception::Generic
{
protected:
int m_code;
std::string m_message;
public:
Regexp(const regex_t& re, int code, const std::string& context)
throw ();
~Regexp() throw () {}
virtual int code() const throw () { return m_code; }
virtual const char* type() const throw () { return "Regexp"; }
virtual std::string desc() const throw () { return m_message; }
};
}
class Regexp
{
protected:
regex_t re;
regmatch_t* pmatch;
int nmatch;
std::string lastMatch;
public:
/* Note that match_count is required to be >1 to enable
sub-regexp capture. The maximum *INCLUDES* the whole-regexp
match (indexed 0). [TODO we may want to fix this to be more
friendly?] */
Regexp(const std::string& expr, int match_count = 0, int flags = 0);
~Regexp() throw ();
bool match(const std::string& str, int flags = 0);
/* Indexing is from 1 for capture matches, like perl's $0,
$1... 0 is whole-regexp match, not a capture. TODO
the range is miscalculated (an off-by-one, wrt. the
counterintuitive match counting). */
std::string operator[](int idx);
size_t matchStart(int idx);
size_t matchEnd(int idx);
size_t matchLength(int idx);
};
class ERegexp : public Regexp
{
public:
ERegexp(const std::string& expr, int match_count = 0, int flags = 0)
: Regexp(expr, match_count, flags | REG_EXTENDED) {}
};
class Tokenizer
{
const std::string& str;
wibble::Regexp re;
public:
class const_iterator
{
Tokenizer& tok;
size_t beg, end;
public:
typedef std::string value_type;
typedef ptrdiff_t difference_type;
typedef value_type *pointer;
typedef value_type &reference;
typedef std::forward_iterator_tag iterator_category;
const_iterator(Tokenizer& tok) : tok(tok), beg(0), end(0) { operator++(); }
const_iterator(Tokenizer& tok, bool) : tok(tok), beg(tok.str.size()), end(tok.str.size()) {}
const_iterator& operator++();
std::string operator*() const
{
return tok.str.substr(beg, end-beg);
}
bool operator==(const const_iterator& ti) const
{
return beg == ti.beg && end == ti.end;
}
bool operator!=(const const_iterator& ti) const
{
return beg != ti.beg || end != ti.end;
}
};
Tokenizer(const std::string& str, const std::string& re, int flags)
: str(str), re(re, 1, flags) {}
const_iterator begin() { return const_iterator(*this); }
const_iterator end() { return const_iterator(*this, false); }
};
class Splitter
{
wibble::Regexp re;
public:
// TODO: add iterator_traits
class const_iterator
{
wibble::Regexp& re;
std::string cur;
std::string next;
public:
typedef std::string value_type;
typedef ptrdiff_t difference_type;
typedef value_type *pointer;
typedef value_type &reference;
typedef std::forward_iterator_tag iterator_category;
const_iterator(wibble::Regexp& re, const std::string& str) : re(re), next(str) { ++*this; }
const_iterator(wibble::Regexp& re) : re(re) {}
const_iterator& operator++();
const std::string& operator*() const
{
return cur;
}
const std::string* operator->() const
{
return &cur;
}
bool operator==(const const_iterator& ti) const
{
return cur == ti.cur && next == ti.next;
}
bool operator!=(const const_iterator& ti) const
{
return cur != ti.cur || next != ti.next;
}
};
Splitter(const std::string& re, int flags)
: re(re, 1, flags) {}
const_iterator begin(const std::string& str) { return const_iterator(re, str); }
const_iterator end() { return const_iterator(re); }
};
}
// vim:set ts=4 sw=4:
#endif
wibble::ERegexp
Definition: regexp.h:83
wibble::ERegexp::ERegexp
ERegexp(const std::string &expr, int match_count=0, int flags=0)
Definition: regexp.h:85
wibble::Regexp
Definition: regexp.h:54
wibble::Regexp::matchLength
size_t matchLength(int idx)
Definition: regexp.cpp:118
wibble::Regexp::lastMatch
std::string lastMatch
Definition: regexp.h:59
wibble::Regexp::pmatch
regmatch_t * pmatch
Definition: regexp.h:57
wibble::Regexp::matchStart
size_t matchStart(int idx)
Definition: regexp.cpp:104
wibble::Regexp::re
regex_t re
Definition: regexp.h:56
wibble::Regexp::Regexp
Regexp(const std::string &expr, int match_count=0, int flags=0)
Definition: regexp.cpp:52
wibble::Regexp::nmatch
int nmatch
Definition: regexp.h:58
wibble::Regexp::matchEnd
size_t matchEnd(int idx)
Definition: regexp.cpp:111
wibble::Regexp::match
bool match(const std::string &str, int flags=0)
Definition: regexp.cpp:73
wibble::Regexp::~Regexp
~Regexp()
Definition: regexp.cpp:65
wibble::Splitter::const_iterator
Warning: the various iterators reuse the Regexps and therefore only one iteration of a Splitter can b...
Definition: regexp.h:156
wibble::Splitter::const_iterator::pointer
value_type * pointer
Definition: regexp.h:164
wibble::Splitter::const_iterator::value_type
std::string value_type
Definition: regexp.h:162
wibble::Splitter::const_iterator::operator!=
bool operator!=(const const_iterator &ti) const
Definition: regexp.h:185
wibble::Splitter::const_iterator::operator*
const std::string & operator*() const
Definition: regexp.h:173
wibble::Splitter::const_iterator::const_iterator
const_iterator(wibble::Regexp &re, const std::string &str)
Definition: regexp.h:168
wibble::Splitter::const_iterator::const_iterator
const_iterator(wibble::Regexp &re)
Definition: regexp.h:169
wibble::Splitter::const_iterator::operator->
const std::string * operator->() const
Definition: regexp.h:177
wibble::Splitter::const_iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: regexp.h:166
wibble::Splitter::const_iterator::operator==
bool operator==(const const_iterator &ti) const
Definition: regexp.h:181
wibble::Splitter::const_iterator::reference
value_type & reference
Definition: regexp.h:165
wibble::Splitter::const_iterator::difference_type
ptrdiff_t difference_type
Definition: regexp.h:163
wibble::Splitter
Split a string using a regular expression to match the token separators.
Definition: regexp.h:146
wibble::Splitter::Splitter
Splitter(const std::string &re, int flags)
Create a splitter that uses the given regular expression to find tokens.
Definition: regexp.h:194
wibble::Splitter::end
const_iterator end()
Definition: regexp.h:201
wibble::Splitter::begin
const_iterator begin(const std::string &str)
Split the string and iterate the resulting tokens.
Definition: regexp.h:200
wibble::Tokenizer::const_iterator
Definition: regexp.h:96
wibble::Tokenizer::const_iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: regexp.h:104
wibble::Tokenizer::const_iterator::value_type
std::string value_type
Definition: regexp.h:100
wibble::Tokenizer::const_iterator::const_iterator
const_iterator(Tokenizer &tok)
Definition: regexp.h:106
wibble::Tokenizer::const_iterator::reference
value_type & reference
Definition: regexp.h:103
wibble::Tokenizer::const_iterator::const_iterator
const_iterator(Tokenizer &tok, bool)
Definition: regexp.h:107
wibble::Tokenizer::const_iterator::pointer
value_type * pointer
Definition: regexp.h:102
wibble::Tokenizer::const_iterator::operator!=
bool operator!=(const const_iterator &ti) const
Definition: regexp.h:119
wibble::Tokenizer::const_iterator::operator==
bool operator==(const const_iterator &ti) const
Definition: regexp.h:115
wibble::Tokenizer::const_iterator::operator*
std::string operator*() const
Definition: regexp.h:111
wibble::Tokenizer::const_iterator::difference_type
ptrdiff_t difference_type
Definition: regexp.h:101
wibble::Tokenizer
Definition: regexp.h:90
wibble::Tokenizer::Tokenizer
Tokenizer(const std::string &str, const std::string &re, int flags)
Definition: regexp.h:125
wibble::Tokenizer::end
const_iterator end()
Definition: regexp.h:129
wibble::Tokenizer::begin
const_iterator begin()
Definition: regexp.h:128
wibble::exception::Context::context
const std::vector< std::string > & context() const
Definition: exception.h:166
wibble::exception::Generic
Base class for all exceptions.
Definition: exception.h:180
wibble::exception::Regexp
Definition: regexp.h:34
wibble::exception::Regexp::Regexp
Regexp(const regex_t &re, int code, const std::string &context)
Definition: regexp.cpp:31
wibble::exception::Regexp::m_code
int m_code
Definition: regexp.h:36
wibble::exception::Regexp::m_message
std::string m_message
Definition: regexp.h:37
wibble::exception::Regexp::desc
virtual std::string desc() const
Get a string describing what happened that threw the exception.
Definition: regexp.h:48
wibble::exception::Regexp::code
virtual int code() const
Get the regexp error code associated to the exception.
Definition: regexp.h:45
wibble::exception::Regexp::type
virtual const char * type() const
Get a string tag identifying the exception type.
Definition: regexp.h:47
wibble::exception::Regexp::~Regexp
~Regexp()
Definition: regexp.h:42
exception.h
wibble::list::end
ListIterator< List > end(List)
Definition: list.h:425
wibble::sys::fs::size
size_t size(const std::string &file)
File size.
Definition: fs.cpp:287
wibble
Definition: amorph.h:17