Files
pip/libs/main/text/piregularexpression.h

124 lines
3.4 KiB
C++

/*! \file pistring.h
* \ingroup Text
* \brief
* \~english Regular expression
* \~russian Регулярное выражение
*/
/*
PIP - Platform Independent Primitives
Regular expression
Ivan Pelipenko peri4ko@yandex.ru
This program 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 3 of the License, or
(at your option) any later version.
This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef piregularexpression_h
#define piregularexpression_h
#include <pistring.h>
class PIP_EXPORT PIRegularExpression {
public:
enum Option {
None = 0x0,
CaseInsensitive = 0x01,
Singleline = 0x02,
Multiline = 0x04,
InvertedGreediness = 0x08,
Extended = 0x10
};
typedef PIFlags<Option> Options;
PIRegularExpression(const PIString & pattern = {}, Options opt = None);
PIRegularExpression(const PIRegularExpression & o);
PIRegularExpression & operator=(const PIRegularExpression & o);
~PIRegularExpression();
class PIP_EXPORT Matcher {
friend class PIRegularExpression;
public:
operator bool() const { return hasMatch(); }
bool hasMatch() const;
bool next();
PIStringList matchedStrings() const;
PIString matchedString(int index = 0) const;
int matchedStart(int index = 0) const;
int matchedSize(int index = 0) const;
PIString matchedString(const PIString & gname) const;
int matchedStart(const PIString & gname) const;
int matchedSize(const PIString & gname) const;
Matcher(Matcher &&) = default;
private:
Matcher(PIRegularExpression * p);
Matcher(const Matcher &) = default;
Matcher & operator=(const Matcher &) = default;
struct Group {
int index = 0;
int size = 0;
};
PIChar * subjectPtr() const;
bool has_match = false;
bool is_error = false;
PIVector<Group> groups;
PIRegularExpression * parent = nullptr;
PIString * subject = nullptr;
PIString subject_own;
size_t start_offset = 0;
};
PIString pattern() const { return pat_; }
Options options() const { return opt_; }
void setPattern(const PIString & pattern);
void setPattern(const PIString & pattern, Options opt);
bool isValid() const;
bool isNotValid() const { return !isValid(); }
PIString errorString() const;
int errorPosition() const;
int captureGroupsCount() const;
PIStringList captureGroupNames() const;
PIString captureGroupName(int index) const;
int captureGroupIndex(const PIString & gname) const;
Matcher match(const PIString & subject, size_t offset = 0);
Matcher match(PIString & subject, size_t offset = 0);
Matcher makeMatcher(const PIString & subject, size_t offset = 0);
Matcher makeMatcher(PIString & subject, size_t offset = 0);
static PIRegularExpression fromGlob(const PIString & pattern, Options opt = None);
static PIRegularExpression fromPOSIX(const PIString & pattern, Options opt = None);
private:
void convertFrom(const PIString & pattern, uint type, Options opt);
PRIVATE_DECLARATION(PIP_EXPORT)
PIString pat_, subj_own;
Options opt_;
};
#endif