rexylib/include/rexy/string_base.hpp
2020-05-06 15:23:58 -07:00

274 lines
11 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 <cstdlib> //size_t
#include <cstring> //strlen
#include "steal.hpp"
#include "cx/utility.hpp"
#include "traits.hpp"
namespace rexy{
class string_expr{};
//Base of all RAII strings. Its use is allowing passing of rexy strings to functions without knowing the exact type
class string_base : public string_expr
{
protected:
size_t m_length = 0;
size_t m_cap = 0;
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(noexcept(cx::exchange(m_data, nullptr)))
{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* 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 Allocator>
class string_intermediary : public string_base
{
public:
using allocator_type = Allocator;
private:
string_intermediary& _copy_string(const char* s, size_t len)noexcept(noexcept(Allocator::free(m_data)) &&
noexcept(Allocator::copy(s, len)));
public:
constexpr string_intermediary(void)noexcept;
constexpr string_intermediary(rexy::steal<char*> data, size_t len)noexcept;
constexpr string_intermediary(rexy::steal<char*> data, size_t len, size_t cap)noexcept;
constexpr string_intermediary(rexy::steal<char*> data)noexcept;
[[deprecated]] constexpr string_intermediary(char* data, size_t len)noexcept;
[[deprecated]] constexpr string_intermediary(char* data, size_t len, size_t cap)noexcept;
string_intermediary(const char* data, size_t len)noexcept(noexcept(Allocator::copy(data,len)));
string_intermediary(const char* data)noexcept(noexcept(strlen(data)) && noexcept(Allocator::copy(data, m_cap)));
string_intermediary(size_t len)noexcept(noexcept(Allocator::allocate(len)));
string_intermediary(size_t len, size_t cap)noexcept(noexcept(Allocator::allocate(len)));
//normal copy and move ctors
string_intermediary(const string_intermediary& b)noexcept(noexcept(Allocator::copy(b.m_data, b.m_length)));
constexpr string_intermediary(string_intermediary&& s)noexcept(noexcept(cx::exchange(s.m_data, nullptr)));
string_intermediary(const string_base& b)noexcept(noexcept(Allocator::copy(b.get(), b.length())));
//dtor
~string_intermediary(void)noexcept(noexcept(Allocator::free(m_data)));
string_intermediary& operator=(const string_intermediary& s)
noexcept(std::is_nothrow_copy_constructible<string_intermediary<Allocator>>::value &&
std::is_nothrow_move_assignable<string_intermediary<Allocator>>::value);
constexpr string_intermediary& operator=(string_intermediary&& s)noexcept(noexcept(cx::swap(m_data, s.m_data)));
//Copy from c string
string_intermediary& operator=(const char* c)noexcept(noexcept(_copy_string(c, 0)));
//Copy from other string_base
string_intermediary& operator=(const string_base& s)noexcept(noexcept(_copy_string(s.get(), 0)));
//Replace managed pointer. Frees existing value
void reset(char* val = nullptr)noexcept(noexcept(Allocator::free(m_data)));
void reset(char* val, size_t len)noexcept(noexcept(Allocator::free(m_data)));
bool resize(size_t newsize)noexcept(std::is_nothrow_copy_constructible<string_intermediary<Allocator>>::value &&
std::is_nothrow_move_assignable<string_intermediary<Allocator>>::value);
void append(const char* data, size_t len)noexcept(std::is_nothrow_constructible<string_intermediary<Allocator>,decltype(m_length)>::value);
void append(const char* data)noexcept(std::is_nothrow_constructible<string_intermediary<Allocator>,decltype(m_length)>::value);
void append(const string_base& s)noexcept(std::is_nothrow_constructible<string_intermediary<Allocator>,decltype(m_length)>::value);
};
namespace detail{
template<class T>
struct string_appender;
}
//Like an expression template but not really
template<class Left, class Right>
class string_cat_expr : public string_expr
{
private:
Left m_l;
Right m_r;
public:
template<class T, class U>
constexpr string_cat_expr(T&& l, U&& r)noexcept(std::is_nothrow_constructible<Left,T&&>::value &&
std::is_nothrow_constructible<Right,U&&>::value);
constexpr string_cat_expr(string_cat_expr&& s)noexcept(std::is_nothrow_constructible<Left,decltype(s.m_l)>::value &&
std::is_nothrow_constructible<Right,decltype(s.m_r)>::value);
constexpr size_t length(void)const noexcept(noexcept(m_l.length()) && noexcept(m_r.length()));
template<class Alloc>
operator string_intermediary<Alloc>(void)
noexcept(std::is_nothrow_constructible<string_intermediary<Alloc>, size_t>::value &&
std::is_nothrow_invocable<detail::string_appender<string_intermediary<Alloc>>,decltype(*this)>::value);
constexpr const Left& left(void)const noexcept;
constexpr const Right& right(void)const noexcept;
};
template<class Left, class Right>
string_cat_expr(Left&, Right&) -> string_cat_expr<Left&,Right&>;
template<class Left, class Right>
string_cat_expr(Left&&,Right&&) -> string_cat_expr<Left,Right>;
template<class Left, class Right>
string_cat_expr(Left&,Right&&) -> string_cat_expr<Left&,Right>;
template<class Left, class Right>
string_cat_expr(Left&&,Right&) -> string_cat_expr<Left,Right&>;
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;
};
namespace detail{
template<class T>
struct is_string{
static constexpr bool value = rexy::is_type<T,string_expr>::value;
};
template<class T>
struct is_concrete_string{
static constexpr bool value = rexy::is_type<T,string_base>::value;
};
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>;
template<class Targ>
struct string_appender
{
private:
Targ& m_targ;
public:
constexpr string_appender(Targ& t)noexcept;
template<class L, class R>
constexpr void operator()(const string_cat_expr<L,R>& str)
noexcept(noexcept((*this)(str.left())) && noexcept((*this)(str.right())));
template<class Str, std::enable_if_t<!rexy::is_template_type<Str,string_cat_expr>::value,int> = 0>
constexpr void operator()(Str&& str)noexcept(noexcept(m_targ.append(str.get(), str.length())));
};
} //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(left), std::forward<Right>(right))))
{
return string_cat_expr(rexy::static_string(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))))
{
return rexy::string_cat_expr(std::forward<Left>(left), rexy::static_string(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 operator"" _ss(const char* str, size_t len)noexcept{
return rexy::static_string(str, len);
}
}
#ifdef REXY_CX_HASH_HPP
#include "cx/string_hash.hpp"
#endif
#endif