308 lines
10 KiB
C++
308 lines
10 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_STRING_BASE_TPP
|
|
#define REXY_STRING_BASE_TPP
|
|
|
|
#include <utility> //forward, move, swap, etc
|
|
#include <cstdlib> //memcpy
|
|
#include <cstring> //strlen, strcpy
|
|
|
|
#include "cx/utility.hpp" //max
|
|
|
|
#define STOP_STRICT_ALIAS_WARNING(x) (x)
|
|
|
|
namespace rexy{
|
|
|
|
template<class Allocator>
|
|
constexpr string_intermediary<Allocator>::string_intermediary(void)noexcept{}
|
|
template<class Allocator>
|
|
constexpr string_intermediary<Allocator>::string_intermediary(rexy::steal<char*> data)noexcept:
|
|
string_base(data.value() ? cx::strlen(data.value()) : 0)
|
|
{
|
|
m_data = data.value();
|
|
m_length = m_cap;
|
|
}
|
|
template<class Allocator>
|
|
constexpr string_intermediary<Allocator>::string_intermediary(rexy::steal<char*> data, size_t len)noexcept:
|
|
string_base(data.value(), len, len){}
|
|
template<class Allocator>
|
|
constexpr string_intermediary<Allocator>::string_intermediary(rexy::steal<char*> data, size_t len, size_t cap)noexcept:
|
|
string_base(data.value(), len, cap){}
|
|
template<class Allocator>
|
|
string_intermediary<Allocator>::string_intermediary(const char* data, size_t len)
|
|
noexcept(noexcept(Allocator::copy(data,len))):
|
|
string_base(reinterpret_cast<char*>(len ? Allocator::copy(data, len+1) : nullptr), len, len)
|
|
{
|
|
m_data[len] = 0;
|
|
}
|
|
template<class Allocator>
|
|
string_intermediary<Allocator>::string_intermediary(const char* data)
|
|
noexcept(noexcept(Allocator::copy(data, m_cap))):
|
|
string_base(data ? strlen(data) : 0)
|
|
{
|
|
if(m_cap){
|
|
m_data = reinterpret_cast<char*>(Allocator::copy(data, m_cap+1));
|
|
m_length = m_cap;
|
|
}
|
|
}
|
|
template<class Allocator>
|
|
string_intermediary<Allocator>::string_intermediary(size_t len)
|
|
noexcept(noexcept(Allocator::allocate(len))):
|
|
string_base(reinterpret_cast<char*>(len ? Allocator::allocate(len+1) : nullptr), len)
|
|
{
|
|
m_data[len] = 0;
|
|
}
|
|
template<class Allocator>
|
|
string_intermediary<Allocator>::string_intermediary(size_t len, size_t cap)
|
|
noexcept(noexcept(Allocator::allocate(len))):
|
|
string_base(reinterpret_cast<char*>(len ? Allocator::allocate(len+1) : nullptr), len, cap)
|
|
{
|
|
m_data[len] = 0;
|
|
}
|
|
|
|
//normal copy and move ctors
|
|
template<class Allocator>
|
|
string_intermediary<Allocator>::string_intermediary(const string_intermediary& b)
|
|
noexcept(noexcept(Allocator::copy(b.m_data, b.m_length))):
|
|
string_base(reinterpret_cast<char*>(b.m_length ? Allocator::copy(b.m_data, b.m_length+1) : nullptr), b.m_length, b.m_cap){}
|
|
template<class Allocator>
|
|
constexpr string_intermediary<Allocator>::string_intermediary(string_intermediary&& s)
|
|
noexcept(noexcept(cx::exchange(s.m_data, nullptr))):
|
|
string_base(cx::exchange(s.m_data, nullptr), s.m_length, s.m_cap){}
|
|
|
|
template<class Allocator>
|
|
string_intermediary<Allocator>::string_intermediary(const string_base& b)
|
|
noexcept(noexcept(Allocator::copy(b.get(), b.length()))):
|
|
string_base(reinterpret_cast<char*>(b.length() ? Allocator::copy(b.get(), b.length()+1) : nullptr), b.length(), b.capacity()){}
|
|
|
|
//dtor
|
|
template<class Allocator>
|
|
string_intermediary<Allocator>::~string_intermediary(void)
|
|
noexcept(noexcept(Allocator::free(m_data)))
|
|
{
|
|
Allocator::free(m_data);
|
|
}
|
|
|
|
template<class Allocator>
|
|
string_intermediary<Allocator>& string_intermediary<Allocator>::operator=(const string_intermediary& s)
|
|
noexcept(noexcept(Allocator::copy(nullptr,0)) &&
|
|
noexcept(Allocator::free(nullptr)))
|
|
{
|
|
if(s.m_length < m_cap){
|
|
memcpy(m_data, s.m_data, s.m_length+1);
|
|
m_length = s.m_length;
|
|
return *this;
|
|
}
|
|
string_intermediary tmp(s);
|
|
return (*this = std::move(tmp));
|
|
}
|
|
template<class Allocator>
|
|
constexpr string_intermediary<Allocator>& string_intermediary<Allocator>::operator=(string_intermediary&& s)noexcept{
|
|
cx::swap(m_data, s.m_data);
|
|
m_length = s.m_length;
|
|
m_cap = s.m_cap;
|
|
return *this;
|
|
}
|
|
//Copy from c string
|
|
template<class Allocator>
|
|
string_intermediary<Allocator>& string_intermediary<Allocator>::operator=(const char* c)
|
|
noexcept(noexcept(Allocator::copy(nullptr,0)) &&
|
|
noexcept(Allocator::free(nullptr)))
|
|
{
|
|
return _copy_string(c, strlen(c));
|
|
}
|
|
//Copy from other string_base
|
|
template<class Allocator>
|
|
string_intermediary<Allocator>& string_intermediary<Allocator>::operator=(const string_base& s)
|
|
noexcept(noexcept(Allocator::copy(nullptr,0)) &&
|
|
noexcept(Allocator::free(nullptr)))
|
|
{
|
|
return _copy_string(s.get(), s.length());
|
|
}
|
|
|
|
//Replace managed pointer. Frees existing value
|
|
template<class Allocator>
|
|
void string_intermediary<Allocator>::reset(char* val)
|
|
noexcept(noexcept(Allocator::free(m_data)))
|
|
{
|
|
Allocator::free(m_data);
|
|
m_data = val;
|
|
m_length = val ? strlen(val) : 0;
|
|
m_cap = m_length;
|
|
}
|
|
template<class Allocator>
|
|
void string_intermediary<Allocator>::reset(char* val, size_t len)
|
|
noexcept(noexcept(Allocator::free(m_data)))
|
|
{
|
|
Allocator::free(m_data);
|
|
m_data = val;
|
|
m_length = len;
|
|
m_cap = len;
|
|
}
|
|
template<class Allocator>
|
|
bool string_intermediary<Allocator>::resize(size_t newsize)
|
|
noexcept(noexcept(Allocator::copy(nullptr,0)) &&
|
|
noexcept(Allocator::free(nullptr)))
|
|
{
|
|
if(newsize < m_cap)
|
|
return false;
|
|
return (*this = string_intermediary(m_data, newsize));
|
|
}
|
|
template<class Allocator>
|
|
void string_intermediary<Allocator>::append(const char* data, size_t len)
|
|
noexcept(noexcept(Allocator::allocate(0)) &&
|
|
noexcept(Allocator::free(nullptr)))
|
|
{
|
|
if(len+m_length <= m_cap){
|
|
memcpy(m_data+m_length, data, len);
|
|
m_length += len;
|
|
m_data[m_length] = 0;
|
|
}else if(!m_data){
|
|
*this = string_intermediary(len, len);
|
|
memcpy(m_data, data, len);
|
|
m_data[len] = 0;
|
|
}else{
|
|
auto newsize = cx::max(m_length+len, m_cap*2);
|
|
string_intermediary tmp(newsize);
|
|
tmp.append(m_data, m_length);
|
|
tmp.append(data, len);
|
|
*this = std::move(tmp);
|
|
}
|
|
}
|
|
template<class Allocator>
|
|
void string_intermediary<Allocator>::append(const char* data)
|
|
noexcept(noexcept(Allocator::allocate(0)) &&
|
|
noexcept(Allocator::free(nullptr)))
|
|
{
|
|
if(data)
|
|
append(data, strlen(data));
|
|
}
|
|
template<class Allocator>
|
|
void string_intermediary<Allocator>::append(const string_base& s)
|
|
noexcept(noexcept(Allocator::allocate(0)) &&
|
|
noexcept(Allocator::free(nullptr)))
|
|
{
|
|
append(s.get(), s.length());
|
|
}
|
|
|
|
template<class Allocator>
|
|
string_intermediary<Allocator>& string_intermediary<Allocator>::_copy_string(const char* s, size_t len)
|
|
noexcept(noexcept(Allocator::copy(nullptr,0)) &&
|
|
noexcept(Allocator::free(nullptr)))
|
|
{
|
|
if(len <= m_length){
|
|
m_length = len;
|
|
memcpy(m_data, s, len);
|
|
m_data[len] = 0;
|
|
return *this;
|
|
}
|
|
return (*this = string_intermediary(s, len));
|
|
}
|
|
|
|
|
|
template<class Left, class Right>
|
|
template<class T, class U>
|
|
constexpr string_cat_expr<Left,Right>::string_cat_expr(T&& l, U&& r)
|
|
noexcept(std::is_nothrow_constructible<Left,T&&>::value &&
|
|
std::is_nothrow_constructible<Right,U&&>::value):
|
|
m_l(std::forward<T>(l)),
|
|
m_r(std::forward<U>(r)){}
|
|
template<class Left, class Right>
|
|
constexpr string_cat_expr<Left,Right>::string_cat_expr(string_cat_expr&& s)
|
|
noexcept(std::is_nothrow_constructible<Left,decltype(s.m_l)>::value &&
|
|
std::is_nothrow_constructible<Right,decltype(s.m_r)>::value):
|
|
m_l(std::forward<Left>(s.m_l)),
|
|
m_r(std::forward<Right>(s.m_r)){}
|
|
|
|
template<class Left, class Right>
|
|
constexpr size_t string_cat_expr<Left,Right>::length(void)const noexcept{
|
|
return m_l.length() + m_r.length();
|
|
}
|
|
template<class Left, class Right>
|
|
template<class Alloc>
|
|
string_cat_expr<Left,Right>::operator string_intermediary<Alloc>(void)
|
|
noexcept(std::is_nothrow_constructible<string_intermediary<Alloc>, size_t>::value &&
|
|
std::is_nothrow_invocable<detail::string_appender<string_intermediary<Alloc>>,decltype(*this)>::value)
|
|
{
|
|
size_t len = length();
|
|
string_intermediary<Alloc> ret(len);
|
|
detail::string_appender<string_intermediary<Alloc>> append(ret);
|
|
append(*this);
|
|
return ret;
|
|
}
|
|
template<class Left, class Right>
|
|
constexpr const Left& string_cat_expr<Left,Right>::left(void)const noexcept{
|
|
return m_l;
|
|
}
|
|
template<class Left, class Right>
|
|
constexpr const Right& string_cat_expr<Left,Right>::right(void)const noexcept{
|
|
return m_r;
|
|
}
|
|
|
|
|
|
constexpr static_string::static_string(const char* str, size_t len)noexcept:
|
|
string_base(const_cast<char*>(str), len, len){}
|
|
constexpr static_string::static_string(const static_string& s)noexcept:
|
|
string_base(s.m_data, s.m_length, s.m_length){}
|
|
constexpr static_string::static_string(static_string&& s)noexcept:
|
|
string_base(s.m_data, s.m_length, s.m_length){}
|
|
constexpr static_string& static_string::operator=(const static_string& s)noexcept{
|
|
m_data = s.m_data;
|
|
m_length = s.m_length;
|
|
return *this;
|
|
}
|
|
constexpr static_string::static_string(const char* c)noexcept:
|
|
static_string(const_cast<char*>(c), cx::strlen(c)){}
|
|
constexpr static_string& static_string::operator=(const char* c)noexcept{
|
|
m_data = const_cast<char*>(c);
|
|
m_length = cx::strlen(c);
|
|
return *this;
|
|
}
|
|
constexpr static_string& static_string::operator=(static_string&& s)noexcept{
|
|
m_data = s.m_data;
|
|
m_length = s.m_length;
|
|
return *this;
|
|
}
|
|
|
|
namespace detail{
|
|
template<class Targ>
|
|
constexpr string_appender<Targ>::string_appender(Targ& t)noexcept: m_targ(t){}
|
|
template<class Targ>
|
|
template<class L, class R>
|
|
constexpr void string_appender<Targ>::operator()(const string_cat_expr<L,R>& str)
|
|
noexcept(noexcept((*this)(str.left())) && noexcept((*this)(str.right())))
|
|
{
|
|
(*this)(str.left());
|
|
(*this)(str.right());
|
|
}
|
|
template<class Targ>
|
|
template<class Str, std::enable_if_t<!rexy::is_template_type<Str,string_cat_expr>::value,int>>
|
|
constexpr void string_appender<Targ>::operator()(Str&& str)
|
|
noexcept(noexcept(m_targ.append(str.get(), str.length())))
|
|
{
|
|
m_targ.append(str.get(), str.length());
|
|
}
|
|
}
|
|
|
|
} //namespace rexy
|
|
|
|
#undef STOP_STRICT_ALIAS_WARNING
|
|
|
|
#endif
|