moved json escape out of string files

This commit is contained in:
rexy712
2019-03-20 16:12:01 -07:00
parent 1a4133bc92
commit e12acfe8bc
8 changed files with 165 additions and 121 deletions
+1 -41
View File
@@ -48,48 +48,8 @@ namespace raii{
}
//new allocated string
struct string : public string_intermediary<detail::default_allocator>
{
string(const string&) = default;
string(string&&) = default;
string& operator=(const string&) = default;
string& operator=(string&&) = default;
using string = string_intermediary<detail::default_allocator>;
using string_intermediary<detail::default_allocator>::string_intermediary;
using string_intermediary<detail::default_allocator>::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<class Tup, size_t I = 0>
void _js_assign(char* dest, Tup&& t, size_t offset){
size_t written = _sanitize_json_copy(dest+offset, std::get<I>(t));
if constexpr(I+2 < std::tuple_size<std::remove_reference_t<Tup>>::value){
_js_assign<Tup,I+2>(dest, std::forward<Tup>(t), offset+written);
}
}
template<class T, size_t I = 0>
size_t _calc_escaped_len_all(T&& t){
size_t len = _calc_escaped_len(std::get<I>(t));
if constexpr(I+2 < std::tuple_size<std::remove_reference_t<T>>::value){
len += _calc_escaped_len_all<T,I+2>(std::forward<T>(t));
}
return len;
}
}
template<class T, typename std::enable_if<detail::is_string<T>::value && !detail::is_concrete_string<T>::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<char*>(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