From 9e055be6593e11849d4fa7966847054372d025b1 Mon Sep 17 00:00:00 2001 From: Rian Quinn Date: Wed, 28 Sep 2016 10:00:47 -0600 Subject: [PATCH] Fix compilation issues with Cygwin Cygwin recently did an update that broken string_span again. The original update had to provide a custom strnlen function because Cygwin doesn't implement this (it's a non-std extension). We never added wchar_t support, and the Cygwin update now breaks on exactly this. This patch provides the missing wchar_t. --- gsl/string_span | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/gsl/string_span b/gsl/string_span index cd49998..768bfd5 100644 --- a/gsl/string_span +++ b/gsl/string_span @@ -118,6 +118,24 @@ namespace details return len; #endif } + + inline std::size_t wstring_length(const wchar_t *str, std::size_t n) + { +#ifdef GSL_PLATFORM_HAS_STRNLEN + return wcsnlen(str, n); +#else + if (str == nullptr || n == 0) + return 0; + + std::size_t len = 0; + span str_span{str, n}; + + while (len < n && str_span[len]) + len++; + + return len; +#endif + } } // @@ -166,14 +184,14 @@ inline span ensure_z(const char* const& sz, std::ptr inline span ensure_z(wchar_t* const& sz, std::ptrdiff_t max) { - auto len = wcsnlen(sz, narrow_cast(max)); + auto len = details::wstring_length(sz, narrow_cast(max)); Ensures(sz[len] == 0); return {sz, static_cast(len)}; } inline span ensure_z(const wchar_t* const& sz, std::ptrdiff_t max) { - auto len = wcsnlen(sz, narrow_cast(max)); + auto len = details::wstring_length(sz, narrow_cast(max)); Ensures(sz[len] == 0); return {sz, static_cast(len)}; } @@ -230,7 +248,7 @@ namespace details { std::ptrdiff_t operator()(wchar_t* const ptr, std::ptrdiff_t length) noexcept { - return narrow_cast(wcsnlen(ptr, narrow_cast(length))); + return narrow_cast(details::wstring_length(ptr, narrow_cast(length))); } }; @@ -248,7 +266,7 @@ namespace details { std::ptrdiff_t operator()(const wchar_t* const ptr, std::ptrdiff_t length) noexcept { - return narrow_cast(wcsnlen(ptr, narrow_cast(length))); + return narrow_cast(details::wstring_length(ptr, narrow_cast(length))); } }; }