// file : cutl/re/re.cxx // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC // license : MIT; see accompanying LICENSE file #include #include // LIBCUTL_* // For build2 build it is either C++11 regex or external Boost. // #ifdef LIBCUTL_BUILD2 # ifdef LIBCUTL_CXX11 # include # else # include # endif #else # ifndef LIBCUTL_EXTERNAL_BOOST # include # else # include # endif #endif using namespace std; namespace cutl { namespace re { #if defined(LIBCUTL_BUILD2) && defined(LIBCUTL_CXX11) namespace ire = std; #else namespace ire = std::tr1; #endif // // format_base // char const* format_base:: what () const LIBCUTL_NOTHROW_NOEXCEPT { return description_.c_str (); } // // basic_regex // template struct basic_regex::impl { typedef basic_string string_type; typedef ire::basic_regex regex_type; typedef typename regex_type::flag_type flag_type; impl () {} impl (regex_type const& r): r (r) {} impl (string_type const& s, bool icase) { flag_type f (ire::regex_constants::ECMAScript); if (icase) f |= ire::regex_constants::icase; r.assign (s, f); } regex_type r; }; template <> LIBCUTL_EXPORT basic_regex:: ~basic_regex () { delete impl_; } template <> LIBCUTL_EXPORT basic_regex:: ~basic_regex () { delete impl_; } template <> LIBCUTL_EXPORT basic_regex:: basic_regex (basic_regex const& r) : str_ (r.str_), impl_ (new impl (r.impl_->r)) { } template <> LIBCUTL_EXPORT basic_regex:: basic_regex (basic_regex const& r) : str_ (r.str_), impl_ (new impl (r.impl_->r)) { } template <> LIBCUTL_EXPORT basic_regex& basic_regex:: operator= (basic_regex const& r) { string_type tmp (r.str_); impl_->r = r.impl_->r; str_.swap (tmp); return *this; } template <> LIBCUTL_EXPORT basic_regex& basic_regex:: operator= (basic_regex const& r) { string_type tmp (r.str_); impl_->r = r.impl_->r; str_.swap (tmp); return *this; } template <> LIBCUTL_EXPORT void basic_regex:: init (string_type const* s, bool icase) { string_type tmp (s == 0 ? string_type () : *s); try { if (impl_ == 0) impl_ = s == 0 ? new impl : new impl (*s, icase); else { impl::flag_type f (ire::regex_constants::ECMAScript); if (icase) f |= ire::regex_constants::icase; impl_->r.assign (*s, f); } } catch (ire::regex_error const& e) { throw basic_format (s == 0 ? "" : *s, e.what ()); } str_.swap (tmp); } template <> LIBCUTL_EXPORT void basic_regex:: init (string_type const* s, bool icase) { string_type tmp (s == 0 ? string_type () : *s); try { if (impl_ == 0) impl_ = s == 0 ? new impl : new impl (*s, icase); else { impl::flag_type f (ire::regex_constants::ECMAScript); if (icase) f |= ire::regex_constants::icase; impl_->r.assign (*s, f); } } catch (ire::regex_error const& e) { throw basic_format (s == 0 ? L"" : *s, e.what ()); } str_.swap (tmp); } template <> LIBCUTL_EXPORT bool basic_regex:: match (string_type const& s) const { return ire::regex_match (s, impl_->r); } template <> LIBCUTL_EXPORT bool basic_regex:: match (string_type const& s) const { return ire::regex_match (s, impl_->r); } template <> LIBCUTL_EXPORT bool basic_regex:: search (string_type const& s) const { return ire::regex_search (s, impl_->r); } template <> LIBCUTL_EXPORT bool basic_regex:: search (string_type const& s) const { return ire::regex_search (s, impl_->r); } template <> LIBCUTL_EXPORT string basic_regex:: replace (string_type const& s, string_type const& sub, bool first_only) const { ire::regex_constants::match_flag_type f ( ire::regex_constants::format_default); if (first_only) f |= ire::regex_constants::format_first_only; return ire::regex_replace (s, impl_->r, sub, f); } template <> LIBCUTL_EXPORT wstring basic_regex:: replace (string_type const& s, string_type const& sub, bool first_only) const { ire::regex_constants::match_flag_type f ( ire::regex_constants::format_default); if (first_only) f |= ire::regex_constants::format_first_only; return ire::regex_replace (s, impl_->r, sub, f); } } }