| re.hxx | | re.hxx | |
| // file : cutl/re.hxx | | // file : cutl/re.hxx | |
| // copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC | | // copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC | |
| // license : MIT; see accompanying LICENSE file | | // license : MIT; see accompanying LICENSE file | |
| | | | |
| #ifndef CUTL_RE_HXX | | #ifndef CUTL_RE_HXX | |
| #define CUTL_RE_HXX | | #define CUTL_RE_HXX | |
| | | | |
| #include <string> | | #include <string> | |
|
| #include <iosfwd> // std::ostream | | #include <ostream> | |
| | | | |
| #include <cutl/exception.hxx> | | #include <cutl/exception.hxx> | |
| #include <cutl/details/export.hxx> | | #include <cutl/details/export.hxx> | |
| | | | |
| namespace cutl | | namespace cutl | |
| { | | { | |
| namespace re | | namespace re | |
| { | | { | |
|
| struct LIBCUTL_EXPORT format: exception | | struct LIBCUTL_EXPORT format_base: exception | |
| { | | { | |
| virtual | | virtual | |
|
| ~format () throw (); | | ~format_base () throw (); | |
| | | | |
|
| format (std::string const& e, std::string const& d) | | format_base (std::string const& d): description_ (d) {} | |
| : regex_ (e), description_ (d) | | | |
| { | | | |
| } | | | |
| | | | |
| std::string const& | | | |
| regex () const | | | |
| { | | | |
| return regex_; | | | |
| } | | | |
| | | | |
| std::string const& | | std::string const& | |
| description () const | | description () const | |
| { | | { | |
| return description_; | | return description_; | |
| } | | } | |
| | | | |
| virtual char const* | | virtual char const* | |
| what () const throw (); | | what () const throw (); | |
| | | | |
|
| private: | | protected: | |
| std::string regex_; | | | |
| std::string description_; | | std::string description_; | |
| }; | | }; | |
| | | | |
|
| | | template <typename C> | |
| | | struct basic_format: format_base | |
| | | { | |
| | | virtual | |
| | | ~basic_format () throw () {} | |
| | | | |
| | | basic_format (std::basic_string<C> const& e, std::string const& d) | |
| | | : format_base (d), regex_ (e) {} | |
| | | | |
| | | std::basic_string<C> const& | |
| | | regex () const | |
| | | { | |
| | | return regex_; | |
| | | } | |
| | | | |
| | | private: | |
| | | std::basic_string<C> regex_; | |
| | | }; | |
| | | | |
| | | typedef basic_format<char> format; | |
| | | typedef basic_format<wchar_t> wformat; | |
| | | | |
| // Regular expression pattern. | | // Regular expression pattern. | |
| // | | // | |
|
| struct LIBCUTL_EXPORT regex | | template <typename C> | |
| | | struct basic_regex | |
| { | | { | |
|
| ~regex (); | | typedef std::basic_string<C> string_type; | |
| | | | |
|
| regex () | | ~basic_regex (); | |
| | | | |
| | | basic_regex (): impl_ (0) {init (0, false);} | |
| | | | |
| | | explicit | |
| | | basic_regex (string_type const& s, bool icase = false) | |
| : impl_ (0) | | : impl_ (0) | |
| { | | { | |
|
| init (0); | | init (&s, icase); | |
| } | | } | |
| | | | |
|
| explicit | | basic_regex& | |
| regex (std::string const& s) | | operator= (string_type const& s) | |
| : impl_ (0) | | | |
| { | | { | |
|
| init (&s); | | init (&s, false); | |
| | | return *this; | |
| } | | } | |
| | | | |
|
| regex& | | basic_regex& | |
| operator= (std::string const& s) | | assign (string_type const& s, bool icase = false) | |
| { | | { | |
|
| init (&s); | | init (&s, icase); | |
| return *this; | | return *this; | |
| } | | } | |
| | | | |
|
| regex (regex const&); | | basic_regex (basic_regex const&); | |
| | | | |
|
| regex& | | basic_regex& | |
| operator= (regex const&); | | operator= (basic_regex const&); | |
| | | | |
| public: | | public: | |
| bool | | bool | |
|
| match (std::string const&) const; | | match (string_type const&) const; | |
| | | | |
| bool | | bool | |
|
| search (std::string const&) const; | | search (string_type const&) const; | |
| | | | |
|
| std::string | | string_type | |
| replace (std::string const& s, | | replace (string_type const& s, | |
| std::string const& sub, | | string_type const& sub, | |
| bool first_only = false) const; | | bool first_only = false) const; | |
| | | | |
| public: | | public: | |
|
| std::string | | string_type const& | |
| str () const; | | str () const | |
| | | { | |
| | | return str_; | |
| | | } | |
| | | | |
| bool | | bool | |
|
| empty () const; | | empty () const | |
| | | { | |
| | | return str_.empty (); | |
| | | } | |
| | | | |
| private: | | private: | |
| void | | void | |
|
| init (std::string const*); | | init (string_type const*, bool); | |
| | | | |
| private: | | private: | |
| struct impl; | | struct impl; | |
|
| | | | |
| | | string_type str_; // Text representation of regex. | |
| impl* impl_; | | impl* impl_; | |
| }; | | }; | |
| | | | |
|
| LIBCUTL_EXPORT std::ostream& | | template <typename C> | |
| operator<< (std::ostream&, regex const&); | | inline std::basic_ostream<C>& | |
| | | operator<< (std::basic_ostream<C>& os, basic_regex<C> const& r) | |
| | | { | |
| | | return os << r.str (); | |
| | | } | |
| | | | |
| | | typedef basic_regex<char> regex; | |
| | | typedef basic_regex<wchar_t> wregex; | |
| | | | |
| // Regular expression pattern and substituation. | | // Regular expression pattern and substituation. | |
| // | | // | |
|
| struct LIBCUTL_EXPORT regexsub | | template <typename C> | |
| | | struct basic_regexsub | |
| { | | { | |
|
| typedef re::regex regex_type; | | typedef basic_regex<C> regex_type; | |
| | | typedef std::basic_string<C> string_type; | |
| | | | |
|
| regexsub () | | basic_regexsub () {} | |
| { | | | |
| } | | | |
| | | | |
| // Expression is of the form /regex/substitution/ where '/' can | | // Expression is of the form /regex/substitution/ where '/' can | |
| // be replaced with any delimiter. Delimiters must be escaped in | | // be replaced with any delimiter. Delimiters must be escaped in | |
| // regex and substitution using back slashes (e.g., "\/"). Back | | // regex and substitution using back slashes (e.g., "\/"). Back | |
| // slashes themselves can be escaped using the double back slash | | // slashes themselves can be escaped using the double back slash | |
| // sequence (e.g., "\\"). | | // sequence (e.g., "\\"). | |
| // | | // | |
| explicit | | explicit | |
|
| regexsub (std::string const& e) | | basic_regexsub (string_type const& e) {init (e);} | |
| { | | | |
| init (e); | | | |
| } | | | |
| | | | |
|
| regexsub (std::string const& regex, std::string const& sub) | | basic_regexsub (string_type const& regex, string_type const& sub) | |
| : regex_ (regex), sub_ (sub) | | : regex_ (regex), sub_ (sub) | |
| { | | { | |
| } | | } | |
| | | | |
|
| regexsub (regex_type const& regex, std::string const& sub) | | basic_regexsub (regex_type const& regex, string_type const& sub) | |
| : regex_ (regex), sub_ (sub) | | : regex_ (regex), sub_ (sub) | |
| { | | { | |
| } | | } | |
| | | | |
|
| regexsub& | | basic_regexsub& | |
| operator= (std::string const& e) | | operator= (string_type const& e) | |
| { | | { | |
| init (e); | | init (e); | |
| return *this; | | return *this; | |
| } | | } | |
| | | | |
| public: | | public: | |
| bool | | bool | |
|
| match (std::string const& s) const | | match (string_type const& s) const | |
| { | | { | |
| return regex_.match (s); | | return regex_.match (s); | |
| } | | } | |
| | | | |
| bool | | bool | |
|
| search (std::string const& s) const | | search (string_type const& s) const | |
| { | | { | |
| return regex_.search (s); | | return regex_.search (s); | |
| } | | } | |
| | | | |
|
| std::string | | string_type | |
| replace (std::string const& s, bool first_only = false) const | | replace (string_type const& s, bool first_only = false) const | |
| { | | { | |
| return regex_.replace (s, sub_, first_only); | | return regex_.replace (s, sub_, first_only); | |
| } | | } | |
| | | | |
| public: | | public: | |
| const regex_type& | | const regex_type& | |
| regex () const | | regex () const | |
| { | | { | |
| return regex_; | | return regex_; | |
| } | | } | |
| | | | |
|
| const std::string& | | const string_type& | |
| substitution () const | | substitution () const | |
| { | | { | |
| return sub_; | | return sub_; | |
| } | | } | |
| | | | |
| bool | | bool | |
| empty () const | | empty () const | |
| { | | { | |
| return sub_.empty () && regex_.empty (); | | return sub_.empty () && regex_.empty (); | |
| } | | } | |
| | | | |
| private: | | private: | |
| void | | void | |
|
| init (std::string const&); | | init (string_type const&); | |
| | | | |
| private: | | private: | |
| regex_type regex_; | | regex_type regex_; | |
|
| std::string sub_; | | string_type sub_; | |
| }; | | }; | |
| | | | |
|
| | | typedef basic_regexsub<char> regexsub; | |
| | | typedef basic_regexsub<wchar_t> wregexsub; | |
| | | | |
| // Once-off regex execution. | | // Once-off regex execution. | |
| // | | // | |
|
| | | template <typename C> | |
| inline bool | | inline bool | |
|
| match (std::string const& s, std::string const& regex) | | match (std::basic_string<C> const& s, std::basic_string<C> const& regex
) | |
| { | | { | |
|
| re::regex r (regex); | | basic_regex<C> r (regex); | |
| return r.match (s); | | return r.match (s); | |
| } | | } | |
| | | | |
|
| | | template <typename C> | |
| inline bool | | inline bool | |
|
| search (std::string const& s, std::string const& regex) | | search (std::basic_string<C> const& s, std::basic_string<C> const& rege
x) | |
| { | | { | |
|
| re::regex r (regex); | | basic_regex<C> r (regex); | |
| return r.search (s); | | return r.search (s); | |
| } | | } | |
| | | | |
|
| inline std::string | | template <typename C> | |
| replace (std::string const& s, | | inline std::basic_string<C> | |
| std::string const& regex, | | replace (std::basic_string<C> const& s, | |
| std::string const& sub, | | std::basic_string<C> const& regex, | |
| | | std::basic_string<C> const& sub, | |
| bool first_only = false) | | bool first_only = false) | |
| { | | { | |
|
| re::regex r (regex); | | basic_regex<C> r (regex); | |
| return r.replace (s, sub, first_only); | | return r.replace (s, sub, first_only); | |
| } | | } | |
| | | | |
|
| inline std::string | | template <typename C> | |
| replace (std::string const& s, | | inline std::basic_string<C> | |
| std::string const& regexsub, // /regex/subst/ | | replace (std::basic_string<C> const& s, | |
| | | std::basic_string<C> const& regexsub, // /regex/subst/ | |
| bool first_only = false) | | bool first_only = false) | |
| { | | { | |
|
| re::regexsub r (regexsub); | | basic_regexsub<C> r (regexsub); | |
| return r.replace (s, first_only); | | return r.replace (s, first_only); | |
| } | | } | |
| | | | |
| // Utility function for parsing expressions in the form /regex/subst/ | | // Utility function for parsing expressions in the form /regex/subst/ | |
| // where '/' can be replaced with any delimiter. This function handles | | // where '/' can be replaced with any delimiter. This function handles | |
| // escaping. It return the position of the next delimiter and stores | | // escaping. It return the position of the next delimiter and stores | |
| // the unescaped chunk in result or throws the format exception if | | // the unescaped chunk in result or throws the format exception if | |
| // the expression is invalid. | | // the expression is invalid. | |
| // | | // | |
|
| LIBCUTL_EXPORT std::string::size_type | | template <typename C> | |
| parse (std::string const& s, | | typename std::basic_string<C>::size_type | |
| std::string::size_type start, | | parse (std::basic_string<C> const& s, | |
| std::string& result); | | typename std::basic_string<C>::size_type start, | |
| | | std::basic_string<C>& result); | |
| } | | } | |
| } | | } | |
| | | | |
|
| | | #include <cutl/re/re.txx> | |
| | | | |
| #endif // CUTL_RE_HXX | | #endif // CUTL_RE_HXX | |
| | | | |
End of changes. 50 change blocks. |
| 77 lines changed or deleted | | 117 lines changed or added | |
|