93 lines
3.3 KiB
C++
93 lines
3.3 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 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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef REXY_BINARY_STRING_CONV_HPP
|
|
#define REXY_BINARY_STRING_CONV_HPP
|
|
|
|
#include "rexy/string.hpp"
|
|
#include "rexy/binary.hpp"
|
|
#include <cstring> //memcpy
|
|
|
|
namespace rexy{
|
|
template<class Al, class Str = rexy::string, detail::enable_if_concrete_string<Str> = 0>
|
|
auto binary_to_string(const binary_data<Al>& b)
|
|
noexcept(std::is_nothrow_constructible<Str, decltype(b.size())>::value)
|
|
{
|
|
Str s(b.size()+1);
|
|
memcpy(s.get(), b.get(), b.size());
|
|
s[b.size()] = 0;
|
|
return s;
|
|
}
|
|
template<class Al, class Str = rexy::string, detail::enable_if_concrete_string<Str> = 0, std::enable_if_t<std::is_same<std::decay_t<Al>,typename Str::allocator_type>::value,int> = 0>
|
|
auto binary_to_string(binary_data<Al>&& b)
|
|
noexcept(std::is_nothrow_default_constructible<Str>::value &&
|
|
noexcept(std::declval<Str>().reset(nullptr, 0)) &&
|
|
noexcept(b.release()))
|
|
{
|
|
Str s;
|
|
s.reset(b.get(), b.size());
|
|
b.release();
|
|
return s;
|
|
}
|
|
template<class Bin = rexy::binary, detail::enable_if_binary<Bin> = 0>
|
|
auto string_to_binary(const string_base& s)
|
|
noexcept(std::is_nothrow_constructible<Bin, decltype(s.length())>::value &&
|
|
noexcept(std::declval<Bin>().append(nullptr, 0)))
|
|
{
|
|
Bin b(s.length()+1);
|
|
b.append(s.get(), s.length()+1);
|
|
return b;
|
|
}
|
|
template<class Al, class Bin = rexy::binary, 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(string_intermediary<Al>&& s)
|
|
noexcept(std::is_nothrow_default_constructible<Bin>::value &&
|
|
noexcept(std::declval<Bin>().reset(nullptr, 0)) &&
|
|
noexcept(s.release()))
|
|
{
|
|
Bin b;
|
|
b.reset(s.get(), s.length()+1);
|
|
s.release();
|
|
return b;
|
|
}
|
|
template<class L, class R, detail::enable_if_binary<L> = 0, detail::enable_if_concrete_string<R> = 0>
|
|
decltype(auto) operator+=(L& l, R&& r)
|
|
noexcept(noexcept(l.append(nullptr, 0)))
|
|
{
|
|
l.append(r.get(), r.length()+1);
|
|
return l;
|
|
}
|
|
template<class L, class R, detail::enable_if_binary<L> = 0, detail::enable_if_string<R> = 0, std::enable_if_t<!detail::is_concrete_string<R>::value,int> = 0>
|
|
decltype(auto) operator+=(L& l, R&& r)
|
|
noexcept(std::is_nothrow_constructible<rexy::string,R&&>::value &&
|
|
std::is_nothrow_assignable<L,rexy::string>::value)
|
|
{
|
|
rexy::string concrete = r;
|
|
return (l = concrete);
|
|
}
|
|
template<class L, class R, detail::enable_if_concrete_string<L> = 0, detail::enable_if_binary<R> = 0>
|
|
decltype(auto) operator+=(L& l, R&& r)
|
|
noexcept(noexcept(l.resize(0, 0)))
|
|
{
|
|
l.resize(l.length(), r.size() + 1);
|
|
memcpy(l.get()+l.length()+1, r.get(), r.size());
|
|
return l;
|
|
}
|
|
} //namespace rexy
|
|
|
|
#endif
|