]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/string.cpp
Font updates
[wxWidgets.git] / src / common / string.cpp
index e232c8a79d989f50ea7d495451d2d9b186f9b4b9..77af363cb5f986680d60b0397c4cbf821e48c50c 100644 (file)
@@ -48,7 +48,9 @@
 
 #if wxUSE_UNICODE
     #undef wxUSE_EXPERIMENTAL_PRINTF
-    #define wxUSE_EXPERIMENTAL_PRINTF 1
+    #ifndef wvsnprintf
+        #define wxUSE_EXPERIMENTAL_PRINTF 1
+    #endif
 #endif
 
 // allocating extra space for each string consumes more memory but speeds up
@@ -184,46 +186,6 @@ wxSTD ostream& operator<<(wxSTD ostream& os, const wxString& str)
 
 #endif  //std::string compatibility
 
-extern int WXDLLEXPORT wxVsnprintf(wxChar *buf, size_t len,
-                                   const wxChar *format, va_list argptr)
-{
-#if wxUSE_UNICODE
-    // FIXME should use wvsnprintf() or whatever if it's available
-    wxString s;
-    int iLen = s.PrintfV(format, argptr);
-    if ( iLen != -1 )
-    {
-        wxStrncpy(buf, s.c_str(), len);
-        buf[len-1] = wxT('\0');
-    }
-
-    return iLen;
-#else // ANSI
-    // vsnprintf() will not terminate the string with '\0' if there is not
-    // enough place, but we want the string to always be NUL terminated
-    int rc = wxVsnprintfA(buf, len - 1, format, argptr);
-    if ( rc == -1 )
-    {
-        buf[len] = 0;
-    }
-
-    return rc;
-#endif // Unicode/ANSI
-}
-
-extern int WXDLLEXPORT wxSnprintf(wxChar *buf, size_t len,
-                                  const wxChar *format, ...)
-{
-    va_list argptr;
-    va_start(argptr, format);
-
-    int iLen = wxVsnprintf(buf, len, format, argptr);
-
-    va_end(argptr);
-
-    return iLen;
-}
-
 // ----------------------------------------------------------------------------
 // private classes
 // ----------------------------------------------------------------------------
@@ -395,12 +357,12 @@ bool wxString::AllocBuffer(size_t nLen)
   // 2) sizeof(wxStringData) for housekeeping info
   wxStringData* pData = (wxStringData*)
     malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar));
-  
+
   if ( pData == NULL ) {
     // allocation failures are handled by the caller
     return FALSE;
   }
-  
+
   pData->nRefs        = 1;
   pData->nDataLength  = nLen;
   pData->nAllocLength = nLen + EXTRA_ALLOC;
@@ -454,7 +416,7 @@ bool wxString::AllocBeforeWrite(size_t nLen)
 
       pData = (wxStringData*)
           realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
-      
+
       if ( pData == NULL ) {
         // allocation failures are handled by the caller
         // keep previous data since reallocation failed
@@ -489,7 +451,7 @@ bool wxString::Alloc(size_t nLen)
         // allocation failure handled by caller
         return FALSE;
       }
-      
+
       pData->nRefs = 1;
       pData->nDataLength = 0;
       pData->nAllocLength = nLen;
@@ -796,6 +758,55 @@ wxString operator+(const wxChar *psz, const wxString& str)
 // other common string functions
 // ===========================================================================
 
+#if wxUSE_UNICODE
+
+wxString wxString::FromAscii(const char *ascii)
+{
+    if (!ascii)
+       return wxEmptyString;
+
+    size_t len = strlen( ascii );
+    wxString res;
+
+    if ( len )
+    {
+        wxStringBuffer buf(res, len);
+
+        wchar_t *dest = buf;
+
+        for ( ;; )
+        {
+           if ( (*dest++ = (wchar_t)(unsigned char)*ascii++) == L'\0' )
+               break;
+        }
+    }
+
+    return res;
+}
+
+const wxCharBuffer wxString::ToAscii() const
+{
+    // this will allocate enough space for the terminating NUL too
+    wxCharBuffer buffer(length());
+
+    signed char *dest = (signed char *)buffer.data();
+
+    const wchar_t *pwc = c_str();
+    for ( ;; )
+    {
+        *dest++ = *pwc > SCHAR_MAX ? '_' : *pwc;
+
+        // the output string can't have embedded NULs anyhow, so we can safely
+        // stop at first of them even if we do have any
+        if ( !*pwc++ )
+            break;
+    }
+
+    return buffer;
+}
+
+#endif // Unicode
+
 // ---------------------------------------------------------------------------
 // simple sub-string extraction
 // ---------------------------------------------------------------------------
@@ -1047,7 +1058,7 @@ wxString& wxString::MakeUpper()
     wxFAIL_MSG( _T("out of memory in wxString::MakeUpper") );
     return *this;
   }
-    
+
   for ( wxChar *p = m_pchData; *p; p++ )
     *p = (wxChar)wxToupper(*p);
 
@@ -2236,7 +2247,7 @@ size_t wxArrayString::Add(const wxString& str, size_t nInsert)
     Grow(nInsert);
 
     for (size_t i = 0; i < nInsert; i++)
-    {   
+    {
         // the string data must not be deleted!
         str.GetStringData()->Lock();