summaryrefslogtreecommitdiff
path: root/include/dwu/capture
blob: 66e00ecaaad0f2af3029096202bb6d46e409c21e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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__*/