]> git.saurik.com Git - wxWidgets.git/blobdiff - include/wx/string.h
added wxCStrData conversion to const unsigned char * too
[wxWidgets.git] / include / wx / string.h
index 8485ea1aad703e1b596f4655aca5b11a74c51ad3..79731a8e8dec15e22f569b47ddd5f7b61f636c25 100644 (file)
@@ -51,7 +51,8 @@
     #include <StringMgr.h>
 #endif
 
-#include "wx/wxchar.h"      // for wxChar
+#include "wx/wxchar.h"      // for wxChar, wxStrlen() etc.
+#include "wx/unichar.h"
 #include "wx/strvararg.h"
 #include "wx/buffer.h"      // for wxCharBuffer
 #include "wx/strconv.h"     // for wxConvertXXX() macros and wxMBConv classes
@@ -98,17 +99,21 @@ extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString;
 // strlen() and portable strcasecmp()
 //---------------------------------------------------------------------------
 
-// Use wxXXX() functions from wxchar.h instead! These functions are for
+#if WXWIN_COMPATIBILITY_2_8
+// Use wxXXX() functions from wxcrt.h instead! These functions are for
 // backwards compatibility only.
 
 // checks whether the passed in pointer is NULL and if the string is empty
+wxDEPRECATED( inline bool IsEmpty(const char *p) );
 inline bool IsEmpty(const char *p) { return (!p || !*p); }
 
 // safe version of strlen() (returns 0 if passed NULL pointer)
+wxDEPRECATED( inline size_t Strlen(const char *psz) );
 inline size_t Strlen(const char *psz)
   { return psz ? strlen(psz) : 0; }
 
 // portable strcasecmp/_stricmp
+wxDEPRECATED( inline int Stricmp(const char *psz1, const char *psz2) );
 inline int Stricmp(const char *psz1, const char *psz2)
 {
 #if defined(__VISUALC__) && defined(__WXWINCE__)
@@ -166,12 +171,24 @@ inline int Stricmp(const char *psz1, const char *psz2)
 #endif  // OS/compiler
 }
 
+#endif // WXWIN_COMPATIBILITY_2_8
+
 // ----------------------------------------------------------------------------
 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
 // ----------------------------------------------------------------------------
 
+// FIXME-UTF8: using std::string as wxString base class is currently broken,
+//             so we use the standard wxString with std::string conversion
+//             enabled, this is API-compatible.
+#define wxUSE_STL_BASED_WXSTRING 0
+#if wxUSE_STL
+    #undef wxUSE_STD_STRING
+    #define wxUSE_STD_STRING 1
+#endif
+//#define wxUSE_STL_BASED_WXSTRING  wxUSE_STL
+
 // in both cases we need to define wxStdString
-#if wxUSE_STL || wxUSE_STD_STRING
+#if wxUSE_STL_BASED_WXSTRING || wxUSE_STD_STRING
 
 #include "wx/beforestd.h"
 #include <string>
@@ -189,7 +206,7 @@ inline int Stricmp(const char *psz1, const char *psz2)
 
 #endif // need <string>
 
-#if wxUSE_STL
+#if wxUSE_STL_BASED_WXSTRING
 
     // we don't need an extra ctor from std::string when copy ctor already does
     // the work
@@ -202,10 +219,10 @@ inline int Stricmp(const char *psz1, const char *psz2)
     #endif
 
     typedef wxStdString wxStringBase;
-#else // if !wxUSE_STL
+#else // if !wxUSE_STL_BASED_WXSTRING
 
 #if !defined(HAVE_STD_STRING_COMPARE) && \
-    (!defined(__WX_SETUP_H__) || wxUSE_STL == 0)
+    (!defined(__WX_SETUP_H__) || wxUSE_STL_BASED_WXSTRING == 0)
     #define HAVE_STD_STRING_COMPARE
 #endif
 
@@ -744,7 +761,7 @@ public:
   wxStringBase& operator+=(wchar_t ch) { return append(1, ch); }
 };
 
-#endif // !wxUSE_STL
+#endif // !wxUSE_STL_BASED_WXSTRING
 
 // ----------------------------------------------------------------------------
 // wxCStrData
@@ -776,7 +793,10 @@ public:
     operator const wchar_t*() const { return AsWChar(); }
 #else
     const char* AsChar() const;
+    const unsigned char* AsUnsignedChar() const
+        { return (const unsigned char *) AsChar(); }
     operator const char*() const { return AsChar(); }
+    operator const unsigned char*() const { return AsUnsignedChar(); }
 #endif
 
     wxString AsString() const;
@@ -800,7 +820,7 @@ public:
     wxCStrData operator+(size_t n) const
         { return wxCStrData(m_str, m_offset + n, m_owned); }
 
-    // this operator is need to make expressions like "*c_str()" or
+    // this operator is needed to make expressions like "*c_str()" or
     // "*(c_str() + 2)" work
     wxUniChar operator*() const;
 
@@ -972,6 +992,16 @@ public:
   wxString(const unsigned char* psz, size_t nLength)
       : wxStringBase((const char*)psz, nLength) { }
 
