From feca03e9c9d11a1a735b42d608ab43f402835945 Mon Sep 17 00:00:00 2001 From: rexy712 Date: Sat, 11 Jun 2022 08:47:47 -0700 Subject: [PATCH] Change utility functions to use more efficient runtime versions when invoked at runtime --- include/rexy/utility.hpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/include/rexy/utility.hpp b/include/rexy/utility.hpp index a9cbb77..67fdca7 100644 --- a/include/rexy/utility.hpp +++ b/include/rexy/utility.hpp @@ -22,6 +22,9 @@ #include //forward, move #include //size_t #include +#include //strlen, strcmp, memcpy +#include //wcslen +#include //char_traits #include "rexy.hpp" @@ -83,6 +86,42 @@ namespace rexy{ { return cmp(l, r) ? l : r; } + +#ifdef __cpp_lib_is_constant_evaluated + template + constexpr size_t strlen(const T* c)noexcept{ + return std::char_traits::length(c); + } + template + constexpr int strcmp(const T* l, const T* r)noexcept{ + if(!std::is_constant_evaluated()){ + if(std::is_same_v,char>){ + return std::strcmp(l, r); + }else if(std::is_same_v,wchar_t>){ + return std::wcscmp(l, r); + } + } + for(;*l == *r && *l;++l, ++r); + return *l - *r; + } + template + 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(l); + const char* rd = static_cast(r); + for(size_t i = 0;i < n;++i){ + ld[i] = rd[i]; + } + } + } + } +#else //__cpp_lib_is_constant_evaluated template constexpr size_t strlen(const T* c)noexcept{ size_t i = 0; @@ -107,6 +146,7 @@ namespace rexy{ } } } +#endif //__cpp_lib_is_constant_evaluated }