From f5d8a4662c77e7b0422159fde81c64340c7185ea Mon Sep 17 00:00:00 2001 From: Roland Kaminski Date: Mon, 3 Oct 2022 19:38:04 +0200 Subject: [PATCH] fix compilation on cygwin Use std::char_traits::find instead of strnlen for better portability. --- src/catch2/catch_tostring.hpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/catch2/catch_tostring.hpp b/src/catch2/catch_tostring.hpp index 235dec3b73..65c6c13eeb 100644 --- a/src/catch2/catch_tostring.hpp +++ b/src/catch2/catch_tostring.hpp @@ -13,7 +13,6 @@ #include #include #include -#include #include #include @@ -41,6 +40,13 @@ namespace Catch { namespace Detail { + inline std::size_t catch_strnlen(const char *str, std::size_t n) { + auto ret = std::char_traits::find(str, n, '\0'); + if (ret != nullptr) { + return ret - str; + } + return n; + } constexpr StringRef unprintableString = "{?}"_sr; @@ -208,28 +214,24 @@ namespace Catch { template struct StringMaker { static std::string convert(char const* str) { - // Note that `strnlen` is not actually part of standard C++, - // but both POSIX and Windows cstdlib provide it. return Detail::convertIntoString( - StringRef( str, strnlen( str, SZ ) ) ); + StringRef( str, Detail::catch_strnlen( str, SZ ) ) ); } }; template struct StringMaker { static std::string convert(signed char const* str) { - // See the plain `char const*` overload auto reinterpreted = reinterpret_cast(str); return Detail::convertIntoString( - StringRef(reinterpreted, strnlen(reinterpreted, SZ))); + StringRef(reinterpreted, Detail::catch_strnlen(reinterpreted, SZ))); } }; template struct StringMaker { static std::string convert(unsigned char const* str) { - // See the plain `char const*` overload auto reinterpreted = reinterpret_cast(str); return Detail::convertIntoString( - StringRef(reinterpreted, strnlen(reinterpreted, SZ))); + StringRef(reinterpreted, Detail::catch_strnlen(reinterpreted, SZ))); } };