rexylib/include/rexy/detail/binary_string_conv.hpp

56 lines
2.0 KiB
C++

/**
This file is a part of rexy's general purpose library
Copyright (C) 2020 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef REXY_BINARY_STRING_CONV_HPP
#define REXY_BINARY_STRING_CONV_HPP
#include "rexy/string_base.hpp"
#include "rexy/binary_base.hpp"
#include <cstring> //memcpy
namespace rexy{
template<class Str, std::enable_if_t<is_string_v<Str>,int> = 0>
auto binary_to_string(const binary_base& b)
noexcept(std::is_nothrow_constructible<Str, decltype(b.size())>::value)
{
Str s(b.size()+1);
s.append(b.get(), b.size());
return s;
}
template<class Al, class Str, std::enable_if_t<is_string_v<Str>,int> = 0, std::enable_if_t<std::is_same<std::decay_t<Al>,typename Str::allocator_type>::value,int> = 0>
auto binary_to_string(basic_binary<Al>&& b)noexcept{
return Str(rexy::steal(b.release()), b.size());
}
template<class Bin, detail::enable_if_binary<Bin> = 0>
auto string_to_binary(const string_base<char>& s)
noexcept(std::is_nothrow_constructible<Bin, decltype(s.length())>::value &&
noexcept(std::decay_t<Bin>::allocator_type::allocate(0)))
{
Bin b(s.length()+1);
b.append(s.get(), s.length()+1);
return b;
}
template<class Al, class Bin, detail::enable_if_binary<Bin> = 0, std::enable_if_t<std::is_same<std::decay_t<Al>,typename Bin::allocator_type>::value,int> = 0>
auto string_to_binary(basic_string<char,Al>&& s)noexcept{
return Bin(rexy::steal(s.release()), s.length());
}
} //namespace rexy
#endif