+    // as we provide both ctors with this signature for both char and unsigned
+    // char string, we need to provide one for wxCStrData to resolve ambiguity
+  wxString(const wxCStrData& cstr, size_t nLength)
+      : wxStringBase(cstr.AsChar(), nLength) { }
+
+    // and because wxString is convertible to wxCStrData and const wxChar *
+    // we also need to provide this one
+  wxString(const wxString& str, size_t nLength)
+      : wxStringBase(str, 0, nLength) { }
+
 #if wxUSE_WCHAR_T
     // from wide (Unicode) string
   wxString(const wchar_t *pwz,
@@ -1144,6 +1174,7 @@ public:
     // from another wxString
   wxString& operator=(const wxStringBase& stringSrc)
     { return (wxString&)wxStringBase::operator=(stringSrc); }
+  wxString& operator=(const wxCStrData& cstr);
     // from a character
   wxString& operator=(wxUniChar ch)
     { return (wxString&)wxStringBase::operator=(ch); }
@@ -1155,7 +1186,7 @@ public:
     { return (wxString&)wxStringBase::operator=(wxUniChar(ch)); }
     // from a C string - STL probably will crash on NULL,
     // so we need to compensate in that case
-#if wxUSE_STL
+#if wxUSE_STL_BASED_WXSTRING
   wxString& operator=(const wxChar *psz)
     { if(psz) wxStringBase::operator=(psz); else Clear(); return *this; }
 #else
@@ -1443,7 +1474,7 @@ public:
     // minimize the string's memory
     // only works if the data of this string is not shared
   bool Shrink();
-#if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL
+#if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING
     // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
     //
     // get writable buffer of at least nLen bytes. Unget() *must* be called
@@ -1452,7 +1483,7 @@ public:
     // call this immediately after GetWriteBuf() has been used
   wxDEPRECATED( void UngetWriteBuf() );
   wxDEPRECATED( void UngetWriteBuf(size_t nLen) );
-#endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL
+#endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING
 
   // wxWidgets version 1 compatibility functions
 
@@ -1652,10 +1683,11 @@ public:
     { return (wxString&)wxStringBase::operator+=(ch); }
   wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
   wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
+  wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); }
   wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
 
 private:
-#if !wxUSE_STL
+#if !wxUSE_STL_BASED_WXSTRING
   // helpers for wxStringBuffer and wxStringBufferLength
   wxChar *DoGetWriteBuf(size_t nLen);
   void DoUngetWriteBuf();
@@ -1700,22 +1732,22 @@ inline wxString operator+(wchar_t ch, const wxString& string)
     { return wxUniChar(ch) + string; }
 
 
-#if wxUSE_STL
+#if wxUSE_STL_BASED_WXSTRING
     // return an empty wxString (not very useful with wxUSE_STL == 1)
     inline const wxString wxGetEmptyString() { return wxString(); }
-#else // !wxUSE_STL
+#else // !wxUSE_STL_BASED_WXSTRING
     // return an empty wxString (more efficient than wxString() here)
     inline const wxString& wxGetEmptyString()
     {
         return *(wxString *)&wxEmptyString;
     }
-#endif // wxUSE_STL/!wxUSE_STL
+#endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
 
 // ----------------------------------------------------------------------------
 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
 // ----------------------------------------------------------------------------
 
-#if wxUSE_STL
+#if wxUSE_STL_BASED_WXSTRING
 
 class WXDLLIMPEXP_BASE wxStringBuffer
 {
@@ -1768,7 +1800,7 @@ private:
     DECLARE_NO_COPY_CLASS(wxStringBufferLength)
 };
 
-#else // if !wxUSE_STL
+#else // if !wxUSE_STL_BASED_WXSTRING
 
 class WXDLLIMPEXP_BASE wxStringBuffer
 {
@@ -1816,16 +1848,16 @@ private:
     DECLARE_NO_COPY_CLASS(wxStringBufferLength)
 };
 
-#endif // !wxUSE_STL
+#endif // !wxUSE_STL_BASED_WXSTRING
 
 // ---------------------------------------------------------------------------
 // wxString comparison functions: operator versions are always case sensitive
 // ---------------------------------------------------------------------------
 
-// note that when wxUSE_STL == 1 the comparison operators taking std::string
-// are used and defining them also for wxString would only result in
-// compilation ambiguities when comparing std::string and wxString
-#if !wxUSE_STL
+// note that when wxUSE_STL_BASED_WXSTRING == 1 the comparison operators taking
+// std::string are used and defining them also for wxString would only result
+// in compilation ambiguities when comparing std::string and wxString
+#if !wxUSE_STL_BASED_WXSTRING
 
 inline bool operator==(const wxString& s1, const wxString& s2)
     { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); }
@@ -1896,7 +1928,7 @@ inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
     { return (const char *)buf + string; }
 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
 
-#endif // !wxUSE_STL
+#endif // !wxUSE_STL_BASED_WXSTRING
 
 // comparison with char (those are not defined by std::[w]string and so should
 // be always available)
@@ -2013,4 +2045,33 @@ inline wxUniChar wxCStrData::operator[](size_t n) const
     return m_str->at(m_offset + n);
 }
 
+// ----------------------------------------------------------------------------
+// implementation of wxString inline methods using wxCStrData
+// ----------------------------------------------------------------------------
+
+inline wxString& wxString::operator=(const wxCStrData& cstr)
+{
+    return *this = cstr.AsString();
+}
+
+// ----------------------------------------------------------------------------
+// implementation of wx[W]CharBuffer inline methods using wxCStrData
+// ----------------------------------------------------------------------------
+
+#if wxUSE_UNICODE
+
+inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr)
+    : m_str(wxStrdupW(cstr))
+{
+}
+
+#else // !wxUSE_UNICODE
+
+inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr)
+    : m_str(wxStrdupA(cstr))
+{
+}
+
+#endif // wxUSE_UNICODE/!wxUSE_UNICODE
+
 #endif  // _WX_WXSTRINGH__