More general cleanup of conditional C++20 support. Add Allocator concept and apply it to strings.

This commit is contained in:
rexy712 2022-05-24 17:24:55 -07:00
parent 9a55d8594b
commit 2578895b40
12 changed files with 174 additions and 103 deletions

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's general purpose library This file is a part of rexy's general purpose library
Copyright (C) 2020 rexy712 Copyright (C) 2020-2022 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -19,12 +19,22 @@
#ifndef REXY_DEFAULT_ALLOCATOR_HPP #ifndef REXY_DEFAULT_ALLOCATOR_HPP
#define REXY_DEFAULT_ALLOCATOR_HPP #define REXY_DEFAULT_ALLOCATOR_HPP
#include <cstdlib> //size_t #include <cstddef> //ptrdiff_t, size_t
#include <cstddef> //ptrdiff_t #include <type_traits> //true_type, false_type, is_constant_evaluated
#include <type_traits> //true_type, false_type
#include <new> #include <new>
#include <limits> //numeric_limits #include <limits> //numeric_limits
#if __cplusplus >= 202002L
#include <memory> //allocator
#endif
#ifdef __cpp_concepts
#include "concepts/allocator.hpp"
#define REXY_ALLOCATOR_CONCEPT Allocator
#else
#define REXY_ALLOCATOR_CONCEPT class
#endif
#include "rexy.hpp" #include "rexy.hpp"
#include "compat/standard.hpp" #include "compat/standard.hpp"
@ -80,9 +90,9 @@ namespace rexy{
return a.allocate(num_items); return a.allocate(num_items);
}else{ }else{
if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){ if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){
return static_cast<pointer>(::operator new(bytes)); return reinterpret_cast<pointer>(::operator new(bytes));
}else{ }else{
return static_cast<pointer>(::operator new(bytes, static_cast<std::align_val_t>(alignof(T)))); return reinterpret_cast<pointer>(::operator new(bytes, static_cast<std::align_val_t>(alignof(T))));
} }
} //if consteval } //if consteval
} }
@ -123,9 +133,9 @@ namespace rexy{
size_type bytes = has_overflow(n) ? std::numeric_limits<size_type>::max() : n*sizeof(T); size_type bytes = has_overflow(n) ? std::numeric_limits<size_type>::max() : n*sizeof(T);
if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){ if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){
return static_cast<pointer>(::operator new(bytes)); return reinterpret_cast<pointer>(::operator new(bytes));
}else{ }else{
return static_cast<pointer>(::operator new(bytes, static_cast<std::align_val_t>(alignof(T)))); return reinterpret_cast<pointer>(::operator new(bytes, static_cast<std::align_val_t>(alignof(T))));
} }
} }
REXY_CPP20_CONSTEXPR void deallocate(pointer p, size_type n){ REXY_CPP20_CONSTEXPR void deallocate(pointer p, size_type n){

View File

@ -22,7 +22,7 @@
#include <cstdint> //uint_least32_t #include <cstdint> //uint_least32_t
//clang bug: https://bugs.llvm.org/show_bug.cgi?id=48886 //clang bug: https://bugs.llvm.org/show_bug.cgi?id=48886
#if __cplusplus >= 202002L && !defined(__clang__) #if defined(__cpp_consteval) && !defined(__clang__)
#define CONSTEVAL consteval #define CONSTEVAL consteval
#else #else
#define CONSTEVAL constexpr #define CONSTEVAL constexpr

View File

@ -19,13 +19,13 @@
#ifndef REXY_COMPAT_CPP20_HPP #ifndef REXY_COMPAT_CPP20_HPP
#define REXY_COMPAT_CPP20_HPP #define REXY_COMPAT_CPP20_HPP
#if __cplusplus >= 202002L #ifdef __cpp_consteval
#define REXY_CPP20_CONSTEXPR constexpr #define REXY_CPP20_CONSTEXPR constexpr
#define REXY_CPP20_CONSTEVAL consteval #define REXY_CPP20_CONSTEVAL consteval
#else //__cplusplus #else //__cpp_consteval
#define REXY_CPP20_CONSTEXPR #define REXY_CPP20_CONSTEXPR
#define REXY_CPP20_CONSTEVAL constexpr #define REXY_CPP20_CONSTEVAL constexpr
#endif //__cplusplus #endif //__cpp_consteval
#if __cplusplus >= 202300L #if __cplusplus >= 202300L
#define REXY_STANDARD_CPP23 #define REXY_STANDARD_CPP23

View File

@ -0,0 +1,38 @@
/**
This file is a part of rexy's general purpose library
Copyright (C) 2022 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_CONCEPTS_ALLOCATOR_HPP
#define REXY_CONCEPTS_ALLOCATOR_HPP
#include <concepts> //convertible_to
namespace rexy{
template<class T>
concept Allocator = requires(T a, typename T::size_type sz, typename T::pointer pa){
typename T::value_type;
typename T::size_type;
typename T::pointer;
typename T::const_pointer;
{a.allocate(sz)} -> std::convertible_to<typename T::pointer>;
{a.deallocate(pa, sz)};
};
}
#endif

View File

@ -20,10 +20,13 @@
#define REXY_DETAIL_HASALLOCATOR_HPP #define REXY_DETAIL_HASALLOCATOR_HPP
#include "../compat/standard.hpp" #include "../compat/standard.hpp"
#include "../allocator.hpp"
#include <type_traits> //is_empty
namespace rexy::detail{ namespace rexy::detail{
template<class Alloc> template<REXY_ALLOCATOR_CONCEPT Alloc, bool EmptyAlloc = std::is_empty_v<Alloc>>
struct hasallocator struct hasallocator
{ {
Alloc m_alloc; Alloc m_alloc;
@ -42,7 +45,28 @@ namespace rexy::detail{
return m_alloc; return m_alloc;
} }
}; };
#if __has_cpp_attribute(no_unique_address)
template<REXY_ALLOCATOR_CONCEPT Alloc>
struct hasallocator<Alloc,true>
{
[[no_unique_address]]
Alloc m_alloc;
REXY_CPP20_CONSTEXPR auto allocate(typename Alloc::size_type bytes)noexcept(noexcept(m_alloc.allocate(0))){
return m_alloc.allocate(bytes);
}
REXY_CPP20_CONSTEXPR void deallocate(typename Alloc::pointer p, typename Alloc::size_type bytes)noexcept(noexcept(m_alloc.deallocate(nullptr,0))){
m_alloc.deallocate(p, bytes);
}
Alloc& allocator(void){
return m_alloc;
}
const Alloc& allocator(void)const{
return m_alloc;
}
};
#endif
} }
#endif #endif

View File

@ -23,11 +23,12 @@
#include "compat/standard.hpp" #include "compat/standard.hpp"
#if __cplusplus < 202002L #ifndef __cpp_concepts
#error "Cannot use enum_traits without C++20 concept support" #error "Cannot use enum_traits without C++20 concept support"
#else //__cplusplus #else //__cpp_concepts
#include <type_traits> #include <type_traits>
@ -209,6 +210,6 @@ namespace rexy::enum_traits{
} }
} }
#endif //__cplusplus #endif //__cpp_concepts
#endif #endif

View File

@ -44,7 +44,7 @@ namespace rexy{
static constexpr auto size = sizeof(value_type); static constexpr auto size = sizeof(value_type);
private: private:
#if __cplusplus >= 202002L #if __cplusplus >= 202002L && __has_cpp_attribute(no_unique_address) && __cpp_constexpr >= 202002L
union storage_{ union storage_{
[[no_unique_address]] [[no_unique_address]]

View File

@ -21,7 +21,7 @@
#include <utility> //forward, move #include <utility> //forward, move
#if __cplusplus >= 202002L #if __cplusplus >= 202002L && __has_cpp_attribute(no_unique_address) && __cpp_constexpr >= 202002L
#include <memory> //construct_at, destroy_at #include <memory> //construct_at, destroy_at

View File

@ -37,10 +37,6 @@
#include "compat/standard.hpp" #include "compat/standard.hpp"
#if __cplusplus >= 202002L
#include <concepts>
#endif
namespace rexy{ namespace rexy{
template<class Char> template<class Char>
@ -72,7 +68,7 @@ namespace rexy{
size_type capacity:(CHAR_BIT*sizeof(size_type)-1); //take away last bit from capacity for islong size_type capacity:(CHAR_BIT*sizeof(size_type)-1); //take away last bit from capacity for islong
size_type length; //length of string excluding null terminator size_type length; //length of string excluding null terminator
constexpr ldata(void)noexcept: constexpr ldata(void)noexcept:
islong(0), islong(1),
capacity(0), capacity(0),
length(0){} length(0){}
}; };
@ -88,12 +84,12 @@ namespace rexy{
length(0), length(0),
data{}{} data{}{}
}; };
//union of short and long string representations. Default to long representation for wstring_view's use case. //union of short and long string representations.
union combine_data{ union combine_data{
ldata l; ldata l;
sdata s; sdata s;
constexpr combine_data(void)noexcept: constexpr combine_data(void)noexcept:
l(){} s(){}
}m_data; }m_data;
//direct access to current string data regardless of representation. Increases access speed. //direct access to current string data regardless of representation. Increases access speed.
@ -245,8 +241,8 @@ namespace rexy{
//Supplies all functions that string_base can't implement //Supplies all functions that string_base can't implement
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
class basic_string : protected detail::hasallocator<Allocator>, public string_base<Char> class basic_string : protected detail::hasallocator<Alloc>, public string_base<Char>
{ {
public: public:
using value_type = typename string_base<Char>::value_type; using value_type = typename string_base<Char>::value_type;
@ -260,7 +256,7 @@ namespace rexy{
using const_iterator = typename string_base<Char>::const_iterator; using const_iterator = typename string_base<Char>::const_iterator;
using reverse_iterator = typename string_base<Char>::reverse_iterator; using reverse_iterator = typename string_base<Char>::reverse_iterator;
using const_reverse_iterator = typename string_base<Char>::const_reverse_iterator; using const_reverse_iterator = typename string_base<Char>::const_reverse_iterator;
using allocator_type = Allocator; using allocator_type = Alloc;
private: private:
REXY_CPP20_CONSTEXPR 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)
@ -325,11 +321,11 @@ namespace rexy{
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0))); noexcept(this->deallocate(nullptr,0)));
template<class Alloc = allocator_type> template<REXY_ALLOCATOR_CONCEPT A = allocator_type>
REXY_CPP20_CONSTEXPR basic_string<value_type,Alloc> substring(size_type start, size_type end)const; REXY_CPP20_CONSTEXPR basic_string<value_type,A> substring(size_type start, size_type end)const;
REXY_CPP20_CONSTEXPR pointer release(void)noexcept(noexcept(this->allocate(0))); REXY_CPP20_CONSTEXPR pointer release(void)noexcept(noexcept(this->allocate(0)));
using detail::hasallocator<Allocator>::allocator; using detail::hasallocator<Alloc>::allocator;
constexpr basic_string_view<value_type> create_view(void)const noexcept; constexpr basic_string_view<value_type> create_view(void)const noexcept;
constexpr basic_string_view<value_type> create_view(const_iterator start, const_iterator fin)const noexcept; constexpr basic_string_view<value_type> create_view(const_iterator start, const_iterator fin)const noexcept;
@ -361,7 +357,7 @@ namespace rexy{
constexpr string_cat_expr(string_cat_expr&&) = default; constexpr string_cat_expr(string_cat_expr&&) = default;
constexpr size_type length(void)const noexcept; constexpr size_type length(void)const noexcept;
template<class Alloc> template<REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR 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 && 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); std::is_nothrow_invocable<detail::string_appender<basic_string<value_type,Alloc>>,decltype(*this)>::value);

View File

@ -29,7 +29,7 @@
#include "compat/to_address.hpp" #include "compat/to_address.hpp"
#include "string_view.hpp" #include "string_view.hpp"
#if __cplusplus >= 202002L #ifdef __cpp_concepts
#include "compat/cpp20/string_base.hpp" #include "compat/cpp20/string_base.hpp"
#else #else
#include "compat/cpp17/string_base.hpp" #include "compat/cpp17/string_base.hpp"
@ -80,8 +80,8 @@ namespace rexy{
//allocate string if longer than small string capacity, copy otherwise //allocate string if longer than small string capacity, copy otherwise
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR void basic_string<Char,Allocator>::_copy_construct_string(const_pointer data, size_type len, size_type cap) REXY_CPP20_CONSTEXPR void basic_string<Char,Alloc>::_copy_construct_string(const_pointer data, size_type len, size_type cap)
noexcept(noexcept(this->allocate(0))) noexcept(noexcept(this->allocate(0)))
{ {
if(cap > this->get_short_capacity()){ if(cap > this->get_short_capacity()){
@ -103,51 +103,51 @@ namespace rexy{
} }
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
constexpr basic_string<Char,Allocator>::basic_string(void)noexcept{} constexpr basic_string<Char,Alloc>::basic_string(void)noexcept{}
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
constexpr basic_string<Char,Allocator>::basic_string(rexy::steal<pointer> data)noexcept: constexpr basic_string<Char,Alloc>::basic_string(rexy::steal<pointer> data)noexcept:
basic_string(data.value(), data.value() ? strlen(data.value()) : 0){} basic_string(data.value(), data.value() ? strlen(data.value()) : 0){}
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
constexpr basic_string<Char,Allocator>::basic_string(rexy::steal<pointer> data, size_type len)noexcept: constexpr basic_string<Char,Alloc>::basic_string(rexy::steal<pointer> data, size_type len)noexcept:
string_base<Char>(data.value(), len, len){} string_base<Char>(data.value(), len, len){}
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
constexpr basic_string<Char,Allocator>::basic_string(rexy::steal<pointer> data, size_type len, size_type cap)noexcept: constexpr basic_string<Char,Alloc>::basic_string(rexy::steal<pointer> data, size_type len, size_type cap)noexcept:
string_base<Char>(data.value(), len, cap){} string_base<Char>(data.value(), len, cap){}
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(const_pointer data, size_type len, size_type cap) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(const_pointer data, size_type len, size_type cap)
noexcept(noexcept(this->allocate(0))) noexcept(noexcept(this->allocate(0)))
{ {
_copy_construct_string(data, len, cap); _copy_construct_string(data, len, cap);
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(const_pointer data, size_type len) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(const_pointer data, size_type len)
noexcept(noexcept(this->allocate(0))): noexcept(noexcept(this->allocate(0))):
basic_string(data, len, len){} basic_string(data, len, len){}
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(const_pointer data) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(const_pointer data)
noexcept(noexcept(this->allocate(0))): noexcept(noexcept(this->allocate(0))):
basic_string(data, data ? strlen(data) : 0){} basic_string(data, data ? strlen(data) : 0){}
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(size_type cap) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(size_type cap)
noexcept(noexcept(this->allocate(0))): noexcept(noexcept(this->allocate(0))):
basic_string(size_type{0}, cap){} basic_string(size_type{0}, cap){}
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(size_type len, size_type cap) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(size_type len, size_type cap)
noexcept(noexcept(this->allocate(0))) noexcept(noexcept(this->allocate(0)))
{ {
_copy_construct_string(nullptr, len, cap); _copy_construct_string(nullptr, len, cap);
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(const basic_string_view<Char>& sv) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(const basic_string_view<Char>& sv)
noexcept(noexcept(this->allocate(0))) noexcept(noexcept(this->allocate(0)))
{ {
_copy_construct_string(sv.c_str(), sv.length(), sv.length()); _copy_construct_string(sv.c_str(), sv.length(), sv.length());
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
template<class InputIt> template<class InputIt>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(InputIt start, InputIt fin) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(InputIt start, InputIt fin)
noexcept(noexcept(this->allocate(0))): noexcept(noexcept(this->allocate(0))):
basic_string(nullptr, size_type{fin - start}) basic_string(nullptr, size_type{fin - start})
{ {
@ -159,35 +159,36 @@ namespace rexy{
raw[i] = 0; raw[i] = 0;
} }
//normal copy and move ctors //normal copy and move ctors
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(const basic_string& b) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(const basic_string& b)
noexcept(noexcept(this->allocate(0))): noexcept(noexcept(this->allocate(0))):
detail::hasallocator<Allocator>(b) detail::hasallocator<Alloc>(b)
{ {
_copy_construct_string(b.get(), b.length(), b.capacity()); _copy_construct_string(b.get(), b.length(), b.capacity());
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
constexpr basic_string<Char,Allocator>::basic_string(basic_string&& s)noexcept: constexpr basic_string<Char,Alloc>::basic_string(basic_string&& s)noexcept:
detail::hasallocator<Allocator>(std::move(s)), detail::hasallocator<Alloc>(std::move(s)),
string_base<Char>(std::move(s)){} string_base<Char>(std::move(s)){}
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::basic_string(const string_base<Char>& b) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::basic_string(const string_base<Char>& b)
noexcept(noexcept(this->allocate(0))) noexcept(noexcept(this->allocate(0)))
{ {
_copy_construct_string(b.get(), b.length(), b.capacity()); _copy_construct_string(b.get(), b.length(), b.capacity());
} }
//dtor //dtor
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>::~basic_string(void) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>::~basic_string(void)
noexcept(noexcept(this->deallocate(nullptr, 0))) noexcept(noexcept(this->deallocate(nullptr, 0)))
{ {
if(this->islong()) if(this->islong()){
this->deallocate(this->get_pointer(), sizeof(value_type)*(this->get_long_capacity()+1)); this->deallocate(this->get_pointer(), sizeof(value_type)*(this->get_long_capacity()+1));
} }
}
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>& basic_string<Char,Allocator>::operator=(const basic_string& s) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>& basic_string<Char,Alloc>::operator=(const basic_string& s)
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr, 0))) noexcept(this->deallocate(nullptr, 0)))
{ {
@ -199,29 +200,29 @@ namespace rexy{
basic_string tmp(s); basic_string tmp(s);
return (*this = std::move(tmp)); return (*this = std::move(tmp));
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
constexpr basic_string<Char,Allocator>& basic_string<Char,Allocator>::operator=(basic_string&& s)noexcept{ constexpr basic_string<Char,Alloc>& basic_string<Char,Alloc>::operator=(basic_string&& s)noexcept{
string_base<Char>::operator=(std::move(s)); string_base<Char>::operator=(std::move(s));
return *this; return *this;
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>& basic_string<Char,Allocator>::operator=(const string_base<Char>& s) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>& basic_string<Char,Alloc>::operator=(const string_base<Char>& s)
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0))) noexcept(this->deallocate(nullptr,0)))
{ {
return (*this = basic_string(s)); return (*this = basic_string(s));
} }
//Copy from c string //Copy from c string
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>& basic_string<Char,Allocator>::operator=(const basic_string_view<Char>& sv) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>& basic_string<Char,Alloc>::operator=(const basic_string_view<Char>& sv)
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0))) noexcept(this->deallocate(nullptr,0)))
{ {
return _copy_string(sv.c_str(), sv.length()); return _copy_string(sv.c_str(), sv.length());
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>& basic_string<Char,Allocator>::operator=(const_pointer c) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>& basic_string<Char,Alloc>::operator=(const_pointer c)
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0))) noexcept(this->deallocate(nullptr,0)))
{ {
@ -229,14 +230,14 @@ namespace rexy{
} }
//Replace managed pointer. Frees existing value //Replace managed pointer. Frees existing value
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR void basic_string<Char,Allocator>::reset(pointer val) REXY_CPP20_CONSTEXPR void basic_string<Char,Alloc>::reset(pointer val)
noexcept(noexcept(this->deallocate(nullptr,0))) noexcept(noexcept(this->deallocate(nullptr,0)))
{ {
reset(val, val ? strlen(val) : 0); reset(val, val ? strlen(val) : 0);
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR void basic_string<Char,Allocator>::reset(pointer val, size_type len) REXY_CPP20_CONSTEXPR void basic_string<Char,Alloc>::reset(pointer val, size_type len)
noexcept(noexcept(this->deallocate(nullptr,0))) noexcept(noexcept(this->deallocate(nullptr,0)))
{ {
if(this->islong()) if(this->islong())
@ -246,8 +247,8 @@ namespace rexy{
this->set_long_length(len); this->set_long_length(len);
this->set_long_capacity(len); this->set_long_capacity(len);
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR bool basic_string<Char,Allocator>::resize(size_type newsize) REXY_CPP20_CONSTEXPR bool basic_string<Char,Alloc>::resize(size_type newsize)
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0))) noexcept(this->deallocate(nullptr,0)))
{ {
@ -257,8 +258,8 @@ namespace rexy{
return false; return false;
return (*this = basic_string(this->get_pointer(), newsize)); return (*this = basic_string(this->get_pointer(), newsize));
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR void basic_string<Char,Allocator>::append(const_pointer data, size_type len) REXY_CPP20_CONSTEXPR void basic_string<Char,Alloc>::append(const_pointer data, size_type len)
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0))) noexcept(this->deallocate(nullptr,0)))
{ {
@ -278,17 +279,17 @@ namespace rexy{
*this = std::move(tmp); *this = std::move(tmp);
} }
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR void basic_string<Char,Allocator>::append(const_pointer data) REXY_CPP20_CONSTEXPR void basic_string<Char,Alloc>::append(const_pointer data)
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0))) noexcept(this->deallocate(nullptr,0)))
{ {
if(data) if(data)
append(data, strlen(data)); append(data, strlen(data));
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
template<class InputIt> template<class InputIt>
REXY_CPP20_CONSTEXPR void basic_string<Char,Allocator>::append(InputIt start, InputIt fin) REXY_CPP20_CONSTEXPR void basic_string<Char,Alloc>::append(InputIt start, InputIt fin)
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0))) noexcept(this->deallocate(nullptr,0)))
{ {
@ -313,9 +314,9 @@ namespace rexy{
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
template<class Alloc> template<REXY_ALLOCATOR_CONCEPT A>
REXY_CPP20_CONSTEXPR 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,Alloc>::substring(size_type start, size_type end)const -> basic_string<value_type,A>{
if(start > end || end > this->length()) if(start > end || end > this->length())
return {}; return {};
const size_type newlen = end - start; const size_type newlen = end - start;
@ -323,8 +324,8 @@ namespace rexy{
tmp.append(this->get() + start, newlen); tmp.append(this->get() + start, newlen);
return tmp; return tmp;
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR auto basic_string<Char,Allocator>::release(void)noexcept(noexcept(this->allocate(0))) -> pointer{ REXY_CPP20_CONSTEXPR auto basic_string<Char,Alloc>::release(void)noexcept(noexcept(this->allocate(0))) -> pointer{
if(this->islong()){ if(this->islong()){
pointer raw = this->get_long_ptr(); pointer raw = this->get_long_ptr();
this->set_islong_flag(false); this->set_islong_flag(false);
@ -341,18 +342,18 @@ namespace rexy{
this->set_short_length(0); this->set_short_length(0);
return retval; return retval;
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
constexpr auto basic_string<Char,Allocator>::create_view(void)const noexcept -> basic_string_view<value_type>{ constexpr auto basic_string<Char,Alloc>::create_view(void)const noexcept -> basic_string_view<value_type>{
const auto ptr = this->get_pointer(); const auto ptr = this->get_pointer();
return basic_string_view<value_type>(ptr, ptr + this->length()); return basic_string_view<value_type>(ptr, ptr + this->length());
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
constexpr auto basic_string<Char,Allocator>::create_view(const_iterator start, const_iterator fin)const noexcept -> basic_string_view<value_type>{ constexpr auto basic_string<Char,Alloc>::create_view(const_iterator start, const_iterator fin)const noexcept -> basic_string_view<value_type>{
return basic_string_view<value_type>(start, fin); return basic_string_view<value_type>(start, fin);
} }
template<class Char, class Allocator> template<class Char, REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR basic_string<Char,Allocator>& basic_string<Char,Allocator>::_copy_string(const_pointer s, size_type len) REXY_CPP20_CONSTEXPR basic_string<Char,Alloc>& basic_string<Char,Alloc>::_copy_string(const_pointer s, size_type len)
noexcept(noexcept(this->allocate(0)) && noexcept(noexcept(this->allocate(0)) &&
noexcept(this->deallocate(nullptr,0))) noexcept(this->deallocate(nullptr,0)))
{ {
@ -374,7 +375,7 @@ namespace rexy{
return this->m_l.length() + this->m_r.length(); return this->m_l.length() + this->m_r.length();
} }
template<class Left, class Right> template<class Left, class Right>
template<class Alloc> template<REXY_ALLOCATOR_CONCEPT Alloc>
REXY_CPP20_CONSTEXPR 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 && 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) std::is_nothrow_invocable<detail::string_appender<basic_string<value_type,Alloc>>,decltype(*this)>::value)

View File

@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0.2)
project(rexylib_tests) project(rexylib_tests)
set(INCLUDE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../include) set(INCLUDE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../include)
include_directories("${INCLUDE_PATH}") include_directories("${INCLUDE_PATH}")
add_compile_options(-Wall -Wextra -pedantic -std=c++17) add_compile_options(-Wall -Wextra -pedantic -std=c++20 -Wno-free-nonheap-object)
link_libraries(rexy) link_libraries(rexy)
if(ENABLE_PROFILING) if(ENABLE_PROFILING)

View File

@ -5,6 +5,7 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <utility> #include <utility>
#include <new>
[[noreturn]] void error(const char* str){ [[noreturn]] void error(const char* str){
fprintf(stderr, "%s", str); fprintf(stderr, "%s", str);