| 1 |
// file : cutl/re/re.cxx |
| 2 |
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC |
| 3 |
// license : MIT; see accompanying LICENSE file |
| 4 |
|
| 5 |
#include <cutl/re.hxx> |
| 6 |
|
| 7 |
#include <cutl/details/config.hxx> // LIBCUTL_EXTERNAL_BOOST |
| 8 |
|
| 9 |
#ifndef LIBCUTL_EXTERNAL_BOOST |
| 10 |
# include <cutl/details/boost/tr1/regex.hpp> |
| 11 |
#else |
| 12 |
# include <boost/regex.hpp> |
| 13 |
#endif |
| 14 |
|
| 15 |
using namespace std; |
| 16 |
|
| 17 |
namespace cutl |
| 18 |
{ |
| 19 |
namespace re |
| 20 |
{ |
| 21 |
// |
| 22 |
// format_base |
| 23 |
// |
| 24 |
|
| 25 |
format_base:: |
| 26 |
~format_base () throw () |
| 27 |
{ |
| 28 |
} |
| 29 |
|
| 30 |
char const* format_base:: |
| 31 |
what () const throw () |
| 32 |
{ |
| 33 |
return description_.c_str (); |
| 34 |
} |
| 35 |
|
| 36 |
// |
| 37 |
// basic_regex |
| 38 |
// |
| 39 |
template <typename C> |
| 40 |
struct basic_regex<C>::impl |
| 41 |
{ |
| 42 |
typedef basic_string<C> string_type; |
| 43 |
typedef boost::basic_regex<C> regex_type; |
| 44 |
typedef typename regex_type::flag_type flag_type; |
| 45 |
|
| 46 |
impl () {} |
| 47 |
impl (regex_type const& r): r (r) {} |
| 48 |
impl (string_type const& s, bool icase) |
| 49 |
{ |
| 50 |
flag_type f (boost::regex_constants::ECMAScript); |
| 51 |
|
| 52 |
if (icase) |
| 53 |
f |= boost::regex_constants::icase; |
| 54 |
|
| 55 |
r.assign (s, f); |
| 56 |
} |
| 57 |
|
| 58 |
regex_type r; |
| 59 |
}; |
| 60 |
|
| 61 |
template <> |
| 62 |
basic_regex<char>:: |
| 63 |
~basic_regex () |
| 64 |
{ |
| 65 |
delete impl_; |
| 66 |
} |
| 67 |
|
| 68 |
template <> |
| 69 |
basic_regex<wchar_t>:: |
| 70 |
~basic_regex () |
| 71 |
{ |
| 72 |
delete impl_; |
| 73 |
} |
| 74 |
|
| 75 |
template <> |
| 76 |
basic_regex<char>:: |
| 77 |
basic_regex (basic_regex const& r) |
| 78 |
: str_ (r.str_), impl_ (new impl (r.impl_->r)) |
| 79 |
{ |
| 80 |
} |
| 81 |
|
| 82 |
template <> |
| 83 |
basic_regex<wchar_t>:: |
| 84 |
basic_regex (basic_regex const& r) |
| 85 |
: str_ (r.str_), impl_ (new impl (r.impl_->r)) |
| 86 |
{ |
| 87 |
} |
| 88 |
|
| 89 |
template <> |
| 90 |
basic_regex<char>& basic_regex<char>:: |
| 91 |
operator= (basic_regex const& r) |
| 92 |
{ |
| 93 |
string_type tmp (r.str_); |
| 94 |
impl_->r = r.impl_->r; |
| 95 |
str_.swap (tmp); |
| 96 |
return *this; |
| 97 |
} |
| 98 |
|
| 99 |
template <> |
| 100 |
basic_regex<wchar_t>& basic_regex<wchar_t>:: |
| 101 |
operator= (basic_regex const& r) |
| 102 |
{ |
| 103 |
string_type tmp (r.str_); |
| 104 |
impl_->r = r.impl_->r; |
| 105 |
str_.swap (tmp); |
| 106 |
return *this; |
| 107 |
} |
| 108 |
|
| 109 |
template <> |
| 110 |
void basic_regex<char>:: |
| 111 |
init (string_type const* s, bool icase) |
| 112 |
{ |
| 113 |
string_type tmp (s == 0 ? string_type () : *s); |
| 114 |
|
| 115 |
try |
| 116 |
{ |
| 117 |
if (impl_ == 0) |
| 118 |
impl_ = s == 0 ? new impl : new impl (*s, icase); |
| 119 |
else |
| 120 |
{ |
| 121 |
impl::flag_type f (boost::regex_constants::ECMAScript); |
| 122 |
|
| 123 |
if (icase) |
| 124 |
f |= boost::regex_constants::icase; |
| 125 |
|
| 126 |
impl_->r.assign (*s, f); |
| 127 |
} |
| 128 |
} |
| 129 |
catch (boost::regex_error const& e) |
| 130 |
{ |
| 131 |
throw basic_format<char> (s == 0 ? "" : *s, e.what ()); |
| 132 |
} |
| 133 |
|
| 134 |
str_.swap (tmp); |
| 135 |
} |
| 136 |
|
| 137 |
template <> |
| 138 |
void basic_regex<wchar_t>:: |
| 139 |
init (string_type const* s, bool icase) |
| 140 |
{ |
| 141 |
string_type tmp (s == 0 ? string_type () : *s); |
| 142 |
|
| 143 |
try |
| 144 |
{ |
| 145 |
if (impl_ == 0) |
| 146 |
impl_ = s == 0 ? new impl : new impl (*s, icase); |
| 147 |
else |
| 148 |
{ |
| 149 |
impl::flag_type f (boost::regex_constants::ECMAScript); |
| 150 |
|
| 151 |
if (icase) |
| 152 |
f |= boost::regex_constants::icase; |
| 153 |
|
| 154 |
impl_->r.assign (*s, f); |
| 155 |
} |
| 156 |
} |
| 157 |
catch (boost::regex_error const& e) |
| 158 |
{ |
| 159 |
throw basic_format<wchar_t> (s == 0 ? L"" : *s, e.what ()); |
| 160 |
} |
| 161 |
|
| 162 |
str_.swap (tmp); |
| 163 |
} |
| 164 |
|
| 165 |
template <> |
| 166 |
bool basic_regex<char>:: |
| 167 |
match (string_type const& s) const |
| 168 |
{ |
| 169 |
return boost::regex_match (s, impl_->r); |
| 170 |
} |
| 171 |
|
| 172 |
template <> |
| 173 |
bool basic_regex<wchar_t>:: |
| 174 |
match (string_type const& s) const |
| 175 |
{ |
| 176 |
return boost::regex_match (s, impl_->r); |
| 177 |
} |
| 178 |
|
| 179 |
template <> |
| 180 |
bool basic_regex<char>:: |
| 181 |
search (string_type const& s) const |
| 182 |
{ |
| 183 |
return boost::regex_search (s, impl_->r); |
| 184 |
} |
| 185 |
|
| 186 |
template <> |
| 187 |
bool basic_regex<wchar_t>:: |
| 188 |
search (string_type const& s) const |
| 189 |
{ |
| 190 |
return boost::regex_search (s, impl_->r); |
| 191 |
} |
| 192 |
|
| 193 |
template <> |
| 194 |
string basic_regex<char>:: |
| 195 |
replace (string_type const& s, |
| 196 |
string_type const& sub, |
| 197 |
bool first_only) const |
| 198 |
{ |
| 199 |
boost::regex_constants::match_flag_type f ( |
| 200 |
boost::regex_constants::format_default); |
| 201 |
|
| 202 |
if (first_only) |
| 203 |
f |= boost::regex_constants::format_first_only; |
| 204 |
|
| 205 |
return boost::regex_replace (s, impl_->r, sub, f); |
| 206 |
} |
| 207 |
|
| 208 |
template <> |
| 209 |
wstring basic_regex<wchar_t>:: |
| 210 |
replace (string_type const& s, |
| 211 |
string_type const& sub, |
| 212 |
bool first_only) const |
| 213 |
{ |
| 214 |
boost::regex_constants::match_flag_type f ( |
| 215 |
boost::regex_constants::format_default); |
| 216 |
|
| 217 |
if (first_only) |
| 218 |
f |= boost::regex_constants::format_first_only; |
| 219 |
|
| 220 |
return boost::regex_replace (s, impl_->r, sub, f); |
| 221 |
} |
| 222 |
} |
| 223 |
} |