int wxString::CmpNoCase(const wxString& s) const
{
-#if defined(__WXMSW__) && !wxUSE_UNICODE_UTF8
- // Prefer to use CompareString() if available as it's more efficient than
- // doing it manually or even using wxStricmp() (see #10375)
- //
- // Also note that not using NORM_STRINGSORT may result in not having a
- // strict weak ordering (e.g. s1 < s2 and s2 < s3 but s3 < s1) and so break
- // algorithms such as std::sort that rely on it. It's also more consistent
- // with the fall back version below.
- switch ( ::CompareString(LOCALE_USER_DEFAULT,
- NORM_IGNORECASE | SORT_STRINGSORT,
- m_impl.c_str(), m_impl.length(),
- s.m_impl.c_str(), s.m_impl.length()) )
+#if !wxUSE_UNICODE_UTF8
+ // We compare NUL-delimited chunks of the strings inside the loop. We will
+ // do as many iterations as there are embedded NULs in the string, i.e.
+ // usually we will run it just once.
+
+ typedef const wxStringImpl::value_type *pchar_type;
+ const pchar_type thisBegin = m_impl.c_str();
+ const pchar_type thatBegin = s.m_impl.c_str();
+
+ const pchar_type thisEnd = thisBegin + m_impl.length();
+ const pchar_type thatEnd = thatBegin + s.m_impl.length();
+
+ pchar_type thisCur = thisBegin;
+ pchar_type thatCur = thatBegin;
+
+ int rc;
+ for ( ;; )
{
- case CSTR_LESS_THAN:
- return -1;
+ // Compare until the next NUL, if the strings differ this is the final
+ // result.
+ rc = wxStricmp(thisCur, thatCur);
+ if ( rc )
+ break;
- case CSTR_EQUAL:
- return 0;
+ const size_t lenChunk = wxStrlen(thisCur);
+ thisCur += lenChunk;
+ thatCur += lenChunk;
- case CSTR_GREATER_THAN:
- return 1;
+ // Skip all the NULs as wxStricmp() doesn't handle them.
+ for ( ; !*thisCur; thisCur++, thatCur++ )
+ {
+ // Check if we exhausted either of the strings.
+ if ( thisCur == thisEnd )
+ {
+ // This one is exhausted, is the other one too?
+ return thatCur == thatEnd ? 0 : -1;
+ }
- default:
- wxFAIL_MSG( "unexpected CompareString() return value" );
- // fall through
+ if ( thatCur == thatEnd )
+ {
+ // Because of the test above we know that this one is not
+ // exhausted yet so it's greater than the other one that is.
+ return 1;
+ }
- case 0:
- wxLogLastError("CompareString");
- // use generic code below
+ if ( *thatCur )
+ {
+ // Anything non-NUL is greater than NUL.
+ return -1;
+ }
+ }
}
-#endif // __WXMSW__ && !wxUSE_UNICODE_UTF8
- // do the comparison manually: notice that we can't use wxStricmp() as it
- // doesn't handle embedded NULs
+ return rc;
+#else // wxUSE_UNICODE_UTF8
+ // CRT functions can't be used for case-insensitive comparison of UTF-8
+ // strings so do it in the naive, simple and inefficient way.
// FIXME-UTF8: use wxUniChar::ToLower/ToUpper once added
const_iterator i1 = begin();
else if ( len1 > len2 )
return 1;
return 0;
+#endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
}
CPPUNIT_ASSERT( s1 != neq2 );
CPPUNIT_ASSERT( s1 != neq3 );
CPPUNIT_ASSERT( s1 != neq4 );
+
+ CPPUNIT_ASSERT( wxString("\n").Cmp(" ") < 0 );
+ CPPUNIT_ASSERT( wxString("'").Cmp("!") > 0 );
+ CPPUNIT_ASSERT( wxString("!").Cmp("z") < 0 );
}
void StringTestCase::CompareNoCase()
CPPUNIT_CNCNEQ_ASSERT( s1, neq );
CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
+
+ CPPUNIT_ASSERT( wxString("\n").CmpNoCase(" ") < 0 );
+ CPPUNIT_ASSERT( wxString("'").CmpNoCase("!") > 0);
+ CPPUNIT_ASSERT( wxString("!").Cmp("Z") < 0 );
}
void StringTestCase::Contains()