File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 // ////////////////////////////////////////////////////////////////////////////////////////////
Original file line number Diff line number Diff 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+
3647PYSTRING_ADD_TEST (pystring, find)
3748{
3849 PYSTRING_CHECK_EQUAL (pystring::find (" " , " " ), 0 );
You can’t perform that action at this time.
0 commit comments