aboutsummaryrefslogtreecommitdiff
path: root/cutl/re/re.txx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2012-05-09 11:49:37 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2012-05-09 11:49:37 +0200
commit5a6ea019fd48b269121f95dc4afabce760531bbe (patch)
tree52112536c9ec5c43c3766076b38a109d00c5096c /cutl/re/re.txx
parent7d3c805c64e9e3e5ef76ae0d6f07dc49a8b96e61 (diff)
Make regex class template
Diffstat (limited to 'cutl/re/re.txx')
-rw-r--r--cutl/re/re.txx69
1 files changed, 69 insertions, 0 deletions
diff --git a/cutl/re/re.txx b/cutl/re/re.txx
new file mode 100644
index 0000000..9c4122b
--- /dev/null
+++ b/cutl/re/re.txx
@@ -0,0 +1,69 @@
+// file : cutl/re/re.txx
+// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
+// license : MIT; see accompanying LICENSE file
+
+namespace cutl
+{
+ namespace re
+ {
+ //
+ // basic_regexsub
+ //
+ template <typename C>
+ void basic_regexsub<C>::
+ 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<C> (s, "junk after third delimiter");
+ }
+
+ //
+ // parse()
+ //
+ template <typename C>
+ typename std::basic_string<C>::size_type
+ parse (std::basic_string<C> const& s,
+ typename std::basic_string<C>::size_type p,
+ std::basic_string<C>& r)
+ {
+ r.clear ();
+ typename std::basic_string<C>::size_type n (s.size ());
+
+ if (p >= n)
+ throw basic_format<C> (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<C> (s, "missing closing delimiter");
+
+ return p;
+ }
+ }
+}