Enable some c++20 constexpr features for strings

This commit is contained in:
rexy712 2022-03-20 18:33:01 -07:00
parent adc52a3659
commit 11ea9db137
4 changed files with 127 additions and 51 deletions

View File

@ -27,7 +27,9 @@ namespace rexy::cx{
#include "../string_base.hpp"
#include "../utility.hpp"
#include <type_traits>
#include <cstddef> //ptrdiff_t
#include <cstddef> //ptrdiff_t, size_t
#include "../detail/cpp20.hpp"
//This is different from rexy::static_string in that this doesn't hold a pointer to a constant string array.
//This holds a mutable array of data which can be modified during compile time. static_string is
@ -98,7 +100,7 @@ namespace rexy::cx{
constexpr string(const string&)noexcept = default;
constexpr string(string&&)noexcept = default;
~string(void)noexcept = default;
REXY_CPP20_CONSTEXPR ~string(void)noexcept = default;
constexpr string& operator=(const_pointer c)noexcept{
m_length = strlen(c);

View File

@ -0,0 +1,30 @@
/**
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 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef REXY_CPP20_HPP
#define REXY_CPP20_HPP
#if __cplusplus > 201703L
#define REXY_CPP20_CONSTEXPR constexpr
#define REXY_CPP20_CONSTEVAL consteval
#else
#define REXY_CPP20_CONSTEXPR
#define REXY_CPP20_CONSTEVAL constexpr
#endif
#endif

View File

@ -34,6 +34,8 @@
#include "detail/hasallocator.hpp"
#include "rexy.hpp"
#include "detail/cpp20.hpp"
namespace rexy{
//Base of all RAII strings. Its use is allowing passing of rexy strings to functions without knowing the exact type
@ -161,8 +163,7 @@ namespace rexy{
{
s.set_islong_flag(false);
}
~string_base(void)noexcept = default;
REXY_CPP20_CONSTEXPR ~string_base(void)noexcept = default;
constexpr string_base& operator=(string_base&& s)noexcept{
std::swap(m_data, s.m_data);
if(this->islong())
@ -254,9 +255,9 @@ namespace rexy{
using allocator_type = Allocator;
private:
void _copy_construct_string(const_pointer data, size_type len, size_type cap)
REXY_CPP20_CONSTEXPR void _copy_construct_string(const_pointer data, size_type len, size_type cap)
noexcept(noexcept(this->allocate(0)));
basic_string& _copy_string(const_pointer s, size_type len)
REXY_CPP20_CONSTEXPR basic_string& _copy_string(const_pointer s, size_type len)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
@ -265,52 +266,57 @@ namespace rexy{
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, size_type len, size_type cap)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)));
REXY_CPP20_CONSTEXPR basic_string(const_pointer data, size_type len)noexcept(noexcept(this->allocate(0)));
REXY_CPP20_CONSTEXPR basic_string(const_pointer data, size_type len, size_type cap)noexcept(noexcept(this->allocate(0)));
REXY_CPP20_CONSTEXPR basic_string(const_pointer data)noexcept(noexcept(this->allocate(0)));
REXY_CPP20_CONSTEXPR explicit basic_string(size_type len)noexcept(noexcept(this->allocate(0)));
REXY_CPP20_CONSTEXPR basic_string(size_type len, size_type cap)noexcept(noexcept(this->allocate(0)));
template<class InputIt>
REXY_CPP20_CONSTEXPR basic_string(InputIt start, InputIt fin)noexcept(noexcept(this->allocate(0)));
//normal copy and move ctors
basic_string(const basic_string& b)noexcept(noexcept(this->allocate(0)));
REXY_CPP20_CONSTEXPR basic_string(const basic_string& b)noexcept(noexcept(this->allocate(0)));
constexpr basic_string(basic_string&& s)noexcept;
basic_string(const string_base<Char>&)noexcept(noexcept(this->allocate(0)));
REXY_CPP20_CONSTEXPR basic_string(const string_base<Char>&)noexcept(noexcept(this->allocate(0)));
//dtor
~basic_string(void)noexcept(noexcept(this->deallocate(nullptr, 0)));
REXY_CPP20_CONSTEXPR ~basic_string(void)noexcept(noexcept(this->deallocate(nullptr, 0)));
basic_string& operator=(const basic_string& s)
REXY_CPP20_CONSTEXPR 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;
basic_string& operator=(const string_base<Char>& s)
REXY_CPP20_CONSTEXPR basic_string& operator=(const string_base<Char>& s)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
//Copy from c string
basic_string& operator=(const_pointer c)
REXY_CPP20_CONSTEXPR basic_string& operator=(const_pointer c)
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)
REXY_CPP20_CONSTEXPR void reset(pointer val = nullptr)noexcept(noexcept(this->deallocate(nullptr,0)));
REXY_CPP20_CONSTEXPR void reset(pointer val, size_type len)noexcept(noexcept(this->deallocate(nullptr,0)));
REXY_CPP20_CONSTEXPR bool resize(size_type newsize)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
void append(const_pointer data, size_type len)
REXY_CPP20_CONSTEXPR void append(const_pointer data, size_type len)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
void append(const_pointer data)
REXY_CPP20_CONSTEXPR void append(const_pointer data)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
template<class InputIt>
REXY_CPP20_CONSTEXPR void append(InputIt start, InputIt fin)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)));
template<class Alloc = allocator_type>
basic_string<value_type,Alloc> substring(size_type start, size_type end)const;
REXY_CPP20_CONSTEXPR basic_string<value_type,Alloc> substring(size_type start, size_type end)const;
pointer release(void)noexcept(noexcept(this->allocate(0)));
REXY_CPP20_CONSTEXPR pointer release(void)noexcept(noexcept(this->allocate(0)));
using detail::hasallocator<Allocator>::allocator;
};
@ -341,7 +347,7 @@ namespace rexy{
constexpr size_type length(void)const noexcept;
template<class Alloc>
operator basic_string<value_type,Alloc>(void)
REXY_CPP20_CONSTEXPR 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);
};
@ -368,7 +374,7 @@ namespace rexy{
constexpr string_view(const_pointer c)noexcept;
constexpr string_view(const string_view& s)noexcept;
constexpr string_view(string_view&& s)noexcept;
~string_view(void)noexcept = default;
REXY_CPP20_CONSTEXPR ~string_view(void)noexcept = default;
constexpr string_view& operator=(const_pointer c)noexcept;
constexpr string_view& operator=(const string_view& s)noexcept;
@ -441,13 +447,13 @@ namespace rexy{
}
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)
REXY_CPP20_CONSTEXPR 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, typename std::decay_t<Left>::const_pointer r)
REXY_CPP20_CONSTEXPR decltype(auto) operator+=(Left& l, typename std::decay_t<Left>::const_pointer r)
noexcept(noexcept(l + r) && std::is_nothrow_assignable<Left, decltype(l + r)>::value)
{
return l = (l + r);

View File

@ -75,7 +75,7 @@ namespace rexy{
//allocate string if longer than small string capacity, copy otherwise
template<class Char, class Allocator>
void basic_string<Char,Allocator>::_copy_construct_string(const_pointer data, size_type len, size_type cap)
REXY_CPP20_CONSTEXPR void basic_string<Char,Allocator>::_copy_construct_string(const_pointer data, size_type len, size_type cap)
noexcept(noexcept(this->allocate(0)))
{
if(cap > this->get_short_capacity()){
@ -109,33 +109,45 @@ namespace rexy{
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, size_type cap)
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(const_pointer data, size_type len, size_type cap)
noexcept(noexcept(this->allocate(0)))
{
_copy_construct_string(data, len, cap);
}
template<class Char, class Allocator>
basic_string<Char,Allocator>::basic_string(const_pointer data, size_type len)
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(const_pointer data, size_type len)
noexcept(noexcept(this->allocate(0))):
basic_string(data, len, len){}
template<class Char, class Allocator>
basic_string<Char,Allocator>::basic_string(const_pointer data)
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(const_pointer data)
noexcept(noexcept(this->allocate(0))):
basic_string(data, data ? strlen(data) : 0){}
template<class Char, class Allocator>
basic_string<Char,Allocator>::basic_string(size_type cap)
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(size_type cap)
noexcept(noexcept(this->allocate(0))):
basic_string(size_type{0}, cap){}
template<class Char, class Allocator>
basic_string<Char,Allocator>::basic_string(size_type len, size_type cap)
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(size_type len, size_type cap)
noexcept(noexcept(this->allocate(0)))
{
_copy_construct_string(nullptr, len, cap);
}
template<class Char, class Allocator>
template<class InputIt>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(InputIt start, InputIt fin)
noexcept(noexcept(this->allocate(0))):
basic_string(nullptr, size_type{fin - start})
{
auto raw = this->get_pointer();
size_type i = 0;
for(auto it = start;it != fin;++it,++i){
raw[i] = *it;
}
raw[i] = 0;
}
//normal copy and move ctors
template<class Char, class Allocator>
basic_string<Char,Allocator>::basic_string(const basic_string& b)
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(const basic_string& b)
noexcept(noexcept(this->allocate(0))):
detail::hasallocator<Allocator>(b)
{
@ -146,7 +158,7 @@ namespace rexy{
detail::hasallocator<Allocator>(std::move(s)),
string_base<Char>(std::move(s)){}
template<class Char, class Allocator>
basic_string<Char,Allocator>::basic_string(const string_base<Char>& b)
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(const string_base<Char>& b)
noexcept(noexcept(this->allocate(0)))
{
_copy_construct_string(b.get(), b.length(), b.capacity());
@ -154,7 +166,7 @@ namespace rexy{
//dtor
template<class Char, class Allocator>
basic_string<Char,Allocator>::~basic_string(void)
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::~basic_string(void)
noexcept(noexcept(this->deallocate(nullptr, 0)))
{
if(this->islong())
@ -162,7 +174,7 @@ namespace rexy{
}
template<class Char, class Allocator>
basic_string<Char,Allocator>& basic_string<Char,Allocator>::operator=(const basic_string& s)
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>& basic_string<Char,Allocator>::operator=(const basic_string& s)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr, 0)))
{
@ -180,7 +192,7 @@ namespace rexy{
return *this;
}
template<class Char, class Allocator>
basic_string<Char,Allocator>& basic_string<Char,Allocator>::operator=(const string_base<Char>& s)
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>& basic_string<Char,Allocator>::operator=(const string_base<Char>& s)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)))
{
@ -188,7 +200,7 @@ namespace rexy{
}
//Copy from c string
template<class Char, class Allocator>
basic_string<Char,Allocator>& basic_string<Char,Allocator>::operator=(const_pointer c)
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>& basic_string<Char,Allocator>::operator=(const_pointer c)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)))
{
@ -197,13 +209,13 @@ namespace rexy{
//Replace managed pointer. Frees existing value
template<class Char, class Allocator>
void basic_string<Char,Allocator>::reset(pointer val)
REXY_CPP20_CONSTEXPR void basic_string<Char,Allocator>::reset(pointer val)
noexcept(noexcept(this->deallocate(nullptr,0)))
{
reset(val, val ? strlen(val) : 0);
}
template<class Char, class Allocator>
void basic_string<Char,Allocator>::reset(pointer val, size_type len)
REXY_CPP20_CONSTEXPR void basic_string<Char,Allocator>::reset(pointer val, size_type len)
noexcept(noexcept(this->deallocate(nullptr,0)))
{
if(this->islong())
@ -214,7 +226,7 @@ namespace rexy{
this->set_long_capacity(len);
}
template<class Char, class Allocator>
bool basic_string<Char,Allocator>::resize(size_type newsize)
REXY_CPP20_CONSTEXPR bool basic_string<Char,Allocator>::resize(size_type newsize)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)))
{
@ -225,7 +237,7 @@ namespace rexy{
return (*this = basic_string(this->get_pointer(), newsize));
}
template<class Char, class Allocator>
void basic_string<Char,Allocator>::append(const_pointer data, size_type len)
REXY_CPP20_CONSTEXPR void basic_string<Char,Allocator>::append(const_pointer data, size_type len)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)))
{
@ -246,17 +258,43 @@ namespace rexy{
}
}
template<class Char, class Allocator>
void basic_string<Char,Allocator>::append(const_pointer data)
REXY_CPP20_CONSTEXPR 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));
}
template<class Char, class Allocator>
template<class InputIt>
REXY_CPP20_CONSTEXPR void basic_string<Char,Allocator>::append(InputIt start, InputIt fin)
noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0)))
{
size_type append_len = fin - start;
size_type my_len = this->length();
size_type my_cap = this->capacity();
pointer raw = this->get_pointer();
if(my_len + append_len > my_cap){
*this = basic_string(raw, my_len, max(my_len+append_len, my_cap*2));
raw = this->get_pointer();
my_cap *= 2;
}
size_type i = 0;
for(auto it = start;it != fin;++it,++i){
raw[my_len + i] = *it;
}
this->set_length(my_len + append_len);
raw[my_len + i] = 0;
}
template<class Char, class Allocator>
template<class Alloc>
auto basic_string<Char,Allocator>::substring(size_type start, size_type end)const -> basic_string<value_type,Alloc>{
REXY_CPP20_CONSTEXPR auto basic_string<Char,Allocator>::substring(size_type start, size_type end)const -> basic_string<value_type,Alloc>{
if(start > end || end > this->length())
return {};
const size_type newlen = end - start;
@ -265,7 +303,7 @@ namespace rexy{
return tmp;
}
template<class Char, class Allocator>
auto basic_string<Char,Allocator>::release(void)noexcept(noexcept(this->allocate(0))) -> pointer{
REXY_CPP20_CONSTEXPR auto basic_string<Char,Allocator>::release(void)noexcept(noexcept(this->allocate(0))) -> pointer{
if(this->islong()){
pointer raw = this->get_long_ptr();
this->set_islong_flag(false);
@ -284,7 +322,7 @@ namespace rexy{
}
template<class Char, class Allocator>
basic_string<Char,Allocator>& basic_string<Char,Allocator>::_copy_string(const_pointer s, size_type len)
REXY_CPP20_CONSTEXPR 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)))
{
@ -307,7 +345,7 @@ namespace rexy{
}
template<class Left, class Right>
template<class Alloc>
string_cat_expr<Left,Right>::operator basic_string<value_type,Alloc>(void)
REXY_CPP20_CONSTEXPR 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)
{