Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions pystring_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 10 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}

Expand Down
20 changes: 20 additions & 0 deletions test.dSYM/Contents/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.test</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
5 changes: 5 additions & 0 deletions test.dSYM/Contents/Resources/Relocations/aarch64/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
triple: 'arm64-apple-darwin'
binary-path: test
relocations: []
...