diff --git a/gsl/string_span b/gsl/string_span index 768bfd5..fb943dc 100644 --- a/gsl/string_span +++ b/gsl/string_span @@ -56,10 +56,6 @@ #endif // _MSC_VER <= 1800 #endif // _MSC_VER -#ifndef __CYGWIN__ -#define GSL_PLATFORM_HAS_STRNLEN -#endif - // In order to test the library, we need it to throw exceptions that we can catch #ifdef GSL_THROW_ON_CONTRACT_VIOLATION @@ -101,40 +97,32 @@ using wzstring = basic_zstring; namespace details { - inline std::size_t string_length(const char *str, std::size_t n) + inline std::ptrdiff_t string_length(const char *str, std::ptrdiff_t n) { -#ifdef GSL_PLATFORM_HAS_STRNLEN - return strnlen(str, n); -#else - if (str == nullptr || n == 0) + if (str == nullptr || n <= 0) return 0; - std::size_t len = 0; span str_span{str, n}; + std::ptrdiff_t len = 0; while (len < n && str_span[len]) len++; return len; -#endif } - inline std::size_t wstring_length(const wchar_t *str, std::size_t n) + inline std::ptrdiff_t wstring_length(const wchar_t *str, std::ptrdiff_t n) { -#ifdef GSL_PLATFORM_HAS_STRNLEN - return wcsnlen(str, n); -#else - if (str == nullptr || n == 0) + if (str == nullptr || n <= 0) return 0; - std::size_t len = 0; span str_span{str, n}; + std::ptrdiff_t len = 0; while (len < n && str_span[len]) len++; return len; -#endif } } @@ -170,30 +158,30 @@ inline span ensure_z(T* const& sz, std::ptrdiff_t max = PTRDI // overloads to share an implementation inline span ensure_z(char* const& sz, std::ptrdiff_t max) { - auto len = details::string_length(sz, narrow_cast(max)); + auto len = details::string_length(sz, max); Ensures(sz[len] == 0); - return {sz, static_cast(len)}; + return {sz, len}; } inline span ensure_z(const char* const& sz, std::ptrdiff_t max) { - auto len = details::string_length(sz, narrow_cast(max)); + auto len = details::string_length(sz, max); Ensures(sz[len] == 0); - return {sz, static_cast(len)}; + return {sz, len}; } inline span ensure_z(wchar_t* const& sz, std::ptrdiff_t max) { - auto len = details::wstring_length(sz, narrow_cast(max)); + auto len = details::wstring_length(sz, max); Ensures(sz[len] == 0); - return {sz, static_cast(len)}; + return {sz, len}; } inline span ensure_z(const wchar_t* const& sz, std::ptrdiff_t max) { - auto len = details::wstring_length(sz, narrow_cast(max)); + auto len = details::wstring_length(sz, max); Ensures(sz[len] == 0); - return {sz, static_cast(len)}; + return {sz, len}; } template @@ -239,7 +227,7 @@ namespace details { std::ptrdiff_t operator()(char* const ptr, std::ptrdiff_t length) noexcept { - return narrow_cast(details::string_length(ptr, narrow_cast(length))); + return details::string_length(ptr, length); } }; @@ -248,7 +236,7 @@ namespace details { std::ptrdiff_t operator()(wchar_t* const ptr, std::ptrdiff_t length) noexcept { - return narrow_cast(details::wstring_length(ptr, narrow_cast(length))); + return details::wstring_length(ptr, length); } }; @@ -257,7 +245,7 @@ namespace details { std::ptrdiff_t operator()(const char* const ptr, std::ptrdiff_t length) noexcept { - return narrow_cast(details::string_length(ptr, narrow_cast(length))); + return details::string_length(ptr, length); } }; @@ -266,7 +254,7 @@ namespace details { std::ptrdiff_t operator()(const wchar_t* const ptr, std::ptrdiff_t length) noexcept { - return narrow_cast(details::wstring_length(ptr, narrow_cast(length))); + return details::wstring_length(ptr, length); } }; }