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))); } }; }