From d93034427d3762e02541ac9940728e8ebf6d4984 Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Sat, 21 Nov 2026 18:12:59 +0530 Subject: [PATCH] Match Python for rfind and count with start/end bounds rfind searched back from end itself, so it found a match that ran past end, rejected it, and returned -1 without trying earlier matches: rfind("abcabc", "c", 0, 5) gave -1 instead of 2. count with an empty substring returned end - start + 1 even when start was past end, giving negative counts: count("abc", "", 5) gave -1. --- pystring_impl.h | 19 +++++++++++------- test.cpp | 10 ++++++++++ test.dSYM/Contents/Info.plist | 20 +++++++++++++++++++ .../Resources/Relocations/aarch64/test.yml | 5 +++++ 4 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 test.dSYM/Contents/Info.plist create mode 100644 test.dSYM/Contents/Resources/Relocations/aarch64/test.yml diff --git a/pystring_impl.h b/pystring_impl.h index e6d6a48..3ed8f0f 100644 --- a/pystring_impl.h +++ b/pystring_impl.h @@ -869,14 +869,18 @@ const std::string colon = ":"; int rfind( const std::string & str, const std::string & sub, int start, int end ) { PYSTRING_ADJUST_INDICES(start, end, (int) str.size()); - - std::string::size_type result = str.rfind( sub, end ); - - if( result == std::string::npos || - result < (std::string::size_type)start || - (result + sub.size() > (std::string::size_type)end)) + + // The match must fit in [start, end), so search back from the + // last position where it could still end at or before end. + if( end - start < (int) sub.size() ) return -1; - + + std::string::size_type result = str.rfind( sub, (std::string::size_type)(end - (int) sub.size()) ); + + if( result == std::string::npos || + result < (std::string::size_type)start ) + return -1; + return (int)result; } @@ -948,6 +952,7 @@ const std::string colon = ":"; if ( substr.empty() ) { PYSTRING_ADJUST_INDICES(start, end, (int)str.size()); + if ( start > end ) return 0; return end - start + 1; } diff --git a/test.cpp b/test.cpp index 2f562b0..5e5ac2c 100644 --- a/test.cpp +++ b/test.cpp @@ -126,6 +126,12 @@ PYSTRING_ADD_TEST(pystring, rfind) PYSTRING_CHECK_EQUAL(pystring::rfind("abcabcabc", "bc", 4, 20), 7); PYSTRING_CHECK_EQUAL(pystring::rfind("abcabcabc", "abc", 6, 8), -1); + + // the last match that fits before end, not the last match overall + PYSTRING_CHECK_EQUAL(pystring::rfind("abcabcabc", "bc", 0, 8), 4); + PYSTRING_CHECK_EQUAL(pystring::rfind("abcabcabc", "abc", 0, 8), 3); + PYSTRING_CHECK_EQUAL(pystring::rfind("abcabc", "c", 0, 5), 2); + PYSTRING_CHECK_EQUAL(pystring::rfind("abc", "", 2, 1), -1); } PYSTRING_ADD_TEST(pystring, removeprefix) @@ -735,6 +741,10 @@ PYSTRING_ADD_TEST(python3_compat, count_python3) PYSTRING_CHECK_EQUAL(pystring::count("hello world", "", 0), 12); PYSTRING_CHECK_EQUAL(pystring::count("aaa", "a", 0), 3); PYSTRING_CHECK_EQUAL(pystring::count("aaa", "aa", 0), 1); + PYSTRING_CHECK_EQUAL(pystring::count("abc", "", 1, 2), 2); + PYSTRING_CHECK_EQUAL(pystring::count("abc", "", 3, 0), 0); + PYSTRING_CHECK_EQUAL(pystring::count("abc", "", 5), 0); + PYSTRING_CHECK_EQUAL(pystring::count("abc", "", -1, -2), 0); PYSTRING_CHECK_EQUAL(pystring::count("hello", "l", 2, 5), 2); } diff --git a/test.dSYM/Contents/Info.plist b/test.dSYM/Contents/Info.plist new file mode 100644 index 0000000..9a0e661 --- /dev/null +++ b/test.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.test + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/test.dSYM/Contents/Resources/Relocations/aarch64/test.yml b/test.dSYM/Contents/Resources/Relocations/aarch64/test.yml new file mode 100644 index 0000000..471436d --- /dev/null +++ b/test.dSYM/Contents/Resources/Relocations/aarch64/test.yml @@ -0,0 +1,5 @@ +--- +triple: 'arm64-apple-darwin' +binary-path: test +relocations: [] +...