Generisize string types

This commit is contained in:
rexy712 2020-08-01 01:28:29 -07:00
parent 55c908686f
commit f7b0822dd2
11 changed files with 260 additions and 164 deletions

View File

@ -24,8 +24,8 @@
namespace rexy::cx{
template<>
struct hash<rexy::static_string> : public string_hash<rexy::static_string>{};
template<class Char>
struct hash<rexy::static_string<Char>> : public string_hash<rexy::static_string<Char>>{};
}

View File

@ -69,7 +69,7 @@ namespace rexy::cx{
}
}
constexpr string(const rexy::static_string& str)noexcept:
constexpr string(const rexy::static_string<char>& str)noexcept:
m_length(str.length())
{
for(size_t i = 0;i < m_length;++i){
@ -153,7 +153,7 @@ namespace rexy::cx{
constexpr void append(const string& s)noexcept{
append(s.get(), s.length());
}
constexpr void append(const rexy::static_string& s)noexcept{
constexpr void append(const rexy::static_string<char>& s)noexcept{
append(s.get(), s.length());
}
};
@ -179,11 +179,11 @@ namespace rexy::cx{
}
template<class Str1, std::enable_if_t<detail::is_cx_string<Str1>::value,int> = 0>
constexpr auto operator+(Str1&& l, const char* r)noexcept{
return string_cat_expr(std::forward<Str1>(l), rexy::static_string(r));
return string_cat_expr(std::forward<Str1>(l), rexy::static_string<char>(r));
}
template<class Str1, std::enable_if_t<detail::is_cx_string<Str1>::value,int> = 0>
constexpr auto operator+(const char* l, Str1&& r)noexcept{
return string_cat_expr(rexy::static_string(l), std::forward<Str1>(r));
return string_cat_expr(rexy::static_string<char>(l), std::forward<Str1>(r));
}
}

View File

@ -85,6 +85,21 @@ namespace rexy::cx{
for(;c[i];++i);
return i;
}
constexpr size_t strlen(const wchar_t* c)noexcept{
size_t i = 0;
for(;c[i];++i);
return i;
}
constexpr size_t strlen(const char16_t* c)noexcept{
size_t i = 0;
for(;c[i];++i);
return i;
}
constexpr size_t strlen(const char32_t* c)noexcept{
size_t i = 0;
for(;c[i];++i);
return i;
}
constexpr int strcmp(const char* l, const char* r)noexcept{
using uchar = unsigned char;
for(;*l == *r && *l;++l, ++r);

View File

@ -37,7 +37,7 @@ namespace rexy{
return Str(rexy::steal(b.release()), b.size());
}
template<class Bin, detail::enable_if_binary<Bin> = 0>
auto string_to_binary(const string_base& s)
auto string_to_binary(const string_base<char>& s)
noexcept(std::is_nothrow_constructible<Bin, decltype(s.length())>::value &&
noexcept(std::decay_t<Bin>::allocator_type::allocate(0)))
{
@ -46,7 +46,7 @@ namespace rexy{
return b;
}
template<class Al, class Bin, 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(basic_string<Al>&& s)noexcept{
auto string_to_binary(basic_string<char,Al>&& s)noexcept{
return Bin(rexy::steal(s.release()), s.length());
}

View File

@ -22,6 +22,7 @@
#include <cstdlib> //size_t
#include <type_traits> //true_type, false_type
#include <new>
#include <limits> //numeric_limits
namespace rexy{
@ -61,7 +62,7 @@ namespace rexy{
~default_allocator(void) = default;
pointer allocate(size_type n){
size_type bytes = has_overflow(n) ? size_type{-1} : n*sizeof(T);
size_type bytes = has_overflow(n) ? std::numeric_limits<size_type>::max() : n*sizeof(T);
if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){
return reinterpret_cast<pointer>(::operator new(bytes));
}else{
@ -69,15 +70,24 @@ namespace rexy{
}
}
void deallocate(pointer p, size_type n){
size_type bytes = has_overflow(n) ? size_type{-1} : n*sizeof(T);
#if defined(__clang__) && !defined(__cpp_sized_deallocation)
//clang does not enable ::operator delete(void* ptr, std::size_t sz) by default for some reason
if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){
::operator delete(p);
}else{
::operator delete(p, static_cast<std::align_val_t>(alignof(T)));
}
#else
size_type bytes = has_overflow(n) ? std::numeric_limits<size_type>::max() : n*sizeof(T);
if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){
::operator delete(p, bytes);
}else{
::operator delete(p, bytes, static_cast<std::align_val_t>(alignof(T)));
}
#endif
}
constexpr size_type max_size(void)const{
return size_type{-1}/sizeof(T);
return std::numeric_limits<size_type>::max()/sizeof(T);
}
};
template<class T, class U>

View File

@ -66,7 +66,7 @@ namespace rexy{
rexy::binary read_bin(size_t bytes)noexcept(std::is_nothrow_constructible<rexy::binary, rexy::steal<char*>, size_t, size_t>::value);
size_t write(const char* c, size_t bytes)noexcept;
size_t write(const rexy::string_base&)noexcept;
size_t write(const rexy::string_base<char>& s)noexcept;
};
}

View File

@ -25,9 +25,9 @@
namespace rexy{
//new allocated string
using string = basic_string<detail::default_allocator<char>>;
using string = basic_string<char,detail::default_allocator<char>>;
extern template class basic_string<detail::default_allocator<char>>;
extern template class basic_string<char,detail::default_allocator<char>>;
}

View File

@ -21,7 +21,7 @@
#include <type_traits> //is_same, integral_contant, enable_if, etc
#include <utility> //forward
#include <cstdlib> //size_t
#include <cstddef> //size_t,ptrdiff
#include <cstring> //strlen
#include "steal.hpp"
@ -34,21 +34,33 @@
namespace rexy{
//Base of all RAII strings. Its use is allowing passing of rexy strings to functions without knowing the exact type
template<class Char>
class string_base
{
public:
using value_type = Char;
using size_type = size_t;
using difference_type = ptrdiff_t;
using pointer = value_type*;
using const_pointer = const value_type*;
using reference = value_type&;
using const_reference = const value_type&;
using iterator = pointer;
using const_iterator = const_pointer;
protected:
size_t m_length = 0; //length of string not including null terminator
size_t m_cap = 0; //size of current buffer not including null terminator
char* m_data = nullptr;
size_type m_length = 0; //length of string not including null terminator
size_type m_cap = 0; //size of current buffer not including null terminator
pointer m_data = nullptr;
protected:
constexpr string_base(void)noexcept = default;
constexpr string_base(size_t len)noexcept:
constexpr string_base(size_type len)noexcept:
m_cap(len){}
//Initialize without copying
constexpr string_base(char* data, size_t len)noexcept:
constexpr string_base(pointer data, size_type len)noexcept:
m_cap(len), m_data(data){}
constexpr string_base(char* data, size_t len, size_t cap)noexcept:
constexpr string_base(pointer data, size_type len, size_type cap)noexcept:
m_length(len), m_cap(cap), m_data(data){}
//Copy ctor (do nothing)
constexpr string_base(const string_base&)noexcept{}
@ -56,53 +68,68 @@ namespace rexy{
public:
//Stop managing stored pointer. Does not free.
constexpr char* release(void)noexcept{return cx::exchange(m_data, nullptr);}
constexpr pointer release(void)noexcept{return cx::exchange(m_data, nullptr);}
//Length of string not including null terminator
constexpr size_t length(void)const noexcept{return m_length;}
constexpr size_t capacity(void)const noexcept{return m_cap;}
constexpr size_type length(void)const noexcept{return m_length;}
constexpr size_type capacity(void)const noexcept{return m_cap;}
//direct access to managed pointer
constexpr char* c_str(void)noexcept{return m_data;}
constexpr const char* c_str(void)const noexcept{return m_data;}
constexpr char* get(void)noexcept{return m_data;}
constexpr const char* get(void)const noexcept{return m_data;}
constexpr operator char*(void)noexcept{return m_data;}
constexpr operator const char*(void)const noexcept{return m_data;}
constexpr pointer c_str(void)noexcept{return m_data;}
constexpr const_pointer c_str(void)const noexcept{return m_data;}
constexpr pointer get(void)noexcept{return m_data;}
constexpr const_pointer get(void)const noexcept{return m_data;}
constexpr operator pointer(void)noexcept{return m_data;}
constexpr operator const_pointer(void)const noexcept{return m_data;}
//true if m_data is not null
constexpr bool valid(void)const noexcept{return m_data;}
constexpr char& operator[](size_t i)noexcept{return m_data[i];}
constexpr const char& operator[](size_t i)const noexcept{return m_data[i];}
constexpr reference operator[](size_type i)noexcept{return m_data[i];}
constexpr const_reference operator[](size_type i)const noexcept{return m_data[i];}
};
//Supplies all functions that string_base can't implement
template<class Allocator>
class basic_string : protected detail::hasallocator<Allocator>, public string_base
template<class Char, class Allocator>
class basic_string : protected detail::hasallocator<Allocator>, public string_base<Char>
{
public:
using value_type = typename string_base<Char>::value_type;
using size_type = typename string_base<Char>::size_type;
using difference_type = typename string_base<Char>::difference_type;
using pointer = typename string_base<Char>::pointer;
using const_pointer = typename string_base<Char>::const_pointer;
using reference = typename string_base<Char>::reference;
using const_reference = typename string_base<Char>::const_reference;
using iterator = typename string_base<Char>::iterator;
using const_iterator = typename string_base<Char>::const_iterator;
using allocator_type = Allocator;
protected:
using string_base<Char>::m_data;
using string_base<Char>::m_length;
using string_base<Char>::m_cap;
private:
basic_string& _copy_string(const char* s, size_t len)
basic_string& _copy_string(const_pointer s, size_type len)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
public:
constexpr basic_string(void)noexcept;
constexpr basic_string(rexy::steal<char*> data, size_t len)noexcept;
constexpr basic_string(rexy::steal<char*> data, size_t len, size_t cap)noexcept;
constexpr basic_string(rexy::steal<char*> data)noexcept;
basic_string(const char* data, size_t len)noexcept(noexcept(this->allocate(data,len)));
basic_string(const char* data)noexcept(noexcept(this->allocate(data, m_cap)));
explicit basic_string(size_t len)noexcept(noexcept(this->allocate(len)));
basic_string(size_t len, size_t cap)noexcept(noexcept(this->allocate(len)));
constexpr basic_string(rexy::steal<pointer> data, size_type len)noexcept;
constexpr basic_string(rexy::steal<pointer> data, size_type len, size_type cap)noexcept;
constexpr basic_string(rexy::steal<pointer> data)noexcept;
basic_string(const_pointer data, size_type len)noexcept(noexcept(this->allocate(0)));
basic_string(const_pointer data)noexcept(noexcept(this->allocate(0)));
explicit basic_string(size_type len)noexcept(noexcept(this->allocate(0)));
basic_string(size_type len, size_type cap)noexcept(noexcept(this->allocate(0)));
//normal copy and move ctors
basic_string(const basic_string& b)noexcept(noexcept(this->allocate(b.m_data, b.m_length)));
basic_string(const basic_string& b)noexcept(noexcept(this->allocate(0)));
constexpr basic_string(basic_string&& s)noexcept(noexcept(cx::exchange(s.m_data, nullptr)));
basic_string(const string_base& b)noexcept(noexcept(this->allocate(b.get(), b.length())));
template<class C>
basic_string(const string_base<C>& b)noexcept(noexcept(this->allocate(0)));
//dtor
~basic_string(void)noexcept(noexcept(this->deallocate(nullptr, 0)));
@ -113,28 +140,30 @@ namespace rexy{
constexpr basic_string& operator=(basic_string&& s)noexcept;
//Copy from c string
basic_string& operator=(const char* c)
basic_string& operator=(const_pointer c)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
//Copy from other string_base
basic_string& operator=(const string_base& s)
template<class C>
basic_string& operator=(const string_base<C>& s)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
//Replace managed pointer. Frees existing value
void reset(char* val = nullptr)noexcept(noexcept(this->deallocate(m_data)));
void reset(char* val, size_t len)noexcept(noexcept(this->deallocate(m_data)));
bool resize(size_t newsize)
void reset(pointer val = nullptr)noexcept(noexcept(this->deallocate(nullptr,0)));
void reset(pointer val, size_type len)noexcept(noexcept(this->deallocate(nullptr,0)));
bool resize(size_type newsize)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
void append(const char* data, size_t len)
void append(const_pointer data, size_type len)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
void append(const char* data)
void append(const_pointer data)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
void append(const string_base& s)
template<class C>
void append(const string_base<C>& s)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
@ -145,32 +174,64 @@ namespace rexy{
template<class Left, class Right>
class string_cat_expr : public rexy::binary_expression<Left,Right>
{
static_assert(std::is_same<typename std::decay_t<Left>::value_type,typename std::decay_t<Right>::value_type>::value);
private:
using left_t = std::decay_t<Left>;
using right_t = std::decay_t<Right>;
public:
using value_type = typename left_t::value_type;
using size_type = decltype(typename left_t::size_type{0} + typename right_t::size_type{0});
using difference_type = decltype(typename left_t::difference_type{0} - typename right_t::difference_type{0});
using pointer = value_type*;
using const_pointer = const value_type*;
using reference = value_type&;
using const_reference = const value_type&;
using iterator = value_type*;
using const_iterator = const value_type*;
public:
using binary_expression<Left,Right>::binary_expression;
constexpr string_cat_expr(const string_cat_expr&) = default;
constexpr string_cat_expr(string_cat_expr&&) = default;
constexpr size_t length(void)const noexcept;
constexpr size_type length(void)const noexcept;
template<class Alloc>
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);
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);
};
template<class Left, class Right>
string_cat_expr(Left&&,Right&&) -> string_cat_expr<Left&&,Right&&>;
class static_string : public string_base
template<class Char>
class static_string : public string_base<Char>
{
public:
using value_type = typename string_base<Char>::value_type;
using size_type = typename string_base<Char>::size_type;
using difference_type = typename string_base<Char>::difference_type;
using pointer = typename string_base<Char>::pointer;
using const_pointer = typename string_base<Char>::const_pointer;
using reference = typename string_base<Char>::reference;
using const_reference = typename string_base<Char>::const_reference;
using iterator = typename string_base<Char>::iterator;
using const_iterator = typename string_base<Char>::const_iterator;
protected:
using string_base<Char>::m_data;
using string_base<Char>::m_length;
using string_base<Char>::m_cap;
public:
constexpr static_string(void)noexcept = default;
constexpr static_string(const char* str, size_t len)noexcept;
constexpr static_string(const char* c)noexcept;
constexpr static_string(const_pointer str, size_type len)noexcept;
constexpr static_string(const_pointer c)noexcept;
constexpr static_string(const static_string& s)noexcept;
constexpr static_string(static_string&& s)noexcept;
~static_string(void)noexcept = default;
constexpr static_string& operator=(const char* c)noexcept;
constexpr static_string& operator=(const_pointer c)noexcept;
constexpr static_string& operator=(const static_string& s)noexcept;
constexpr static_string& operator=(static_string&&)noexcept;
};
@ -178,11 +239,11 @@ namespace rexy{
template<class T>
struct is_string{
static constexpr bool value = rexy::is_type<T,string_base>::value || rexy::is_template_type<T,string_cat_expr>::value;
static constexpr bool value = rexy::is_template_derived_type<T,string_base>::value || rexy::is_template_type<T,string_cat_expr>::value;
};
template<class T>
struct is_concrete_string{
static constexpr bool value = rexy::is_type<T,string_base>::value;
static constexpr bool value = rexy::is_template_derived_type<T,string_base>::value;
};
namespace detail{
@ -213,16 +274,16 @@ namespace rexy{
}
template<class Right, detail::enable_if_string<Right> = 0>
constexpr auto operator+(const char* left, Right&& right)
noexcept(noexcept(::new (nullptr) string_cat_expr(rexy::static_string(left), std::forward<Right>(right))))
noexcept(noexcept(::new (nullptr) string_cat_expr(rexy::static_string<char>(left), std::forward<Right>(right))))
{
return string_cat_expr(rexy::static_string(left), std::forward<Right>(right));
return string_cat_expr(rexy::static_string<char>(left), std::forward<Right>(right));
}
template<class Left, detail::enable_if_string<Left> = 0>
constexpr auto operator+(Left&& left, const char* right)
noexcept(noexcept(::new (nullptr) string_cat_expr(std::forward<Left>(left), rexy::static_string(right))))
noexcept(noexcept(::new (nullptr) string_cat_expr(std::forward<Left>(left), rexy::static_string<char>(right))))
{
return rexy::string_cat_expr(std::forward<Left>(left), rexy::static_string(right));
return rexy::string_cat_expr(std::forward<Left>(left), rexy::static_string<char>(right));
}
template<class Left, class Right, detail::enable_if_concrete_string<Left> = 0, detail::enable_if_string<Right> = 0>
@ -242,8 +303,8 @@ namespace rexy{
#include "string_base.tpp"
namespace{
constexpr inline rexy::static_string operator"" _ss(const char* str, size_t len)noexcept{
return rexy::static_string(str, len);
constexpr inline rexy::static_string<char> operator"" _ss(const char* str, size_t len)noexcept{
return rexy::static_string<char>(str, len);
}
}

View File

@ -30,121 +30,123 @@
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)
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 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)
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(len ? this->allocate(len+1) : nullptr, len, len)
string_base<Char>(len ? this->allocate(sizeof(Char)*(len+1)) : nullptr, len, len)
{
if(len){
memcpy(m_data, data, len);
memcpy(m_data, data, len*sizeof(Char));
m_data[len] = 0;
}
}
template<class Allocator>
basic_string<Allocator>::basic_string(const char* data)
template<class Char, class Allocator>
basic_string<Char,Allocator>::basic_string(const_pointer data)
noexcept(noexcept(this->allocate(0))):
string_base(data ? strlen(data) : 0)
string_base<Char>(data ? cx::strlen(data) : 0)
{
if(m_cap){
m_data = this->allocate(m_cap+1);
memcpy(m_data, data, 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 Allocator>
basic_string<Allocator>::basic_string(size_t len)
template<class Char, class Allocator>
basic_string<Char,Allocator>::basic_string(size_type len)
noexcept(noexcept(this->allocate(0))):
string_base(len ? this->allocate(len+1) : nullptr, len)
string_base<Char>(len ? this->allocate(sizeof(Char)*(len+1)) : nullptr, len)
{
if(len)
m_data[len] = 0;
}
template<class Allocator>
basic_string<Allocator>::basic_string(size_t len, size_t cap)
noexcept(noexcept(this->allocate(len))):
string_base(len ? this->allocate(len+1) : nullptr, len, cap)
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 Allocator>
basic_string<Allocator>::basic_string(const basic_string& b)
template<class Char, class Allocator>
basic_string<Char,Allocator>::basic_string(const basic_string& b)
noexcept(noexcept(this->allocate(0))):
string_base(b.m_length ? this->allocate(b.m_length+1) : nullptr, b.m_length, b.m_length)
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, b.m_length+1);
memcpy(m_data, b.m_data, sizeof(Char)*(b.m_length+1));
}
template<class Allocator>
constexpr basic_string<Allocator>::basic_string(basic_string&& s)
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(cx::exchange(s.m_data, nullptr), s.m_length, s.m_cap){}
string_base<Char>(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(this->allocate(b.length()))):
string_base(b.length() ? this->allocate(b.length()+1) : nullptr, b.length(), b.length())
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(), b.length()+1);
memcpy(m_data, b.get(), sizeof(Char)*(b.length()+1));
}
//dtor
template<class Allocator>
basic_string<Allocator>::~basic_string(void)
template<class Char, class Allocator>
basic_string<Char,Allocator>::~basic_string(void)
noexcept(noexcept(this->deallocate(nullptr, 0)))
{
this->deallocate(m_data, m_cap+1);
this->deallocate(m_data, sizeof(Char)*(m_cap+1));
}
template<class Allocator>
basic_string<Allocator>& basic_string<Allocator>::operator=(const basic_string& s)
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, s.m_length+1);
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 Allocator>
constexpr basic_string<Allocator>& basic_string<Allocator>::operator=(basic_string&& s)noexcept{
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 Allocator>
basic_string<Allocator>& basic_string<Allocator>::operator=(const char* c)
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, strlen(c));
return _copy_string(c, cx::strlen(c));
}
//Copy from other string_base
template<class Allocator>
basic_string<Allocator>& basic_string<Allocator>::operator=(const string_base& s)
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)))
{
@ -152,26 +154,26 @@ namespace rexy{
}
//Replace managed pointer. Frees existing value
template<class Allocator>
void basic_string<Allocator>::reset(char* val)
noexcept(noexcept(this->deallocate(m_data,0)))
template<class Char, class Allocator>
void basic_string<Char,Allocator>::reset(pointer val)
noexcept(noexcept(this->deallocate(nullptr,0)))
{
this->deallocate(m_data,m_cap+1);
this->deallocate(m_data,sizeof(Char)*(m_cap+1));
m_data = val;
m_length = val ? strlen(val) : 0;
m_length = val ? cx::strlen(val) : 0;
m_cap = m_length;
}
template<class Allocator>
void basic_string<Allocator>::reset(char* val, size_t len)
noexcept(noexcept(this->deallocate(m_data,0)))
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,m_cap+1);
this->deallocate(m_data,sizeof(Char)*(m_cap+1));
m_data = val;
m_length = len;
m_cap = len;
}
template<class Allocator>
bool basic_string<Allocator>::resize(size_t newsize)
template<class Char, class Allocator>
bool basic_string<Char,Allocator>::resize(size_type newsize)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)))
{
@ -179,18 +181,18 @@ namespace rexy{
return false;
return (*this = basic_string(m_data, newsize));
}
template<class Allocator>
void basic_string<Allocator>::append(const char* data, size_t len)
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, len);
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, len);
memcpy(m_data, data, sizeof(Char)*len);
m_data[len] = 0;
}else{
auto newsize = cx::max(m_length+len, m_cap*2);
@ -200,32 +202,33 @@ namespace rexy{
*this = std::move(tmp);
}
}
template<class Allocator>
void basic_string<Allocator>::append(const char* data)
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, strlen(data));
append(data, cx::strlen(data));
}
template<class Allocator>
void basic_string<Allocator>::append(const string_base& s)
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 Allocator>
basic_string<Allocator>& basic_string<Allocator>::_copy_string(const char* s, size_t len)
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<char*>(nullptr), 0, 0));
return (*this = basic_string(rexy::steal<pointer>(nullptr), 0, 0));
if(len <= m_length){
m_length = len;
memcpy(m_data, s, len);
memcpy(m_data, s, sizeof(Char)*len);
m_data[len] = 0;
return *this;
}
@ -234,44 +237,51 @@ namespace rexy{
template<class Left, class Right>
constexpr size_t string_cat_expr<Left,Right>::length(void)const noexcept{
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<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)
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_t len = length();
basic_string<Alloc> ret(len);
detail::string_appender<basic_string<Alloc>> append(ret);
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;
}
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{
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;
}
constexpr static_string::static_string(const char* c)noexcept:
template<class Char>
constexpr static_string<Char>::static_string(const_pointer c)noexcept:
static_string(c, cx::strlen(c)){}
constexpr static_string& static_string::operator=(const char* c)noexcept{
m_data = const_cast<char*>(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;
}
constexpr static_string& static_string::operator=(static_string&& s)noexcept{
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;

View File

@ -106,7 +106,7 @@ namespace rexy{
size_t filerd::write(const char* c, size_t bytes)noexcept{
return fwrite(c, 1, bytes, m_fp);
}
size_t filerd::write(const rexy::string_base& c)noexcept{
size_t filerd::write(const rexy::string_base<char>& c)noexcept{
return write(c.get(), c.length());
}

View File

@ -21,6 +21,6 @@
namespace rexy{
template class basic_string<detail::default_allocator<char>>;
template class basic_string<char,detail::default_allocator<char>>;
}