/** This file is a part of rexy's matrix bot Copyright (C) 2019 rexy712 This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ #ifndef RAII_STRING_HPP #define RAII_STRING_HPP #include "raii/string_base.hpp" #include //operator new/delete #include //memcpy namespace raii{ namespace detail{ class default_allocator { public: static void free(void* data){ ::operator delete(data); } static void* allocate(size_t size){ return ::operator new(size); } static void* copy(const void* c, size_t size){ char* tmp = reinterpret_cast(allocate(size)); memcpy(tmp, c, size-1); tmp[size-1] = 0; return tmp; } }; } //new allocated string struct string : public string_intermediary { string(const string&) = default; string(string&&) = default; string& operator=(const string&) = default; string& operator=(string&&) = default; using string_intermediary::string_intermediary; using string_intermediary::operator=; }; namespace detail{ size_t _calc_escaped_len(const char* str); char _escape_to_letter(char escape); size_t _sanitize_json_copy(char* dest, const char* in); template void _js_assign(char* dest, Tup&& t, size_t offset){ size_t written = _sanitize_json_copy(dest+offset, std::get(t)); if constexpr(I+2 < std::tuple_size>::value){ _js_assign(dest, std::forward(t), offset+written); } } template size_t _calc_escaped_len_all(T&& t){ size_t len = _calc_escaped_len(std::get(t)); if constexpr(I+2 < std::tuple_size>::value){ len += _calc_escaped_len_all(std::forward(t)); } return len; } } template::value && !detail::is_concrete_string::value,void>::type* = nullptr> string json_escape(T&& t){ auto tup = t.get(); size_t len = detail::_calc_escaped_len_all(tup); char* tmp = reinterpret_cast(string::allocator_type::allocate(len+1)); detail::_js_assign(tmp, tup, 0); tmp[len] = 0; return string(tmp, len); } string json_escape(const string_base& str); } #endif