Skip to content

Commit b7cde2b

Browse files
committed
tightening compatbility with python's behavior in edge cases
1 parent f64d7e3 commit b7cde2b

2 files changed

Lines changed: 626 additions & 11 deletions

File tree

pystring_impl.h

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -502,14 +502,22 @@ const std::string colon = ":";
502502
{
503503
std::string::size_type len = str.size(), i;
504504
if ( len == 0 ) return false;
505-
if( len == 1 ) return ::islower( str[0] );
506505

506+
// python's islower is a lot more leniant than c++
507+
// this will match the python behavior so that something like
508+
// islower("hello123") is true
509+
510+
bool has_cased = false;
507511
for ( i = 0; i < len; ++i )
508512
{
509-
if ( !::islower( str[i] ) ) return false;
513+
if ( ::islower( str[i] ) )
514+
has_cased = true;
515+
else if ( ::isupper( str[i] ) )
516+
return false;
510517
}
511-
return true;
512-
}
518+
return has_cased;
519+
}
520+
513521

514522
//////////////////////////////////////////////////////////////////////////////////////////////
515523
///
@@ -574,17 +582,22 @@ const std::string colon = ":";
574582
//////////////////////////////////////////////////////////////////////////////////////////////
575583
///
576584
///
577-
bool isupper( const std::string & str )
585+
bool isupper(const std::string &str)
578586
{
579-
std::string::size_type len = str.size(), i;
580-
if ( len == 0 ) return false;
581-
if( len == 1 ) return ::isupper( str[0] );
582587

583-
for ( i = 0; i < len; ++i )
588+
// python's isupper is a lot more leniant than c++
589+
// this will match the python behavior so that something like
590+
// isupper("HELLO123") is true
591+
592+
bool has_cased = false;
593+
for (std::string::size_type i = 0; i < str.size(); ++i)
584594
{
585-
if ( !::isupper( str[i] ) ) return false;
595+
if (::isupper(str[i]))
596+
has_cased = true;
597+
else if (::islower(str[i]))
598+
return false;
586599
}
587-
return true;
600+
return has_cased;
588601
}
589602

590603
//////////////////////////////////////////////////////////////////////////////////////////////
@@ -927,6 +940,16 @@ const std::string colon = ":";
927940
int nummatches = 0;
928941
int cursor = start;
929942

943+
// special handling for an empty substring
944+
// this will match python's behavior of
945+
// "bob".count("") == 4
946+
// "".count("") == 1
947+
if ( substr.empty() )
948+
{
949+
PYSTRING_ADJUST_INDICES(start, end, (int)str.size());
950+
return end - start + 1;
951+
}
952+
930953
while ( 1 )
931954
{
932955
cursor = find( str, substr, cursor, end );

0 commit comments

Comments
 (0)