From 5a6ea019fd48b269121f95dc4afabce760531bbe Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 9 May 2012 11:49:37 +0200 Subject: Make regex class template --- cutl/re.hxx | 177 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 99 insertions(+), 78 deletions(-) (limited to 'cutl/re.hxx') diff --git a/cutl/re.hxx b/cutl/re.hxx index 3c47852..7638bb4 100644 --- a/cutl/re.hxx +++ b/cutl/re.hxx @@ -6,7 +6,7 @@ #define CUTL_RE_HXX #include -#include // std::ostream +#include #include #include @@ -15,21 +15,12 @@ namespace cutl { namespace re { - struct LIBCUTL_EXPORT format: exception + struct LIBCUTL_EXPORT format_base: exception { virtual - ~format () throw (); + ~format_base () throw (); - format (std::string const& e, std::string const& d) - : regex_ (e), description_ (d) - { - } - - std::string const& - regex () const - { - return regex_; - } + format_base (std::string const& d): description_ (d) {} std::string const& description () const @@ -40,56 +31,72 @@ namespace cutl virtual char const* what () const throw (); - private: - std::string regex_; + protected: std::string description_; }; - // Regular expression pattern. - // - struct LIBCUTL_EXPORT regex + template + struct basic_format: format_base { - ~regex (); + virtual + ~basic_format () throw () {} + + basic_format (std::basic_string const& e, std::string const& d) + : format_base (d), regex_ (e) {} - regex () - : impl_ (0) + std::basic_string const& + regex () const { - init (0); + return regex_; } + private: + std::basic_string regex_; + }; + + typedef basic_format format; + typedef basic_format wformat; + + // Regular expression pattern. + // + template + struct basic_regex + { + typedef std::basic_string string_type; + + ~basic_regex (); + + basic_regex (): impl_ (0) {init (0);} + explicit - regex (std::string const& s) - : impl_ (0) - { - init (&s); - } + basic_regex (string_type const& s): impl_ (0) {init (&s);} - regex& - operator= (std::string const& s) + basic_regex& + operator= (string_type const& s) { init (&s); return *this; } - regex (regex const&); + basic_regex (basic_regex const&); - regex& - operator= (regex const&); + basic_regex& + operator= (basic_regex const&); public: bool - match (std::string const&) const; + match (string_type const&) const; bool - search (std::string const&) const; + search (string_type const&) const; - std::string - replace (std::string const& s, - std::string const& sub, + string_type + replace (string_type const& s, + string_type const& sub, bool first_only = false) const; public: - std::string + string_type str () const; bool @@ -97,25 +104,32 @@ namespace cutl private: void - init (std::string const*); + init (string_type const*); private: struct impl; impl* impl_; }; - LIBCUTL_EXPORT std::ostream& - operator<< (std::ostream&, regex const&); + template + inline std::basic_ostream& + operator<< (std::basic_ostream& os, basic_regex const& r) + { + return os << r.str (); + } + + typedef basic_regex regex; + typedef basic_regex wregex; // Regular expression pattern and substituation. // - struct LIBCUTL_EXPORT regexsub + template + struct basic_regexsub { - typedef re::regex regex_type; + typedef basic_regex regex_type; + typedef std::basic_string string_type; - regexsub () - { - } + basic_regexsub () {} // Expression is of the form /regex/substitution/ where '/' can // be replaced with any delimiter. Delimiters must be escaped in @@ -124,23 +138,20 @@ namespace cutl // sequence (e.g., "\\"). // explicit - regexsub (std::string const& e) - { - init (e); - } + basic_regexsub (string_type const& 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) { } - regexsub (regex_type const& regex, std::string const& sub) + basic_regexsub (regex_type const& regex, string_type const& sub) : regex_ (regex), sub_ (sub) { } - regexsub& - operator= (std::string const& e) + basic_regexsub& + operator= (string_type const& e) { init (e); return *this; @@ -148,19 +159,19 @@ namespace cutl public: bool - match (std::string const& s) const + match (string_type const& s) const { return regex_.match (s); } bool - search (std::string const& s) const + search (string_type const& s) const { return regex_.search (s); } - std::string - replace (std::string const& s, bool first_only = false) const + string_type + replace (string_type const& s, bool first_only = false) const { return regex_.replace (s, sub_, first_only); } @@ -172,7 +183,7 @@ namespace cutl return regex_; } - const std::string& + const string_type& substitution () const { return sub_; @@ -186,45 +197,52 @@ namespace cutl private: void - init (std::string const&); + init (string_type const&); private: regex_type regex_; - std::string sub_; + string_type sub_; }; + typedef basic_regexsub regexsub; + typedef basic_regexsub wregexsub; + // Once-off regex execution. // + template inline bool - match (std::string const& s, std::string const& regex) + match (std::basic_string const& s, std::basic_string const& regex) { - re::regex r (regex); + basic_regex r (regex); return r.match (s); } + template inline bool - search (std::string const& s, std::string const& regex) + search (std::basic_string const& s, std::basic_string const& regex) { - re::regex r (regex); + basic_regex r (regex); return r.search (s); } - inline std::string - replace (std::string const& s, - std::string const& regex, - std::string const& sub, + template + inline std::basic_string + replace (std::basic_string const& s, + std::basic_string const& regex, + std::basic_string const& sub, bool first_only = false) { - re::regex r (regex); + basic_regex r (regex); return r.replace (s, sub, first_only); } - inline std::string - replace (std::string const& s, - std::string const& regexsub, // /regex/subst/ + template + inline std::basic_string + replace (std::basic_string const& s, + std::basic_string const& regexsub, // /regex/subst/ bool first_only = false) { - re::regexsub r (regexsub); + basic_regexsub r (regexsub); return r.replace (s, first_only); } @@ -234,11 +252,14 @@ namespace cutl // the unescaped chunk in result or throws the format exception if // the expression is invalid. // - LIBCUTL_EXPORT std::string::size_type - parse (std::string const& s, - std::string::size_type start, - std::string& result); + template + typename std::basic_string::size_type + parse (std::basic_string const& s, + typename std::basic_string::size_type start, + std::basic_string& result); } } +#include + #endif // CUTL_RE_HXX -- cgit v1.1