From d7330233fd94f17c0f7e0e1008aa2ff9a72f0409 Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Tue, 20 Feb 2007 20:05:43 +0000 Subject: [PATCH] allow creating wxString from char*, assigning to it from char* and comparing with char* values git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@44543 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/string.h | 55 ++++++++++++++++++++++++++++++++++----- tests/strings/strings.cpp | 20 ++++++++++++++ tests/strings/unicode.cpp | 12 +++++++++ 3 files changed, 80 insertions(+), 7 deletions(-) diff --git a/include/wx/string.h b/include/wx/string.h index fb73ad8757..4c52821e51 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -717,7 +717,9 @@ public: #if wxUSE_UNICODE // from multibyte string - wxString(const char *psz, const wxMBConv& conv, size_t nLength = npos); + wxString(const char *psz, + const wxMBConv& conv = wxConvLibc, + size_t nLength = npos); // from wxWCharBuffer (i.e. return from wxGetString) wxString(const wxWCharBuffer& psz) : wxStringBase(psz.data()) { } #else // ANSI @@ -915,6 +917,9 @@ public: // from wxWCharBuffer wxString& operator=(const wxWCharBuffer& psz) { (void) operator=((const wchar_t *)psz); return *this; } + // from C string + wxString& operator=(const char* psz) + { return operator=(wxString(psz)); } #else // ANSI // from another kind of C string wxString& operator=(const unsigned char* psz); @@ -954,16 +959,24 @@ public: // string += buffer (i.e. from wxGetString) #if wxUSE_UNICODE wxString& operator<<(const wxWCharBuffer& s) - { (void)operator<<((const wchar_t *)s); return *this; } - void operator+=(const wxWCharBuffer& s) - { (void)operator<<((const wchar_t *)s); } + { return operator<<((const wchar_t *)s); } + wxString& operator+=(const wxWCharBuffer& s) + { return operator<<((const wchar_t *)s); } #else // !wxUSE_UNICODE wxString& operator<<(const wxCharBuffer& s) - { (void)operator<<((const char *)s); return *this; } - void operator+=(const wxCharBuffer& s) - { (void)operator<<((const char *)s); } + { return operator<<((const char *)s); } + wxString& operator+=(const wxCharBuffer& s) + { return operator<<((const char *)s); } #endif // wxUSE_UNICODE/!wxUSE_UNICODE +#if wxUSE_UNICODE + // string += C string in Unicode build (with conversion) + wxString& operator<<(const char *s) + { return operator<<(wxString(s)); } + wxString& operator+=(const char *s) + { return operator+=(wxString(s)); } +#endif // wxUSE_UNICODE + // string += C string wxString& Append(const wxString& s) { @@ -1581,6 +1594,34 @@ inline bool operator==(const wxString& s, wxChar c) { return s.IsSameAs(c); } inline bool operator!=(wxChar c, const wxString& s) { return !s.IsSameAs(c); } inline bool operator!=(const wxString& s, wxChar c) { return !s.IsSameAs(c); } +// comparison with C string in Unicode build +#if wxUSE_UNICODE +inline bool operator==(const wxString& s1, const char* s2) + { return s1 == wxString(s2); } +inline bool operator==(const char* s1, const wxString& s2) + { return wxString(s1) == s2; } +inline bool operator!=(const wxString& s1, const char* s2) + { return s1 != wxString(s2); } +inline bool operator!=(const char* s1, const wxString& s2) + { return wxString(s1) != s2; } +inline bool operator< (const wxString& s1, const char* s2) + { return s1 < wxString(s2); } +inline bool operator< (const char* s1, const wxString& s2) + { return wxString(s1) < s2; } +inline bool operator> (const wxString& s1, const char* s2) + { return s1 > wxString(s2); } +inline bool operator> (const char* s1, const wxString& s2) + { return wxString(s1) > s2; } +inline bool operator<=(const wxString& s1, const char* s2) + { return s1 <= wxString(s2); } +inline bool operator<=(const char* s1, const wxString& s2) + { return wxString(s1) <= s2; } +inline bool operator>=(const wxString& s1, const char* s2) + { return s1 >= wxString(s2); } +inline bool operator>=(const char* s1, const wxString& s2) + { return wxString(s1) >= s2; } +#endif // wxUSE_UNICODE + // --------------------------------------------------------------------------- // Implementation only from here until the end of file // --------------------------------------------------------------------------- diff --git a/tests/strings/strings.cpp b/tests/strings/strings.cpp index 9bda408884..03a0a50177 100644 --- a/tests/strings/strings.cpp +++ b/tests/strings/strings.cpp @@ -355,6 +355,26 @@ void StringTestCase::Compare() CPPUNIT_ASSERT( s1 != neq3 ); CPPUNIT_ASSERT( s1 != neq4 ); + CPPUNIT_ASSERT( s1 == wxT("AHH") ); + CPPUNIT_ASSERT( s1 != wxT("no") ); + CPPUNIT_ASSERT( s1 < wxT("AZ") ); + CPPUNIT_ASSERT( s1 <= wxT("AZ") ); + CPPUNIT_ASSERT( s1 <= wxT("AHH") ); + CPPUNIT_ASSERT( s1 > wxT("AA") ); + CPPUNIT_ASSERT( s1 >= wxT("AA") ); + CPPUNIT_ASSERT( s1 >= wxT("AHH") ); + + // test comparison with C strings in Unicode build (must work in ANSI as + // well, of course): + CPPUNIT_ASSERT( s1 == "AHH" ); + CPPUNIT_ASSERT( s1 != "no" ); + CPPUNIT_ASSERT( s1 < "AZ" ); + CPPUNIT_ASSERT( s1 <= "AZ" ); + CPPUNIT_ASSERT( s1 <= "AHH" ); + CPPUNIT_ASSERT( s1 > "AA" ); + CPPUNIT_ASSERT( s1 >= "AA" ); + CPPUNIT_ASSERT( s1 >= "AHH" ); + // wxString _s1 = wxT("A\0HH"); // wxString _eq = wxT("A\0HH"); // wxString _neq1 = wxT("H\0AH"); diff --git a/tests/strings/unicode.cpp b/tests/strings/unicode.cpp index e9888331c2..dc28e5c8c6 100644 --- a/tests/strings/unicode.cpp +++ b/tests/strings/unicode.cpp @@ -141,6 +141,18 @@ void UnicodeTestCase::ConstructorsWithConversion() CPPUNIT_ASSERT ( wxString("\t[pl]open.format.Sformatuj dyskietkê=gfloppy %f", wxConvUTF8) == wxT("") ); //should stop at pos 35 #endif + + + // test using Unicode strings together with char* strings (this must work + // in ANSI mode as well, of course): + wxString s5("ascii"); + CPPUNIT_ASSERT( s5 == "ascii" ); + + s5 += " value"; + + CPPUNIT_ASSERT( strcmp(s5.mb_str(), "ascii value") == 0 ); + CPPUNIT_ASSERT( s5 == "ascii value" ); + CPPUNIT_ASSERT( s5 != "SomethingElse" ); } void UnicodeTestCase::ConversionEmpty() -- 2.45.2