rewrote wxString::To/FromAscii() to fix a few small problems

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16626 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2002-08-20 15:09:27 +00:00
parent e7d41190e0
commit e015c2a31f
2 changed files with 57 additions and 44 deletions

View File

@@ -446,24 +446,27 @@ public:
// implicit conversion to C string // implicit conversion to C string
operator const wxChar*() const { return m_pchData; } operator const wxChar*() const { return m_pchData; }
// explicit conversion to C string (use this with printf()!) // explicit conversion to C string (use this with printf()!)
const wxChar* c_str() const { return m_pchData; } const wxChar* c_str() const { return m_pchData; }
// identical to c_str() // identical to c_str(), for wxWin 1.6x compatibility
const wxChar* wx_str() const { return m_pchData; } const wxChar* wx_str() const { return m_pchData; }
// identical to c_str() // identical to c_str(), for MFC compatibility
const wxChar* GetData() const { return m_pchData; } const wxChar* GetData() const { return m_pchData; }
// conversion to plain ascii: this is usefull for // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
// converting numbers or strings which are certain // converting numbers or strings which are certain not to contain special
// not to contain special chars (typically system // chars (typically system functions, X atoms, environment variables etc.)
// functions, X atoms, environment variables etc.) //
// the behaviour of these functions with the strings containing anything
// else than 7 bit ASCII characters is undefined, use at your own risk.
#if wxUSE_UNICODE #if wxUSE_UNICODE
static wxString FromAscii( char *ascii ); static wxString FromAscii(const char *ascii);
const wxCharBuffer ToAscii() const; const wxCharBuffer ToAscii() const;
#else #else // ANSI
static wxString FromAscii( char *ascii ) { return wxString( ascii ); } static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
const char *ToAscii() const { return m_pchData; } const char *ToAscii() const { return c_str(); }
#endif #endif // Unicode/!Unicode
// conversions with (possible) format convertions: have to return a // conversions with (possible) format convertions: have to return a
// buffer with temporary data // buffer with temporary data
@@ -1147,20 +1150,20 @@ public:
class WXDLLEXPORT wxStringBuffer class WXDLLEXPORT wxStringBuffer
{ {
DECLARE_NO_COPY_CLASS(wxStringBuffer)
public: public:
wxStringBuffer(wxString& str, size_t lenWanted = 1024) wxStringBuffer(wxString& str, size_t lenWanted = 1024)
: m_str(str), m_buf(NULL) : m_str(str), m_buf(NULL)
{ m_buf = m_str.GetWriteBuf(lenWanted); } { m_buf = m_str.GetWriteBuf(lenWanted); }
~wxStringBuffer() { m_str.UngetWriteBuf(); } ~wxStringBuffer() { m_str.UngetWriteBuf(); }
operator wxChar*() const { return m_buf; } operator wxChar*() const { return m_buf; }
private: private:
wxString& m_str; wxString& m_str;
wxChar *m_buf; wxChar *m_buf;
DECLARE_NO_COPY_CLASS(wxStringBuffer)
}; };
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------

View File

@@ -357,12 +357,12 @@ bool wxString::AllocBuffer(size_t nLen)
// 2) sizeof(wxStringData) for housekeeping info // 2) sizeof(wxStringData) for housekeeping info
wxStringData* pData = (wxStringData*) wxStringData* pData = (wxStringData*)
malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar)); malloc(sizeof(wxStringData) + (nLen + EXTRA_ALLOC + 1)*sizeof(wxChar));
if ( pData == NULL ) { if ( pData == NULL ) {
// allocation failures are handled by the caller // allocation failures are handled by the caller
return FALSE; return FALSE;
} }
pData->nRefs = 1; pData->nRefs = 1;
pData->nDataLength = nLen; pData->nDataLength = nLen;
pData->nAllocLength = nLen + EXTRA_ALLOC; pData->nAllocLength = nLen + EXTRA_ALLOC;
@@ -416,7 +416,7 @@ bool wxString::AllocBeforeWrite(size_t nLen)
pData = (wxStringData*) pData = (wxStringData*)
realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar)); realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxChar));
if ( pData == NULL ) { if ( pData == NULL ) {
// allocation failures are handled by the caller // allocation failures are handled by the caller
// keep previous data since reallocation failed // keep previous data since reallocation failed
@@ -451,7 +451,7 @@ bool wxString::Alloc(size_t nLen)
// allocation failure handled by caller // allocation failure handled by caller
return FALSE; return FALSE;
} }
pData->nRefs = 1; pData->nRefs = 1;
pData->nDataLength = 0; pData->nDataLength = 0;
pData->nAllocLength = nLen; pData->nAllocLength = nLen;
@@ -759,43 +759,53 @@ wxString operator+(const wxChar *psz, const wxString& str)
// =========================================================================== // ===========================================================================
#if wxUSE_UNICODE #if wxUSE_UNICODE
wxString wxString::FromAscii( char *ascii )
wxString wxString::FromAscii(const char *ascii)
{ {
if (!ascii) if (!ascii)
return wxEmptyString; return wxEmptyString;
size_t len = strlen( ascii ); size_t len = strlen( ascii );
wxString res; wxString res;
res.AllocBuffer( len );
wchar_t *dest = (wchar_t*)(const wchar_t*) res.c_str(); if ( len )
{
for (size_t i = 0; i < len+1; i++) wxStringBuffer buf(res, len);
dest[i] = (wchar_t) ascii[i];
wchar_t *dest = buf;
for ( ;; )
{
if ( (*dest++ = (wchar_t)(unsigned char)*ascii++) == L'\0' )
break;
}
}
return res; return res;
} }
const wxCharBuffer wxString::ToAscii() const const wxCharBuffer wxString::ToAscii() const
{ {
if (IsNull()) // this will allocate enough space for the terminating NUL too
return wxCharBuffer( (const char*)NULL ); wxCharBuffer buffer(length());
size_t len = Len(); signed char *dest = (signed char *)buffer.data();
wxCharBuffer buffer( len ); // allocates len+1
const wchar_t *pwc = c_str();
char *dest = (char*)(const char*) buffer; for ( ;; )
for (size_t i = 0; i < len+1; i++)
{ {
if (m_pchData[i] > 127) *dest++ = *pwc > SCHAR_MAX ? '_' : *pwc;
dest[i] = '_';
else // the output string can't have embedded NULs anyhow, so we can safely
dest[i] = (char) m_pchData[i]; // stop at first of them even if we do have any
if ( !*pwc++ )
break;
} }
return buffer; return buffer;
} }
#endif
#endif // Unicode
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// simple sub-string extraction // simple sub-string extraction
@@ -1048,7 +1058,7 @@ wxString& wxString::MakeUpper()
wxFAIL_MSG( _T("out of memory in wxString::MakeUpper") ); wxFAIL_MSG( _T("out of memory in wxString::MakeUpper") );
return *this; return *this;
} }
for ( wxChar *p = m_pchData; *p; p++ ) for ( wxChar *p = m_pchData; *p; p++ )
*p = (wxChar)wxToupper(*p); *p = (wxChar)wxToupper(*p);
@@ -2237,7 +2247,7 @@ size_t wxArrayString::Add(const wxString& str, size_t nInsert)
Grow(nInsert); Grow(nInsert);
for (size_t i = 0; i < nInsert; i++) for (size_t i = 0; i < nInsert; i++)
{ {
// the string data must not be deleted! // the string data must not be deleted!
str.GetStringData()->Lock(); str.GetStringData()->Lock();