/** 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 . */ #ifndef REXY_STRING_BASE_HPP #define REXY_STRING_BASE_HPP #include //is_same, integral_contant, enable_if, etc #include //forward #include //size_t #include //strlen #include "steal.hpp" #include "cx/utility.hpp" #include "traits.hpp" #include "expression.hpp" #include "detail/string_appender.hpp" #include "detail/hasallocator.hpp" namespace rexy{ //Base of all RAII strings. Its use is allowing passing of rexy strings to functions without knowing the exact type class string_base { 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; protected: constexpr string_base(void)noexcept = default; constexpr string_base(size_t len)noexcept: m_cap(len){} //Initialize without copying constexpr string_base(char* data, size_t len)noexcept: m_cap(len), m_data(data){} constexpr string_base(char* data, size_t len, size_t cap)noexcept: m_length(len), m_cap(cap), m_data(data){} //Copy ctor (do nothing) constexpr string_base(const string_base&)noexcept{} ~string_base(void)noexcept = default; public: //Stop managing stored pointer. Does not free. constexpr char* 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;} //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;} //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];} }; //Supplies all functions that string_base can't implement template class basic_string : private detail::hasallocator, public string_base { public: using allocator_type = Allocator; private: basic_string& _copy_string(const char* s, size_t 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))); //normal copy and move ctors basic_string(const basic_string& b)noexcept(noexcept(this->allocate(b.m_data, b.m_length))); 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()))); //dtor ~basic_string(void)noexcept(noexcept(this->deallocate(nullptr, 0))); basic_string& operator=(const basic_string& s) noexcept(noexcept(this->allocate(0)) && noexcept(this->deallocate(nullptr,0))); constexpr basic_string& operator=(basic_string&& s)noexcept; //Copy from c string basic_string& operator=(const char* c) noexcept(noexcept(this->allocate(0)) && noexcept(this->deallocate(nullptr,0))); //Copy from other string_base 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) noexcept(noexcept(this->allocate(0)) && noexcept(this->deallocate(nullptr,0))); void append(const char* data, size_t len) noexcept(noexcept(this->allocate(0)) && noexcept(this->deallocate(nullptr,0))); void append(const char* data) noexcept(noexcept(this->allocate(0)) && noexcept(this->deallocate(nullptr,0))); void append(const string_base& s) noexcept(noexcept(this->allocate(0)) && noexcept(this->deallocate(nullptr,0))); using detail::hasallocator::allocator; }; //Like an expression template but not really template class string_cat_expr : public rexy::binary_expression { 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; template operator basic_string(void) noexcept(std::is_nothrow_constructible, size_t>::value && std::is_nothrow_invocable>,decltype(*this)>::value); }; template string_cat_expr(Left&&,Right&&) -> string_cat_expr; class static_string : public string_base { 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 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 static_string& s)noexcept; constexpr static_string& operator=(static_string&&)noexcept; }; template struct is_string{ static constexpr bool value = rexy::is_type::value || rexy::is_template_type::value; }; template struct is_concrete_string{ static constexpr bool value = rexy::is_type::value; }; namespace detail{ template using enable_if_string = std::enable_if_t<(is_string::value && ...),int>; template using enable_if_concrete_string = std::enable_if_t<(is_concrete_string::value && ...),int>; template using enable_if_expr_string = std::enable_if_t<(rexy::is_template_type::value && ...),int>; } //namespace detail template = 0> constexpr bool operator==(Str1&& left, Str2&& right)noexcept{ return left.valid() && right.valid() && left.length() == right.length() && !cx::strcmp(left.get(), right.get()); } template = 0> constexpr bool operator!=(Str1&& left, Str2&& right)noexcept{ return !(left == right); } template = 0> constexpr auto operator+(Left&& l, Right&& r) //uses deduction guide whereas std::is_nothrow_constructible couldn't noexcept(noexcept(::new (nullptr) string_cat_expr(std::forward(l), std::forward(r)))) { return string_cat_expr(std::forward(l), std::forward(r)); } template = 0> constexpr auto operator+(const char* left, Right&& 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)); } template = 0> constexpr auto operator+(Left&& left, const char* 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)); } template = 0, detail::enable_if_string = 0> decltype(auto) operator+=(Left& l, Right&& r) noexcept(noexcept(l + std::forward(r)) && std::is_nothrow_assignable(r))>::value) { return l = (l + std::forward(r)); } template = 0> decltype(auto) operator+=(Left& l, const char* r) noexcept(noexcept(l + r) && std::is_nothrow_assignable::value) { return l = (l + r); } } #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); } } #ifdef REXY_BINARY_BASE_HPP #include "detail/binary_string_conv.hpp" #endif #ifdef REXY_CX_HASH_HPP #include "cx/static_string_hash.hpp" #endif #endif