From cda9ed449f134405cc1758e0e1bd253f9afcf968 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Mon, 10 Aug 2015 14:35:42 +0300 Subject: [PATCH] fix some functions for valid processing of empty string content (cstr_==0, len_==0) --- modules/core/include/opencv2/core/cvstd.hpp | 3 +++ modules/core/test/test_misc.cpp | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/modules/core/include/opencv2/core/cvstd.hpp b/modules/core/include/opencv2/core/cvstd.hpp index a229f533e..3977d513f 100644 --- a/modules/core/include/opencv2/core/cvstd.hpp +++ b/modules/core/include/opencv2/core/cvstd.hpp @@ -896,6 +896,7 @@ size_t String::find_first_of(const String& str, size_t pos) const inline size_t String::find_first_of(const char* s, size_t pos) const { + if (len_ == 0) return npos; if (pos >= len_ || !s[0]) return npos; const char* lmax = cstr_ + len_; for (const char* i = cstr_ + pos; i < lmax; ++i) @@ -910,6 +911,7 @@ size_t String::find_first_of(const char* s, size_t pos) const inline size_t String::find_last_of(const char* s, size_t pos, size_t n) const { + if (len_ == 0) return npos; if (pos >= len_) pos = len_ - 1; for (const char* i = cstr_ + pos; i >= cstr_; --i) { @@ -935,6 +937,7 @@ size_t String::find_last_of(const String& str, size_t pos) const inline size_t String::find_last_of(const char* s, size_t pos) const { + if (len_ == 0) return npos; if (pos >= len_) pos = len_ - 1; for (const char* i = cstr_ + pos; i >= cstr_; --i) { diff --git a/modules/core/test/test_misc.cpp b/modules/core/test/test_misc.cpp index cd4ec7c5a..9e69ffb66 100644 --- a/modules/core/test/test_misc.cpp +++ b/modules/core/test/test_misc.cpp @@ -129,3 +129,12 @@ TEST(Core_OutputArrayAssign, _Matxf_UMatd) EXPECT_LE(maxAbsDiff(expected, actual), FLT_EPSILON); } + + +TEST(Core_String, find_last_of__with__empty_string) +{ + cv::String s; + size_t p = s.find_last_of("q", 0); + // npos is not exported: EXPECT_EQ(cv::String::npos, p); + EXPECT_EQ(std::string::npos, p); +}