From f7b0822dd28f52d73bfcff981ee53fa1fb2e8bb6 Mon Sep 17 00:00:00 2001 From: rexy712 Date: Sat, 1 Aug 2020 01:28:29 -0700 Subject: [PATCH] Generisize string types --- include/rexy/cx/static_string_hash.hpp | 4 +- include/rexy/cx/string.hpp | 8 +- include/rexy/cx/utility.hpp | 15 ++ include/rexy/detail/binary_string_conv.hpp | 4 +- include/rexy/detail/default_allocator.hpp | 16 +- include/rexy/filerd.hpp | 2 +- include/rexy/string.hpp | 4 +- include/rexy/string_base.hpp | 169 ++++++++++++------ include/rexy/string_base.tpp | 198 +++++++++++---------- src/filerd.cpp | 2 +- src/string.cpp | 2 +- 11 files changed, 260 insertions(+), 164 deletions(-) diff --git a/include/rexy/cx/static_string_hash.hpp b/include/rexy/cx/static_string_hash.hpp index 4fb8f8b..983deea 100644 --- a/include/rexy/cx/static_string_hash.hpp +++ b/include/rexy/cx/static_string_hash.hpp @@ -24,8 +24,8 @@ namespace rexy::cx{ - template<> - struct hash : public string_hash{}; + template + struct hash> : public string_hash>{}; } diff --git a/include/rexy/cx/string.hpp b/include/rexy/cx/string.hpp index ec973ac..508d626 100644 --- a/include/rexy/cx/string.hpp +++ b/include/rexy/cx/string.hpp @@ -69,7 +69,7 @@ namespace rexy::cx{ } } - constexpr string(const rexy::static_string& str)noexcept: + constexpr string(const rexy::static_string& 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& s)noexcept{ append(s.get(), s.length()); } }; @@ -179,11 +179,11 @@ namespace rexy::cx{ } template::value,int> = 0> constexpr auto operator+(Str1&& l, const char* r)noexcept{ - return string_cat_expr(std::forward(l), rexy::static_string(r)); + return string_cat_expr(std::forward(l), rexy::static_string(r)); } template::value,int> = 0> constexpr auto operator+(const char* l, Str1&& r)noexcept{ - return string_cat_expr(rexy::static_string(l), std::forward(r)); + return string_cat_expr(rexy::static_string(l), std::forward(r)); } } diff --git a/include/rexy/cx/utility.hpp b/include/rexy/cx/utility.hpp index 99adb77..4dcaa97 100644 --- a/include/rexy/cx/utility.hpp +++ b/include/rexy/cx/utility.hpp @@ -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); diff --git a/include/rexy/detail/binary_string_conv.hpp b/include/rexy/detail/binary_string_conv.hpp index 8ce1a1d..0bbd5b1 100644 --- a/include/rexy/detail/binary_string_conv.hpp +++ b/include/rexy/detail/binary_string_conv.hpp @@ -37,7 +37,7 @@ namespace rexy{ return Str(rexy::steal(b.release()), b.size()); } template = 0> - auto string_to_binary(const string_base& s) + auto string_to_binary(const string_base& s) noexcept(std::is_nothrow_constructible::value && noexcept(std::decay_t::allocator_type::allocate(0))) { @@ -46,7 +46,7 @@ namespace rexy{ return b; } template = 0, std::enable_if_t,typename Bin::allocator_type>::value,int> = 0> - auto string_to_binary(basic_string&& s)noexcept{ + auto string_to_binary(basic_string&& s)noexcept{ return Bin(rexy::steal(s.release()), s.length()); } diff --git a/include/rexy/detail/default_allocator.hpp b/include/rexy/detail/default_allocator.hpp index c9e35ca..08470ba 100644 --- a/include/rexy/detail/default_allocator.hpp +++ b/include/rexy/detail/default_allocator.hpp @@ -22,6 +22,7 @@ #include //size_t #include //true_type, false_type #include +#include //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::max() : n*sizeof(T); if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){ return reinterpret_cast(::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(alignof(T))); + } +#else + size_type bytes = has_overflow(n) ? std::numeric_limits::max() : n*sizeof(T); if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){ ::operator delete(p, bytes); }else{ ::operator delete(p, bytes, static_cast(alignof(T))); } +#endif } constexpr size_type max_size(void)const{ - return size_type{-1}/sizeof(T); + return std::numeric_limits::max()/sizeof(T); } }; template diff --git a/include/rexy/filerd.hpp b/include/rexy/filerd.hpp index 1ac83e9..be85cb0 100644 --- a/include/rexy/filerd.hpp +++ b/include/rexy/filerd.hpp @@ -66,7 +66,7 @@ namespace rexy{ rexy::binary read_bin(size_t bytes)noexcept(std::is_nothrow_constructible, 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& s)noexcept; }; } diff --git a/include/rexy/string.hpp b/include/rexy/string.hpp index 6e6d029..40ca2ed 100644 --- a/include/rexy/string.hpp +++ b/include/rexy/string.hpp @@ -25,9 +25,9 @@ namespace rexy{ //new allocated string - using string = basic_string>; + using string = basic_string>; - extern template class basic_string>; + extern template class basic_string>; } diff --git a/include/rexy/string_base.hpp b/include/rexy/string_base.hpp index 31ebabc..c337e03 100644 --- a/include/rexy/string_base.hpp +++ b/include/rexy/string_base.hpp @@ -21,7 +21,7 @@ #include //is_same, integral_contant, enable_if, etc #include //forward -#include //size_t +#include //size_t,ptrdiff #include //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 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 basic_string : protected detail::hasallocator, public string_base + template + class basic_string : protected detail::hasallocator, public string_base { public: + using value_type = typename string_base::value_type; + using size_type = typename string_base::size_type; + using difference_type = typename string_base::difference_type; + using pointer = typename string_base::pointer; + using const_pointer = typename string_base::const_pointer; + using reference = typename string_base::reference; + using const_reference = typename string_base::const_reference; + using iterator = typename string_base::iterator; + using const_iterator = typename string_base::const_iterator; using allocator_type = Allocator; + protected: + using string_base::m_data; + using string_base::m_length; + using string_base::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 data, size_t len)noexcept; - constexpr basic_string(rexy::steal data, size_t len, size_t cap)noexcept; - constexpr basic_string(rexy::steal 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 data, size_type len)noexcept; + constexpr basic_string(rexy::steal data, size_type len, size_type cap)noexcept; + constexpr basic_string(rexy::steal 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 + basic_string(const string_base& 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 + basic_string& operator=(const string_base& 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 + void append(const string_base& s) noexcept(noexcept(this->allocate(0)) && noexcept(this->deallocate(nullptr,0))); @@ -145,32 +174,64 @@ namespace rexy{ template class string_cat_expr : public rexy::binary_expression { + static_assert(std::is_same::value_type,typename std::decay_t::value_type>::value); + private: + using left_t = std::decay_t; + using right_t = std::decay_t; + 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::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 - operator basic_string(void) - noexcept(std::is_nothrow_constructible, size_t>::value && - std::is_nothrow_invocable>,decltype(*this)>::value); + operator basic_string(void) + noexcept(std::is_nothrow_constructible, typename basic_string::size_type>::value && + std::is_nothrow_invocable>,decltype(*this)>::value); }; template string_cat_expr(Left&&,Right&&) -> string_cat_expr; - class static_string : public string_base + template + class static_string : public string_base { + public: + using value_type = typename string_base::value_type; + using size_type = typename string_base::size_type; + using difference_type = typename string_base::difference_type; + using pointer = typename string_base::pointer; + using const_pointer = typename string_base::const_pointer; + using reference = typename string_base::reference; + using const_reference = typename string_base::const_reference; + using iterator = typename string_base::iterator; + using const_iterator = typename string_base::const_iterator; + + protected: + using string_base::m_data; + using string_base::m_length; + using string_base::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 struct is_string{ - static constexpr bool value = rexy::is_type::value || rexy::is_template_type::value; + static constexpr bool value = rexy::is_template_derived_type::value || rexy::is_template_type::value; }; template struct is_concrete_string{ - static constexpr bool value = rexy::is_type::value; + static constexpr bool value = rexy::is_template_derived_type::value; }; namespace detail{ @@ -213,16 +274,16 @@ namespace rexy{ } template = 0> constexpr auto operator+(const char* left, Right&& right) - noexcept(noexcept(::new (nullptr) string_cat_expr(rexy::static_string(left), std::forward(right)))) + noexcept(noexcept(::new (nullptr) string_cat_expr(rexy::static_string(left), std::forward(right)))) { - return string_cat_expr(rexy::static_string(left), std::forward(right)); + return string_cat_expr(rexy::static_string(left), std::forward(right)); } template = 0> constexpr auto operator+(Left&& left, const char* right) - noexcept(noexcept(::new (nullptr) string_cat_expr(std::forward(left), rexy::static_string(right)))) + noexcept(noexcept(::new (nullptr) string_cat_expr(std::forward(left), rexy::static_string(right)))) { - return rexy::string_cat_expr(std::forward(left), rexy::static_string(right)); + return rexy::string_cat_expr(std::forward(left), rexy::static_string(right)); } template = 0, detail::enable_if_string = 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 operator"" _ss(const char* str, size_t len)noexcept{ + return rexy::static_string(str, len); } } diff --git a/include/rexy/string_base.tpp b/include/rexy/string_base.tpp index da1bc4b..9cf2d1c 100644 --- a/include/rexy/string_base.tpp +++ b/include/rexy/string_base.tpp @@ -30,121 +30,123 @@ namespace rexy{ - template - constexpr basic_string::basic_string(void)noexcept{} - template - constexpr basic_string::basic_string(rexy::steal data)noexcept: - string_base(data.value() ? cx::strlen(data.value()) : 0) + template + constexpr basic_string::basic_string(void)noexcept{} + template + constexpr basic_string::basic_string(rexy::steal data)noexcept: + string_base(data.value() ? cx::strlen(data.value()) : 0) { m_data = data.value(); m_length = m_cap; } - template - constexpr basic_string::basic_string(rexy::steal data, size_t len)noexcept: - string_base(data.value(), len, len){} - template - constexpr basic_string::basic_string(rexy::steal data, size_t len, size_t cap)noexcept: - string_base(data.value(), len, cap){} - template - basic_string::basic_string(const char* data, size_t len) + template + constexpr basic_string::basic_string(rexy::steal data, size_type len)noexcept: + string_base(data.value(), len, len){} + template + constexpr basic_string::basic_string(rexy::steal data, size_type len, size_type cap)noexcept: + string_base(data.value(), len, cap){} + template + basic_string::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(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 - basic_string::basic_string(const char* data) + template + basic_string::basic_string(const_pointer data) noexcept(noexcept(this->allocate(0))): - string_base(data ? strlen(data) : 0) + string_base(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 - basic_string::basic_string(size_t len) + template + basic_string::basic_string(size_type len) noexcept(noexcept(this->allocate(0))): - string_base(len ? this->allocate(len+1) : nullptr, len) + string_base(len ? this->allocate(sizeof(Char)*(len+1)) : nullptr, len) { if(len) m_data[len] = 0; } - template - basic_string::basic_string(size_t len, size_t cap) - noexcept(noexcept(this->allocate(len))): - string_base(len ? this->allocate(len+1) : nullptr, len, cap) + template + basic_string::basic_string(size_type len, size_type cap) + noexcept(noexcept(this->allocate(0))): + string_base(len ? this->allocate(sizeof(Char)*(len+1)) : nullptr, len, cap) { if(len) m_data[len] = 0; } //normal copy and move ctors - template - basic_string::basic_string(const basic_string& b) + template + basic_string::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(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 - constexpr basic_string::basic_string(basic_string&& s) + template + constexpr basic_string::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(cx::exchange(s.m_data, nullptr), s.m_length, s.m_cap){} - template - basic_string::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 + template + basic_string::basic_string(const string_base& b) + noexcept(noexcept(this->allocate(0))): + string_base(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 - basic_string::~basic_string(void) + template + basic_string::~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 - basic_string& basic_string::operator=(const basic_string& s) + template + basic_string& basic_string::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 - constexpr basic_string& basic_string::operator=(basic_string&& s)noexcept{ + template + constexpr basic_string& basic_string::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 - basic_string& basic_string::operator=(const char* c) + template + basic_string& basic_string::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 - basic_string& basic_string::operator=(const string_base& s) + template + template + basic_string& basic_string::operator=(const string_base& s) noexcept(noexcept(this->allocate(0)) && noexcept(this->deallocate(nullptr,0))) { @@ -152,26 +154,26 @@ namespace rexy{ } //Replace managed pointer. Frees existing value - template - void basic_string::reset(char* val) - noexcept(noexcept(this->deallocate(m_data,0))) + template + void basic_string::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 - void basic_string::reset(char* val, size_t len) - noexcept(noexcept(this->deallocate(m_data,0))) + template + void basic_string::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 - bool basic_string::resize(size_t newsize) + template + bool basic_string::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 - void basic_string::append(const char* data, size_t len) + template + void basic_string::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 - void basic_string::append(const char* data) + template + void basic_string::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 - void basic_string::append(const string_base& s) + template + template + void basic_string::append(const string_base& s) noexcept(noexcept(this->allocate(0)) && noexcept(this->deallocate(nullptr,0))) { append(s.get(), s.length()); } - template - basic_string& basic_string::_copy_string(const char* s, size_t len) + template + basic_string& basic_string::_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(nullptr), 0, 0)); + return (*this = basic_string(rexy::steal(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 - constexpr size_t string_cat_expr::length(void)const noexcept{ + constexpr auto string_cat_expr::length(void)const noexcept -> size_type{ return this->m_l.length() + this->m_r.length(); } template template - string_cat_expr::operator basic_string(void) - noexcept(std::is_nothrow_constructible, size_t>::value && - std::is_nothrow_invocable>,decltype(*this)>::value) + string_cat_expr::operator basic_string(void) + noexcept(std::is_nothrow_constructible, typename basic_string::size_type>::value && + std::is_nothrow_invocable>,decltype(*this)>::value) { - size_t len = length(); - basic_string ret(len); - detail::string_appender> append(ret); + size_type len = length(); + basic_string ret(len); + detail::string_appender> append(ret); append(*this); return ret; } - constexpr static_string::static_string(const char* str, size_t len)noexcept: - string_base(const_cast(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 + constexpr static_string::static_string(const_pointer str, size_type len)noexcept: + string_base(const_cast(str), len, len){} + template + constexpr static_string::static_string(const static_string& s)noexcept: + string_base(s.m_data, s.m_length, s.m_length){} + template + constexpr static_string::static_string(static_string&& s)noexcept: + string_base(s.m_data, s.m_length, s.m_length){} + template + 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: + template + constexpr static_string::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(c); + template + constexpr static_string& static_string::operator=(const_pointer c)noexcept{ + m_data = const_cast(c); m_length = cx::strlen(c); m_cap = m_length; return *this; } - constexpr static_string& static_string::operator=(static_string&& s)noexcept{ + template + 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; diff --git a/src/filerd.cpp b/src/filerd.cpp index 996cec2..8e056d4 100644 --- a/src/filerd.cpp +++ b/src/filerd.cpp @@ -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& c)noexcept{ return write(c.get(), c.length()); } diff --git a/src/string.cpp b/src/string.cpp index 2abbf3d..fa3b53a 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -21,6 +21,6 @@ namespace rexy{ - template class basic_string>; + template class basic_string>; }