From e25a44195ca7a6192d2f78b0629ad1bf29fc7cfa Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Thu, 3 Sep 2026 12:36:00 +0200 Subject: [PATCH] Fix size type of string_view (#1586) Fixes #1577 Rename variables to align to snake_case convention --- include/etl/string_view.h | 66 +++++++++++++++++++-------------------- test/test_string_view.cpp | 18 +++++++++++ 2 files changed, 51 insertions(+), 33 deletions(-) diff --git a/include/etl/string_view.h b/include/etl/string_view.h index 81446bcf..2cb20e37 100644 --- a/include/etl/string_view.h +++ b/include/etl/string_view.h @@ -128,10 +128,7 @@ namespace etl typedef const T* const_iterator; typedef ETL_OR_STD::reverse_iterator const_reverse_iterator; - enum - { - npos = etl::integral_limits::max - }; + static ETL_CONSTANT size_type npos = etl::integral_limits::max; //************************************************************************* /// Default constructor. @@ -172,7 +169,7 @@ namespace etl //************************************************************************* /// Construct from pointer/size. //************************************************************************* - ETL_CONSTEXPR basic_string_view(const T* begin_, size_t size_) ETL_NOEXCEPT + ETL_CONSTEXPR basic_string_view(const T* begin_, size_type size_) ETL_NOEXCEPT : mbegin(begin_) , mend(begin_ + size_) { @@ -304,15 +301,15 @@ namespace etl //************************************************************************* /// Returns the size of the array. //************************************************************************* - ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT + ETL_CONSTEXPR size_type size() const ETL_NOEXCEPT { - return static_cast(mend - mbegin); + return static_cast(mend - mbegin); } //************************************************************************* /// Returns the size of the array. //************************************************************************* - ETL_CONSTEXPR size_t length() const ETL_NOEXCEPT + ETL_CONSTEXPR size_type length() const ETL_NOEXCEPT { return size(); } @@ -320,7 +317,7 @@ namespace etl //************************************************************************* /// Returns the maximum possible size of the array. //************************************************************************* - ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT + ETL_CONSTEXPR size_type max_size() const ETL_NOEXCEPT { return size(); } @@ -347,7 +344,7 @@ namespace etl //************************************************************************* /// Assign from iterator and size. //************************************************************************* - ETL_CONSTEXPR14 void assign(const_pointer begin_, size_t size_) ETL_NOEXCEPT + ETL_CONSTEXPR14 void assign(const_pointer begin_, size_type size_) ETL_NOEXCEPT { mbegin = begin_; mend = begin_ + size_; @@ -358,7 +355,7 @@ namespace etl /// If asserts or exceptions are enabled, throws an etl::string_view_bounds /// if the index is out of bounds. //************************************************************************* - ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_INDEX_OPERATOR) + ETL_CONSTEXPR const_reference operator[](size_type i) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_INDEX_OPERATOR) { #if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_INDEX_OPERATOR return i < size() ? mbegin[i] : throw(ETL_ERROR(string_view_bounds)); @@ -371,7 +368,7 @@ namespace etl //************************************************************************* /// Returns a const reference to the indexed value. //************************************************************************* - const_reference at(size_t i) const + const_reference at(size_type i) const { ETL_ASSERT((mbegin != ETL_NULLPTR && mend != ETL_NULLPTR), ETL_ERROR(string_view_uninitialised)); ETL_ASSERT(i < size(), ETL_ERROR(string_view_bounds)); @@ -394,7 +391,7 @@ namespace etl //************************************************************************* ETL_CONSTEXPR14 size_type copy(T* destination, size_type count, size_type position = 0) const ETL_NOEXCEPT { - size_t n = 0UL; + size_type n = 0UL; if (position < size()) { @@ -415,7 +412,7 @@ namespace etl if (position < size()) { - size_t n = etl::min(count, size() - position); + size_type n = etl::min(count, size() - position); view = basic_string_view(mbegin + position, mbegin + position + n); } @@ -526,9 +523,9 @@ namespace etl ETL_CONSTEXPR14 bool starts_with(const T* text) const ETL_NOEXCEPT { - size_t lengthtext = TTraits::length(text); + size_type length_text = TTraits::length(text); - return (size() >= lengthtext) && (compare(0, lengthtext, text) == 0); + return (size() >= length_text) && (compare(0, length_text, text) == 0); } //************************************************************************* @@ -546,10 +543,10 @@ namespace etl ETL_CONSTEXPR14 bool ends_with(const T* text) const { - size_t lengthtext = TTraits::length(text); - size_t lengthview = size(); + size_type length_text = TTraits::length(text); + size_type length_view = size(); - return (lengthview >= lengthtext) && (compare(lengthview - lengthtext, lengthtext, text) == 0); + return (length_view >= length_text) && (compare(length_view - length_text, length_text, text) == 0); } //************************************************************************* @@ -633,15 +630,15 @@ namespace etl //************************************************************************* ETL_CONSTEXPR14 size_type find_first_of(etl::basic_string_view view, size_type position = 0) const ETL_NOEXCEPT { - const size_t lengthtext = size(); + const size_type length_text = size(); - if (position < lengthtext) + if (position < length_text) { - for (size_t i = position; i < lengthtext; ++i) + for (size_type i = position; i < length_text; ++i) { - const size_t lengthview = view.size(); + const size_type length_view = view.size(); - for (size_t j = 0UL; j < lengthview; ++j) + for (size_type j = 0UL; j < length_view; ++j) { if (mbegin[i] == view[j]) { @@ -685,9 +682,9 @@ namespace etl while (it != rend()) { - const size_t viewlength = view.size(); + const size_type view_length = view.size(); - for (size_t j = 0UL; j < viewlength; ++j) + for (size_type j = 0UL; j < view_length; ++j) { if (mbegin[position] == view[j]) { @@ -722,17 +719,17 @@ namespace etl //************************************************************************* ETL_CONSTEXPR14 size_type find_first_not_of(etl::basic_string_view view, size_type position = 0) const ETL_NOEXCEPT { - const size_t lengthtext = size(); + const size_type length_text = size(); - if (position < lengthtext) + if (position < length_text) { - for (size_t i = position; i < lengthtext; ++i) + for (size_type i = position; i < length_text; ++i) { bool found = false; - const size_t viewlength = view.size(); + const size_type view_length = view.size(); - for (size_t j = 0UL; j < viewlength; ++j) + for (size_type j = 0UL; j < view_length; ++j) { if (mbegin[i] == view[j]) { @@ -784,9 +781,9 @@ namespace etl { bool found = false; - const size_t viewlength = view.size(); + const size_type view_length = view.size(); - for (size_t j = 0UL; j < viewlength; ++j) + for (size_type j = 0UL; j < view_length; ++j) { if (mbegin[position] == view[j]) { @@ -900,6 +897,9 @@ namespace etl const_pointer mend; }; + template + ETL_CONSTANT typename basic_string_view::size_type basic_string_view::npos; + typedef etl::basic_string_view string_view; typedef etl::basic_string_view wstring_view; typedef etl::basic_string_view u8string_view; diff --git a/test/test_string_view.cpp b/test/test_string_view.cpp index 1d016160..ad234834 100644 --- a/test/test_string_view.cpp +++ b/test/test_string_view.cpp @@ -1262,5 +1262,23 @@ namespace CHECK_EQUAL(std_pos, etl_pos); } } + + //************************************************************************* + TEST(test_npos_type_and_value) + { + typedef etl::string_view::size_type size_type; + + // npos must have the same type as size_type, not an anonymous enum. + CHECK((etl::is_same::value)); + CHECK_EQUAL(etl::integral_limits::max, etl::string_view::npos); + + // npos must compare equal to the equivalent std::string_view constant. + CHECK_EQUAL(std::string::npos, etl::string_view::npos); + + // substr with the default count must return the whole remaining view. + etl::string_view view("Hello World"); + CHECK_EQUAL(std::string("World"), std::string(view.substr(6).data(), view.substr(6).size())); + CHECK_EQUAL(5U, view.substr(6, etl::string_view::npos).size()); + } } } // namespace