/**
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 .
*/
#ifndef REXY_BINARY_STRING_CONV_HPP
#define REXY_BINARY_STRING_CONV_HPP
#include "rexy/string.hpp"
#include "rexy/binary.hpp"
#include //memcpy
namespace rexy{
template = 0>
auto binary_to_string(const binary_data& b)
noexcept(std::is_nothrow_constructible::value)
{
Str s(b.size()+1);
s.append(b.get(), b.size());
return s;
}
template = 0, std::enable_if_t,typename Str::allocator_type>::value,int> = 0>
auto binary_to_string(binary_data&& b)noexcept{
return Str(rexy::steal(b.release()), b.size());
}
template = 0>
auto string_to_binary(const string_base& s)
noexcept(std::is_nothrow_constructible::value &&
noexcept(std::decay_t::allocator_type::allocate(0)))
{
Bin b(s.length()+1);
b.append(s.get(), s.length()+1);
return b;
}
template = 0, std::enable_if_t,typename Bin::allocator_type>::value,int> = 0>
auto string_to_binary(string_intermediary&& s)noexcept{
return Bin(rexy::steal(s.release()), s.length());
}
template = 0, detail::enable_if_concrete_string = 0>
decltype(auto) operator+=(L& l, R&& r)
noexcept(noexcept(std::decay_t::allocator_type::allocate(0)) &&
noexcept(std::decay_t::allocator_type::free(nullptr)))
{
l.append(r.get(), r.length()+1);
return l;
}
template = 0, detail::enable_if_string = 0, std::enable_if_t::value,int> = 0>
decltype(auto) operator+=(L& l, R&& r)
noexcept(std::is_nothrow_constructible::value &&
(noexcept(std::decay_t::allocator_type::allocate(0))))
{
rexy::string concrete = r;
return (l += concrete);
}
template = 0, detail::enable_if_binary = 0>
decltype(auto) operator+=(L& l, R&& r)
noexcept(noexcept(std::decay_t::allocator_type::allocate(0)) &&
noexcept(std::decay_t::allocator_type::free(nullptr)))
{
l.append(r.get(), r.size());
return l;
}
} //namespace rexy
#endif