summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorWolfgang Draxinger <code@datenwolf.net>2020-03-30 20:48:51 +0200
committerWolfgang Draxinger <code@datenwolf.net>2020-03-30 20:48:51 +0200
commit51be437989033e28aedb9ba8d72c2b2fcadd463e (patch)
tree18716f456d07901b9520d0b6a7f0048227adf9bd /include
parent5aeae45d8b06d4f7b2227c222cbf9b95221b85a2 (diff)
downloaddwu-51be437989033e28aedb9ba8d72c2b2fcadd463e.tar.gz
dwu-51be437989033e28aedb9ba8d72c2b2fcadd463e.tar.bz2
string escape sequence capture
Diffstat (limited to 'include')
-rw-r--r--include/dwu/capture93
1 files changed, 93 insertions, 0 deletions
diff --git a/include/dwu/capture b/include/dwu/capture
new file mode 100644
index 0000000..66e00ec
--- /dev/null
+++ b/include/dwu/capture
@@ -0,0 +1,93 @@
+// <dwu/capture> -*- C++ -*-
+//
+// Copyright (c) 2019 Wolfgang 'datenwolf' Draxinger
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+#pragma once
+#ifndef DWU__CAPTURE__
+#define DWU__CAPTURE__ 1
+
+#include <iterator>
+
+namespace dwu {
+ template<class S>
+ S capture(S s)
+ {
+ auto it_w = s.begin();
+ for( auto it_r = s.begin()
+ ; s.end() != it_r
+ ; it_w = std::next(it_w)
+ ){
+ if( '\\' != *it_r ){
+ *it_w = *it_r;
+ it_r = std::next(it_r);
+ continue;
+ }
+
+ it_r = std::next(it_r);
+ if( s.end() == it_r ){ break; }
+
+ int c = 0;
+ if( '0' <= *it_r && '7' >= *it_r ){
+ c = 0;
+ while( s.end() != it_r
+ && '0' <= *it_r && '7' >= *it_r
+ ){
+ c = (c<<3) | ((*it_r) - '0');
+ it_r = std::next(it_r);
+ }
+ } else
+ if( 'x' == *it_r ){
+ c = 0;
+ it_r = std::next(it_r);
+ while( s.end() != it_r ){
+ int nyb = 0;
+ if( '0' <= *it_r && '9' >= *it_r ){
+ nyb = (*it_r) - '0' + 0x0;
+ } else
+ if( 'a' <= *it_r && 'f' >= *it_r ){
+ nyb = (*it_r) - 'a' + 0xA;
+ } else
+ if( 'A' <= *it_r && 'F' >= *it_r ){
+ nyb = (*it_r) - 'A' + 0xA;
+ } else break;
+ c = (c<<4) | nyb;
+ it_r = std::next(it_r);
+ }
+ } else {
+ switch( *it_r ){
+ case 'a': c = '\a'; break;
+ case 'b': c = '\b'; break;
+ case 'f': c = '\f'; break;
+ case 'n': c = '\n'; break;
+ case 'r': c = '\r'; break;
+ case 't': c = '\t'; break;
+ case 'v': c = '\v'; break;
+ default: c = 0xFF;
+ }
+ it_r = std::next(it_r);
+ }
+ *it_w = c;
+ }
+ s.resize(it_w - s.begin());
+ return s;
+ }
+}
+
+#endif/*DWU__CAPTURE__*/