// -*- C++ -*- // // Copyright (c) 2020 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 namespace dwu { template 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__*/