From 8286ac511144e4f17d34eac9affb97e50646344a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Wed, 23 Jul 2014 15:25:44 +0200 Subject: Imported Upstream version 4.0.0 --- libcutl/cutl/re/re.cxx | 223 +++++++++++++++++++++++++++++++++++++++++++++++++ libcutl/cutl/re/re.txx | 69 +++++++++++++++ 2 files changed, 292 insertions(+) create mode 100644 libcutl/cutl/re/re.cxx create mode 100644 libcutl/cutl/re/re.txx (limited to 'libcutl/cutl/re') diff --git a/libcutl/cutl/re/re.cxx b/libcutl/cutl/re/re.cxx new file mode 100644 index 0000000..3b132ab --- /dev/null +++ b/libcutl/cutl/re/re.cxx @@ -0,0 +1,223 @@ +// file : cutl/re/re.cxx +// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#include + +#include // LIBCUTL_EXTERNAL_BOOST + +#ifndef LIBCUTL_EXTERNAL_BOOST +# include +#else +# include +#endif + +using namespace std; + +namespace cutl +{ + namespace re + { + // + // format_base + // + + format_base:: + ~format_base () throw () + { + } + + char const* format_base:: + what () const throw () + { + return description_.c_str (); + } + + // + // basic_regex + // + template + struct basic_regex::impl + { + typedef basic_string string_type; + typedef tr1::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 (tr1::regex_constants::ECMAScript); + + if (icase) + f |= tr1::regex_constants::icase; + + r.assign (s, f); + } + + regex_type r; + }; + + template <> + basic_regex:: + ~basic_regex () + { + delete impl_; + } + + template <> + basic_regex:: + ~basic_regex () + { + delete impl_; + } + + template <> + basic_regex:: + basic_regex (basic_regex const& r) + : str_ (r.str_), impl_ (new impl (r.impl_->r)) + { + } + + template <> + basic_regex:: + basic_regex (basic_regex const& r) + : str_ (r.str_), impl_ (new impl (r.impl_->r)) + { + } + + template <> + 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 <> + 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 <> + 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 (tr1::regex_constants::ECMAScript); + + if (icase) + f |= tr1::regex_constants::icase; + + impl_->r.assign (*s, f); + } + } + catch (tr1::regex_error const& e) + { + throw basic_format (s == 0 ? "" : *s, e.what ()); + } + + str_.swap (tmp); + } + + template <> + 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 (tr1::regex_constants::ECMAScript); + + if (icase) + f |= tr1::regex_constants::icase; + + impl_->r.assign (*s, f); + } + } + catch (tr1::regex_error const& e) + { + throw basic_format (s == 0 ? L"" : *s, e.what ()); + } + + str_.swap (tmp); + } + + template <> + bool basic_regex:: + match (string_type const& s) const + { + return tr1::regex_match (s, impl_->r); + } + + template <> + bool basic_regex:: + match (string_type const& s) const + { + return tr1::regex_match (s, impl_->r); + } + + template <> + bool basic_regex:: + search (string_type const& s) const + { + return tr1::regex_search (s, impl_->r); + } + + template <> + bool basic_regex:: + search (string_type const& s) const + { + return tr1::regex_search (s, impl_->r); + } + + template <> + string basic_regex:: + replace (string_type const& s, + string_type const& sub, + bool first_only) const + { + tr1::regex_constants::match_flag_type f ( + tr1::regex_constants::format_default); + + if (first_only) + f |= tr1::regex_constants::format_first_only; + + return tr1::regex_replace (s, impl_->r, sub, f); + } + + template <> + wstring basic_regex:: + replace (string_type const& s, + string_type const& sub, + bool first_only) const + { + tr1::regex_constants::match_flag_type f ( + tr1::regex_constants::format_default); + + if (first_only) + f |= tr1::regex_constants::format_first_only; + + return tr1::regex_replace (s, impl_->r, sub, f); + } + } +} diff --git a/libcutl/cutl/re/re.txx b/libcutl/cutl/re/re.txx new file mode 100644 index 0000000..174e0d4 --- /dev/null +++ b/libcutl/cutl/re/re.txx @@ -0,0 +1,69 @@ +// file : cutl/re/re.txx +// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +namespace cutl +{ + namespace re + { + // + // basic_regexsub + // + template + void basic_regexsub:: + init (string_type const& s) + { + string_type r; + typename string_type::size_type p (parse (s, 0, r)); + regex_ = r; + p = parse (s, p, sub_); + if (p + 1 < s.size ()) + throw basic_format (s, "junk after third delimiter"); + } + + // + // parse() + // + template + typename std::basic_string::size_type + parse (std::basic_string const& s, + typename std::basic_string::size_type p, + std::basic_string& r) + { + r.clear (); + typename std::basic_string::size_type n (s.size ()); + + if (p >= n) + throw basic_format (s, "empty expression"); + + char d (s[p++]); + + for (; p < n; ++p) + { + if (s[p] == d) + break; + + if (s[p] == '\\') + { + if (++p < n) + { + // Pass the escape sequence through unless it is the delimiter. + // + if (s[p] != d) + r += '\\'; + + r += s[p]; + } + // else {We ran out of stuff before finding the delimiter.} + } + else + r += s[p]; + } + + if (p == n) + throw basic_format (s, "missing closing delimiter"); + + return p; + } + } +} -- cgit v1.2.3