rexylib/include/rexy/string_base.hpp

329 lines
13 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_HPP
#define REXY_STRING_BASE_HPP
#include <type_traits> //is_same, integral_contant, enable_if, etc
#include <utility> //forward
#include <cstddef> //size_t,ptrdiff
#include <cstring> //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
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_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_type len)noexcept:
m_cap(len){}
//Initialize without copying
constexpr string_base(pointer data, size_type len)noexcept:
m_cap(len), m_data(data){}
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{}
~string_base(void)noexcept = default;
public:
//Stop managing stored pointer. Does not free.
constexpr pointer release(void)noexcept{return cx::exchange(m_data, nullptr);}
//Length of string not including null terminator
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 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 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 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_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<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(0)));
constexpr basic_string(basic_string&& s)noexcept(noexcept(cx::exchange(s.m_data, nullptr)));
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)));
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_pointer c)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
//Copy from other string_base
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(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_pointer data, size_type len)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
void append(const_pointer data)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
template<class C>
void append(const string_base<C>& s)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
using detail::hasallocator<Allocator>::allocator;
};
//Like an expression template but not really
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_type length(void)const noexcept;
template<class Alloc>
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&&>;
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_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_pointer c)noexcept;
constexpr static_string& operator=(const static_string& s)noexcept;
constexpr static_string& operator=(static_string&&)noexcept;
};
template<class T>
struct is_string{
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_template_derived_type<T,string_base>::value;
};
namespace detail{
template<class... Ts>
using enable_if_string = std::enable_if_t<(is_string<Ts>::value && ...),int>;
template<class... Ts>
using enable_if_concrete_string = std::enable_if_t<(is_concrete_string<Ts>::value && ...),int>;
template<class... Ts>
using enable_if_expr_string = std::enable_if_t<(rexy::is_template_type<Ts,string_cat_expr>::value && ...),int>;
} //namespace detail
template<class Str1, class Str2, detail::enable_if_concrete_string<Str1,Str2> = 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<class Str1, class Str2, detail::enable_if_concrete_string<Str1,Str2> = 0>
constexpr bool operator!=(Str1&& left, Str2&& right)noexcept{
return !(left == right);
}
template<class Left, class Right, detail::enable_if_string<Left,Right> = 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<Left>(l), std::forward<Right>(r))))
{
return string_cat_expr(std::forward<Left>(l), std::forward<Right>(r));
}
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<char>(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<char>(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>
decltype(auto) operator+=(Left& l, Right&& r)
noexcept(noexcept(l + std::forward<Right>(r)) && std::is_nothrow_assignable<Left, decltype(l + std::forward<Right>(r))>::value)
{
return l = (l + std::forward<Right>(r));
}
template<class Left, detail::enable_if_concrete_string<Left> = 0>
decltype(auto) operator+=(Left& l, const char* r)
noexcept(noexcept(l + r) && std::is_nothrow_assignable<Left, decltype(l + r)>::value)
{
return l = (l + r);
}
}
#include "string_base.tpp"
namespace{
constexpr inline rexy::static_string<char> operator"" _ss(const char* str, size_t len)noexcept{
return rexy::static_string<char>(str, len);
}
constexpr inline rexy::static_string<wchar_t> operator"" _ss(const wchar_t* str, size_t len)noexcept{
return rexy::static_string<wchar_t>(str, len);
}
constexpr inline rexy::static_string<char16_t> operator"" _ss(const char16_t* str, size_t len)noexcept{
return rexy::static_string<char16_t>(str, len);
}
constexpr inline rexy::static_string<char32_t> operator"" _ss(const char32_t* str, size_t len)noexcept{
return rexy::static_string<char32_t>(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