From a52858520f4dc25135a60d0b7dd52cdf50c50808 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 8 Sep 2016 09:19:01 -0700 Subject: [PATCH] Fixed strnlen. --- include/bx/string.h | 3 +-- tests/string_test.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/bx/string.h b/include/bx/string.h index df837a3..ea21ff3 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -43,9 +43,8 @@ namespace bx /// inline size_t strnlen(const char* _str, size_t _max) { - const char* end = _str + _max; const char* ptr; - for (ptr = _str; ptr < end && *ptr != '\0'; ++ptr) {}; + for (ptr = _str; 0 < _max && *ptr != '\0'; ++ptr, --_max) {}; return ptr - _str; } diff --git a/tests/string_test.cpp b/tests/string_test.cpp index 390a75b..3d98e38 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -10,6 +10,15 @@ bx::AllocatorI* g_allocator; +TEST_CASE("strnlen", "") +{ + const char* test = "test"; + + REQUIRE(0 == bx::strnlen(test, 0) ); + REQUIRE(2 == bx::strnlen(test, 2) ); + REQUIRE(4 == bx::strnlen(test, UINT32_MAX) ); +} + TEST_CASE("StringView", "") { bx::StringView sv("test");