| 1 |
// file : cutl/re/re.txx |
| 2 |
// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC |
| 3 |
// license : MIT; see accompanying LICENSE file |
| 4 |
|
| 5 |
namespace cutl |
| 6 |
{ |
| 7 |
namespace re |
| 8 |
{ |
| 9 |
// |
| 10 |
// basic_regexsub |
| 11 |
// |
| 12 |
template <typename C> |
| 13 |
void basic_regexsub<C>:: |
| 14 |
init (string_type const& s) |
| 15 |
{ |
| 16 |
string_type r; |
| 17 |
typename string_type::size_type p (parse (s, 0, r)); |
| 18 |
regex_ = r; |
| 19 |
p = parse (s, p, sub_); |
| 20 |
if (p + 1 < s.size ()) |
| 21 |
throw basic_format<C> (s, "junk after third delimiter"); |
| 22 |
} |
| 23 |
|
| 24 |
// |
| 25 |
// parse() |
| 26 |
// |
| 27 |
template <typename C> |
| 28 |
typename std::basic_string<C>::size_type |
| 29 |
parse (std::basic_string<C> const& s, |
| 30 |
typename std::basic_string<C>::size_type p, |
| 31 |
std::basic_string<C>& r) |
| 32 |
{ |
| 33 |
r.clear (); |
| 34 |
typename std::basic_string<C>::size_type n (s.size ()); |
| 35 |
|
| 36 |
if (p >= n) |
| 37 |
throw basic_format<C> (s, "empty expression"); |
| 38 |
|
| 39 |
char d (s[p++]); |
| 40 |
|
| 41 |
for (; p < n; ++p) |
| 42 |
{ |
| 43 |
if (s[p] == d) |
| 44 |
break; |
| 45 |
|
| 46 |
if (s[p] == '\\') |
| 47 |
{ |
| 48 |
if (++p < n) |
| 49 |
{ |
| 50 |
// Pass the escape sequence through unless it is the delimiter. |
| 51 |
// |
| 52 |
if (s[p] != d) |
| 53 |
r += '\\'; |
| 54 |
|
| 55 |
r += s[p]; |
| 56 |
} |
| 57 |
// else {We ran out of stuff before finding the delimiter.} |
| 58 |
} |
| 59 |
else |
| 60 |
r += s[p]; |
| 61 |
} |
| 62 |
|
| 63 |
if (p == n) |
| 64 |
throw basic_format<C> (s, "missing closing delimiter"); |
| 65 |
|
| 66 |
return p; |
| 67 |
} |
| 68 |
} |
| 69 |
} |