Skip to content

Commit c1778f1

Browse files
committed
Merge pull request opencv#5161 from alalek:fix_string
2 parents 3afe883 + cda9ed4 commit c1778f1

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

modules/core/include/opencv2/core/cvstd.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ size_t String::find_first_of(const String& str, size_t pos) const
896896
inline
897897
size_t String::find_first_of(const char* s, size_t pos) const
898898
{
899+
if (len_ == 0) return npos;
899900
if (pos >= len_ || !s[0]) return npos;
900901
const char* lmax = cstr_ + len_;
901902
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
910911
inline
911912
size_t String::find_last_of(const char* s, size_t pos, size_t n) const
912913
{
914+
if (len_ == 0) return npos;
913915
if (pos >= len_) pos = len_ - 1;
914916
for (const char* i = cstr_ + pos; i >= cstr_; --i)
915917
{
@@ -935,6 +937,7 @@ size_t String::find_last_of(const String& str, size_t pos) const
935937
inline
936938
size_t String::find_last_of(const char* s, size_t pos) const
937939
{
940+
if (len_ == 0) return npos;
938941
if (pos >= len_) pos = len_ - 1;
939942
for (const char* i = cstr_ + pos; i >= cstr_; --i)
940943
{

modules/core/test/test_misc.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,12 @@ TEST(Core_OutputArrayAssign, _Matxf_UMatd)
129129

130130
EXPECT_LE(maxAbsDiff(expected, actual), FLT_EPSILON);
131131
}
132+
133+
134+
TEST(Core_String, find_last_of__with__empty_string)
135+
{
136+
cv::String s;
137+
size_t p = s.find_last_of("q", 0);
138+
// npos is not exported: EXPECT_EQ(cv::String::npos, p);
139+
EXPECT_EQ(std::string::npos, p);
140+
}

0 commit comments

Comments
 (0)