strusBase  0.17
regex.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
9 #ifndef _STRUS_BASE_REGEX_HPP_INCLUDED
10 #define _STRUS_BASE_REGEX_HPP_INCLUDED
11 #include <string>
12 
13 namespace strus {
14 
16 class ErrorBufferInterface;
17 
21 {
22 public:
23  struct Match
24  {
25  int pos;
26  int len;
27 
29  :pos(-1),len(0){}
30  Match( int pos_, int len_)
31  :pos(pos_),len(len_){}
32  Match( const Match& o)
33  :pos(o.pos),len(o.len){}
34  Match& operator=( const Match& o)
35  {pos=o.pos;len=o.len; return *this;}
36  bool valid() const {return pos>=0;}
37  };
38 
41  RegexSearch( const std::string& expression, int index, ErrorBufferInterface* errhnd_);
42 
44  ~RegexSearch();
45 
50  Match find( const char* begin, const char* end) const;
51 
56  int match_start( const char* begin, const char* end) const;
57 
58 private:
59 #if __cplusplus >= 201103L
60  RegexSearch( const RegexSearch&) = delete;
61  void operator=( const RegexSearch&) = delete;
62 #else
63  RegexSearch( const RegexSearch&){}
64  void operator=( const RegexSearch&){}
65 #endif
66 
67 private:
68  void* m_config; //< PIMPL for implementation
69  ErrorBufferInterface* m_errhnd; //< where to report errors
70 };
71 
72 
76 {
77 public:
81  RegexSubst( const std::string& expression, const std::string& fmtstring, ErrorBufferInterface* errhnd_);
83  ~RegexSubst();
84 
89  bool exec( std::string& out, const std::string& input) const;
90 
96  bool exec( std::string& out, const char* src, std::size_t srcsize) const;
97 
99  const char* error() const;
100 
101 private:
102 #if __cplusplus >= 201103L
103  RegexSubst( const RegexSubst&) = delete;
104  void operator=( const RegexSubst&) = delete;
105 #else
106  RegexSubst( const RegexSubst&){}
107  void operator=( const RegexSubst&){}
108 #endif
109 
110 private:
111  void reportError( const char* err);
112 
113  void* m_config; //< PIMPL for implementation
114  ErrorBufferInterface* m_errhnd; //< where to report errors
115 };
116 
117 } //namespace
118 #endif
119 
bool valid() const
Definition: regex.hpp:36
~RegexSearch()
Destructor.
Definition: regex.hpp:23
Match(int pos_, int len_)
Definition: regex.hpp:30
Match()
Definition: regex.hpp:28
Match find(const char *begin, const char *end) const
Execute the search.
~RegexSubst()
Destructor.
Interface for reporting and catching errors in modules.
Definition: errorBufferInterface.hpp:24
Match & operator=(const Match &o)
Definition: regex.hpp:34
RegexSubst(const std::string &expression, const std::string &fmtstring, ErrorBufferInterface *errhnd_)
Constructor.
const char * error() const
Class to replace a regular expression by a string with sub-matches as substitute. ...
Definition: regex.hpp:75
int len
length of the match in bytes
Definition: regex.hpp:26
RegexSearch(const std::string &expression, int index, ErrorBufferInterface *errhnd_)
Constructor.
bool exec(std::string &out, const std::string &input) const
Execute the replacement.
int pos
offset of the match in bytes from the start of the search or -1 if not found
Definition: regex.hpp:25
int match_start(const char *begin, const char *end) const
Match the regex to the start of the source.
Class to search for a regular expression.
Definition: regex.hpp:20
Match(const Match &o)
Definition: regex.hpp:32