Skip to content

Commit 92b04c9

Browse files
committed
matching our isupper behavior to pythons
1 parent 64a428f commit 92b04c9

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

pystring_impl.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -574,17 +574,19 @@ const std::string colon = ":";
574574
//////////////////////////////////////////////////////////////////////////////////////////////
575575
///
576576
///
577-
bool isupper( const std::string & str )
577+
bool isupper(const std::string &str)
578578
{
579-
std::string::size_type len = str.size(), i;
580-
if ( len == 0 ) return false;
581-
if( len == 1 ) return ::isupper( str[0] );
582-
583-
for ( i = 0; i < len; ++i )
579+
// this is written this way to match python's behavior of isupper
580+
// in python something like "HELLO!" returns true (the "!" is basically ignored)
581+
bool has_cased = false;
582+
for (std::string::size_type i = 0; i < str.size(); ++i)
584583
{
585-
if ( !::isupper( str[i] ) ) return false;
584+
if (::isupper(str[i]))
585+
has_cased = true;
586+
else if (::islower(str[i]))
587+
return false;
586588
}
587-
return true;
589+
return has_cased;
588590
}
589591

590592
//////////////////////////////////////////////////////////////////////////////////////////////

test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ PYSTRING_ADD_TEST(pystring, endswith)
3333
PYSTRING_CHECK_EQUAL(pystring::endswith("abcdef", "cdef", -10), true);
3434
}
3535

36+
PYSTRING_ADD_TEST(pystring, isupper)
37+
{
38+
PYSTRING_CHECK_EQUAL(pystring::isupper("ABC"), true);
39+
PYSTRING_CHECK_EQUAL(pystring::isupper("abc"), false);
40+
PYSTRING_CHECK_EQUAL(pystring::isupper("ABc"), false);
41+
PYSTRING_CHECK_EQUAL(pystring::isupper("AB-C"), true);
42+
PYSTRING_CHECK_EQUAL(pystring::isupper("HELLO 123"), true);
43+
PYSTRING_CHECK_EQUAL(pystring::isupper("123"), false);
44+
PYSTRING_CHECK_EQUAL(pystring::isupper("HELLO!"), true);
45+
}
46+
3647
PYSTRING_ADD_TEST(pystring, find)
3748
{
3849
PYSTRING_CHECK_EQUAL(pystring::find("", ""), 0);

0 commit comments

Comments
 (0)