rexylib/include/rexy/string_base.tpp

273 lines
8.6 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 Allocator>
constexpr basic_string<Allocator>::basic_string(void)noexcept{}
template<class Allocator>
constexpr basic_string<Allocator>::basic_string(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 basic_string<Allocator>::basic_string(rexy::steal<char*> data, size_t len)noexcept:
string_base(data.value(), len, len){}
template<class Allocator>
constexpr basic_string<Allocator>::basic_string(rexy::steal<char*> data, size_t len, size_t cap)noexcept:
string_base(data.value(), len, cap){}
template<class Allocator>
basic_string<Allocator>::basic_string(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>
basic_string<Allocator>::basic_string(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>
basic_string<Allocator>::basic_string(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>
basic_string<Allocator>::basic_string(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>
basic_string<Allocator>::basic_string(const basic_string& 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_length){}
template<class Allocator>
constexpr basic_string<Allocator>::basic_string(basic_string&& 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>
basic_string<Allocator>::basic_string(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.length()){}
//dtor
template<class Allocator>
basic_string<Allocator>::~basic_string(void)
noexcept(noexcept(Allocator::free(m_data)))
{
Allocator::free(m_data);
}
template<class Allocator>
basic_string<Allocator>& basic_string<Allocator>::operator=(const basic_string& 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;
}
basic_string tmp(s);
return (*this = std::move(tmp));
}
template<class Allocator>
constexpr basic_string<Allocator>& basic_string<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 Allocator>
basic_string<Allocator>& basic_string<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>
basic_string<Allocator>& basic_string<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 basic_string<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 basic_string<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 basic_string<Allocator>::resize(size_t newsize)
noexcept(noexcept(Allocator::copy(nullptr,0)) &&
noexcept(Allocator::free(nullptr)))
{
if(newsize < m_cap)
return false;
return (*this = basic_string(m_data, newsize));
}
template<class Allocator>
void basic_string<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 = basic_string(len, len);
memcpy(m_data, data, 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 Allocator>
void basic_string<Allocator>::append(const char* data)
noexcept(noexcept(Allocator::allocate(0)) &&
noexcept(Allocator::free(nullptr)))
{
if(data)
append(data, strlen(data));
}
template<class Allocator>
void basic_string<Allocator>::append(const string_base& s)
noexcept(noexcept(Allocator::allocate(0)) &&
noexcept(Allocator::free(nullptr)))
{
append(s.get(), s.length());
}
template<class Allocator>
basic_string<Allocator>& basic_string<Allocator>::_copy_string(const char* s, size_t len)
noexcept(noexcept(Allocator::copy(nullptr,0)) &&
noexcept(Allocator::free(nullptr)))
{
if(!len)
return (*this = basic_string(rexy::steal<char*>(nullptr), 0, 0));
if(len <= m_length){
m_length = len;
memcpy(m_data, s, len);
m_data[len] = 0;
return *this;
}
return (*this = basic_string(s, len));
}
template<class Left, class Right>
constexpr size_t string_cat_expr<Left,Right>::length(void)const noexcept{
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<Alloc>(void)
noexcept(std::is_nothrow_constructible<basic_string<Alloc>, size_t>::value &&
std::is_nothrow_invocable<detail::string_appender<basic_string<Alloc>>,decltype(*this)>::value)
{
size_t len = length();
basic_string<Alloc> ret(len);
detail::string_appender<basic_string<Alloc>> append(ret);
append(*this);
return ret;
}
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;
m_cap = s.m_cap;
return *this;
}
constexpr static_string::static_string(const char* c)noexcept:
static_string(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);
m_cap = m_length;
return *this;
}
constexpr static_string& static_string::operator=(static_string&& s)noexcept{
m_data = s.m_data;
m_length = s.m_length;
m_cap = s.m_cap;
return *this;
}
} //namespace rexy
#undef STOP_STRICT_ALIAS_WARNING
#endif