rexylib/include/rexy/string_base.tpp

301 lines
9.9 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
#include "detail/string_appender.hpp"
#define STOP_STRICT_ALIAS_WARNING(x) (x)
namespace rexy{
template<class Char, class Allocator>
constexpr basic_string<Char,Allocator>::basic_string(void)noexcept{}
template<class Char, class Allocator>
constexpr basic_string<Char,Allocator>::basic_string(rexy::steal<pointer> data)noexcept:
string_base<Char>(data.value() ? cx::strlen(data.value()) : 0)
{
m_data = data.value();
m_length = m_cap;
}
template<class Char, class Allocator>
constexpr basic_string<Char,Allocator>::basic_string(rexy::steal<pointer> data, size_type len)noexcept:
string_base<Char>(data.value(), len, len){}
template<class Char, class Allocator>
constexpr basic_string<Char,Allocator>::basic_string(rexy::steal<pointer> data, size_type len, size_type cap)noexcept:
string_base<Char>(data.value(), len, cap){}
template<class Char, class Allocator>
basic_string<Char,Allocator>::basic_string(const_pointer data, size_type len)
noexcept(noexcept(this->allocate(0))):
string_base<Char>(len ? this->allocate(sizeof(Char)*(len+1)) : nullptr, len, len)
{
if(len){
memcpy(m_data, data, len*sizeof(Char));
m_data[len] = 0;
}
}
template<class Char, class Allocator>
basic_string<Char,Allocator>::basic_string(const_pointer data)
noexcept(noexcept(this->allocate(0))):
string_base<Char>(data ? cx::strlen(data) : 0)
{
if(m_cap){
m_data = this->allocate(sizeof(Char)*(m_cap+1));
memcpy(m_data, data, sizeof(Char)*m_cap);
m_length = m_cap;
}
}
template<class Char, class Allocator>
basic_string<Char,Allocator>::basic_string(size_type len)
noexcept(noexcept(this->allocate(0))):
string_base<Char>(len ? this->allocate(sizeof(Char)*(len+1)) : nullptr, len)
{
if(len)
m_data[len] = 0;
}
template<class Char, class Allocator>
basic_string<Char,Allocator>::basic_string(size_type len, size_type cap)
noexcept(noexcept(this->allocate(0))):
string_base<Char>(len ? this->allocate(sizeof(Char)*(len+1)) : nullptr, len, cap)
{
if(len)
m_data[len] = 0;
}
//normal copy and move ctors
template<class Char, class Allocator>
basic_string<Char,Allocator>::basic_string(const basic_string& b)
noexcept(noexcept(this->allocate(0))):
string_base<Char>(b.m_length ? this->allocate(sizeof(Char)*(b.m_length+1)) : nullptr, b.m_length, b.m_length)
{
if(b.m_length)
memcpy(m_data, b.m_data, sizeof(Char)*(b.m_length+1));
}
template<class Char, class Allocator>
constexpr basic_string<Char,Allocator>::basic_string(basic_string&& s)
noexcept(noexcept(cx::exchange(s.m_data, nullptr))):
string_base<Char>(cx::exchange(s.m_data, nullptr), s.m_length, s.m_cap){}
template<class Char, class Allocator>
template<class C>
basic_string<Char,Allocator>::basic_string(const string_base<C>& b)
noexcept(noexcept(this->allocate(0))):
string_base<Char>(b.length() ? this->allocate(sizeof(Char)*(b.length()+1)) : nullptr, b.length(), b.length())
{
if(b.length())
memcpy(m_data, b.get(), sizeof(Char)*(b.length()+1));
}
//dtor
template<class Char, class Allocator>
basic_string<Char,Allocator>::~basic_string(void)
noexcept(noexcept(this->deallocate(nullptr, 0)))
{
this->deallocate(m_data, sizeof(Char)*(m_cap+1));
}
template<class Char, class Allocator>
basic_string<Char,Allocator>& basic_string<Char,Allocator>::operator=(const basic_string& s)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr, 0)))
{
if(s.m_length < m_cap){
memcpy(m_data, s.m_data, sizeof(Char)*(s.m_length+1));
m_length = s.m_length;
return *this;
}
basic_string tmp(s);
return (*this = std::move(tmp));
}
template<class Char, class Allocator>
constexpr basic_string<Char,Allocator>& basic_string<Char,Allocator>::operator=(basic_string&& 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 Char, class Allocator>
basic_string<Char,Allocator>& basic_string<Char,Allocator>::operator=(const_pointer c)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)))
{
return _copy_string(c, cx::strlen(c));
}
//Copy from other string_base
template<class Char, class Allocator>
template<class C>
basic_string<Char,Allocator>& basic_string<Char,Allocator>::operator=(const string_base<C>& s)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)))
{
return _copy_string(s.get(), s.length());
}
//Replace managed pointer. Frees existing value
template<class Char, class Allocator>
void basic_string<Char,Allocator>::reset(pointer val)
noexcept(noexcept(this->deallocate(nullptr,0)))
{
this->deallocate(m_data,sizeof(Char)*(m_cap+1));
m_data = val;
m_length = val ? cx::strlen(val) : 0;
m_cap = m_length;
}
template<class Char, class Allocator>
void basic_string<Char,Allocator>::reset(pointer val, size_type len)
noexcept(noexcept(this->deallocate(nullptr,0)))
{
this->deallocate(m_data,sizeof(Char)*(m_cap+1));
m_data = val;
m_length = len;
m_cap = len;
}
template<class Char, class Allocator>
bool basic_string<Char,Allocator>::resize(size_type newsize)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)))
{
if(newsize < m_cap)
return false;
return (*this = basic_string(m_data, newsize));
}
template<class Char, class Allocator>
void basic_string<Char,Allocator>::append(const_pointer data, size_type len)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)))
{
if(len+m_length <= m_cap){
memcpy(m_data+m_length, data, sizeof(Char)*len);
m_length += len;
m_data[m_length] = 0;
}else if(!m_data){
*this = basic_string(len, len);
memcpy(m_data, data, sizeof(Char)*len);
m_data[len] = 0;
}else{
auto newsize = cx::max(m_length+len, m_cap*2);
basic_string tmp(newsize);
tmp.append(m_data, m_length);
tmp.append(data, len);
*this = std::move(tmp);
}
}
template<class Char, class Allocator>
void basic_string<Char,Allocator>::append(const_pointer data)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)))
{
if(data)
append(data, cx::strlen(data));
}
template<class Char, class Allocator>
template<class C>
void basic_string<Char,Allocator>::append(const string_base<C>& s)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)))
{
append(s.get(), s.length());
}
template<class Char, class Allocator>
basic_string<Char,Allocator>& basic_string<Char,Allocator>::_copy_string(const_pointer s, size_type len)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)))
{
if(!len)
return (*this = basic_string(rexy::steal<pointer>(nullptr), 0, 0));
if(len <= m_length){
m_length = len;
memcpy(m_data, s, sizeof(Char)*len);
m_data[len] = 0;
return *this;
}
return (*this = basic_string(s, len));
}
template<class Left, class Right>
constexpr auto string_cat_expr<Left,Right>::length(void)const noexcept -> size_type{
return this->m_l.length() + this->m_r.length();
}
template<class Left, class Right>
template<class Alloc>
string_cat_expr<Left,Right>::operator basic_string<value_type,Alloc>(void)
noexcept(std::is_nothrow_constructible<basic_string<value_type,Alloc>, typename basic_string<value_type,Alloc>::size_type>::value &&
std::is_nothrow_invocable<detail::string_appender<basic_string<value_type,Alloc>>,decltype(*this)>::value)
{
size_type len = length();
basic_string<value_type,Alloc> ret(len);
detail::string_appender<basic_string<value_type,Alloc>> append(ret);
append(*this);
return ret;
}
template<class Char>
constexpr static_string<Char>::static_string(const_pointer str, size_type len)noexcept:
string_base<Char>(const_cast<pointer>(str), len, len){}
template<class Char>
constexpr static_string<Char>::static_string(const static_string& s)noexcept:
string_base<Char>(s.m_data, s.m_length, s.m_length){}
template<class Char>
constexpr static_string<Char>::static_string(static_string&& s)noexcept:
string_base<Char>(s.m_data, s.m_length, s.m_length){}
template<class Char>
constexpr static_string<Char>& static_string<Char>::operator=(const static_string& s)noexcept{
m_data = s.m_data;
m_length = s.m_length;
m_cap = s.m_cap;
return *this;
}
template<class Char>
constexpr static_string<Char>::static_string(const_pointer c)noexcept:
static_string(c, cx::strlen(c)){}
template<class Char>
constexpr static_string<Char>& static_string<Char>::operator=(const_pointer c)noexcept{
m_data = const_cast<pointer>(c);
m_length = cx::strlen(c);
m_cap = m_length;
return *this;
}
template<class Char>
constexpr static_string<Char>& static_string<Char>::operator=(static_string&& s)noexcept{
m_data = s.m_data;
m_length = s.m_length;
m_cap = s.m_cap;
return *this;
}
extern template class static_string<char>;
extern template class static_string<wchar_t>;
extern template class static_string<char16_t>;
extern template class static_string<char32_t>;
} //namespace rexy
#undef STOP_STRICT_ALIAS_WARNING
#endif