Change utility functions to use more efficient runtime versions when invoked at runtime
This commit is contained in:
parent
fd22069562
commit
feca03e9c9
@ -22,6 +22,9 @@
|
||||
#include <utility> //forward, move
|
||||
#include <cstdlib> //size_t
|
||||
#include <type_traits>
|
||||
#include <cstring> //strlen, strcmp, memcpy
|
||||
#include <cwchar> //wcslen
|
||||
#include <string> //char_traits
|
||||
|
||||
#include "rexy.hpp"
|
||||
|
||||
@ -83,6 +86,42 @@ namespace rexy{
|
||||
{
|
||||
return cmp(l, r) ? l : r;
|
||||
}
|
||||
|
||||
#ifdef __cpp_lib_is_constant_evaluated
|
||||
template<class T>
|
||||
constexpr size_t strlen(const T* c)noexcept{
|
||||
return std::char_traits<T>::length(c);
|
||||
}
|
||||
template<class T>
|
||||
constexpr int strcmp(const T* l, const T* r)noexcept{
|
||||
if(!std::is_constant_evaluated()){
|
||||
if(std::is_same_v<std::remove_cvref_t<T>,char>){
|
||||
return std::strcmp(l, r);
|
||||
}else if(std::is_same_v<std::remove_cvref_t<T>,wchar_t>){
|
||||
return std::wcscmp(l, r);
|
||||
}
|
||||
}
|
||||
for(;*l == *r && *l;++l, ++r);
|
||||
return *l - *r;
|
||||
}
|
||||
template<class T, class Compare>
|
||||
constexpr int strcmp(const T* l, const T* r, Compare cmp)noexcept{
|
||||
for(;cmp(*l, *r) && *l;++l, ++r);
|
||||
return *l - *r;
|
||||
}
|
||||
constexpr void memcpy(void* l, const void* r, size_t n){
|
||||
if(!std::is_constant_evaluated()){
|
||||
std::memcpy(l, r, n);
|
||||
}else{
|
||||
char* ld = static_cast<char*>(l);
|
||||
const char* rd = static_cast<const char*>(r);
|
||||
for(size_t i = 0;i < n;++i){
|
||||
ld[i] = rd[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#else //__cpp_lib_is_constant_evaluated
|
||||
template<class T>
|
||||
constexpr size_t strlen(const T* c)noexcept{
|
||||
size_t i = 0;
|
||||
@ -107,6 +146,7 @@ namespace rexy{
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif //__cpp_lib_is_constant_evaluated
|
||||
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user