X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/6244848856584171db8cd74a7ff227250e46cc54..884470b12ee25b20796bfef11a9bfa5fb1e1511c:/src/common/string.cpp diff --git a/src/common/string.cpp b/src/common/string.cpp index dcd371e0d2..5329f085e2 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -41,13 +41,11 @@ #include #include -// This probably isn't right, what should it be Vadim? -// Otherwise we end up with no wxVsprintf defined. -#ifdef __WXMOTIF__ -#define HAVE_VPRINTF +#ifdef __SALFORDC__ +#include #endif -#ifdef wxUSE_WCSRTOMBS +#if wxUSE_WCSRTOMBS #include // for wcsrtombs(), see comments where it's used #endif // GNU @@ -64,9 +62,9 @@ // static class variables definition // --------------------------------------------------------------------------- -#ifdef STD_STRING_COMPATIBILITY +#ifdef wxSTD_STRING_COMPATIBILITY const size_t wxString::npos = STRING_MAXLEN; -#endif +#endif // wxSTD_STRING_COMPATIBILITY // ---------------------------------------------------------------------------- // static data @@ -82,7 +80,7 @@ static const struct } g_strEmpty = { {-1, 0, 0}, '\0' }; // empty C style string: points to 'string data' byte of g_strEmpty -extern const char *g_szNul = &g_strEmpty.dummy; +extern const char WXDLLEXPORT *g_szNul = &g_strEmpty.dummy; // ---------------------------------------------------------------------------- // conditional compilation @@ -106,14 +104,16 @@ extern const char *g_szNul = &g_strEmpty.dummy; // always available), but it's unsafe because it doesn't check for buffer // size - so give a warning #define wxVsprintf(buffer,len,format,argptr) vsprintf(buffer,format, argptr) +#ifndef __SC__ #pragma message("Using sprintf() because no snprintf()-like function defined") #endif +#endif // ---------------------------------------------------------------------------- // global functions // ---------------------------------------------------------------------------- -#ifdef STD_STRING_COMPATIBILITY +#ifdef wxSTD_STRING_COMPATIBILITY // MS Visual C++ version 5.0 provides the new STL headers as well as the old // iostream ones. @@ -257,7 +257,7 @@ wxString::wxString(const unsigned char* psz, size_t nLength) InitWith((const char *)psz, 0, nLength); } -#ifdef STD_STRING_COMPATIBILITY +#ifdef wxSTD_STRING_COMPATIBILITY // poor man's iterators are "void *" pointers wxString::wxString(const void *pStart, const void *pEnd) @@ -275,7 +275,7 @@ wxString::wxString(const wchar_t *pwz) // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't // honor the 3rd parameter, thus it will happily crash here). -#ifdef wxUSE_WCSRTOMBS +#if wxUSE_WCSRTOMBS // don't know if it's really needed (or if we can pass NULL), but better safe // than quick mbstate_t mbstate; @@ -345,6 +345,10 @@ void wxString::AllocBeforeWrite(size_t nLen) pData->Unlock(); AllocBuffer(nLen); } + else { + // update the string length + pData->nDataLength = nLen; + } wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner } @@ -672,11 +676,11 @@ wxString wxString::Right(size_t nCount) const // get all characters after the last occurence of ch // (returns the whole string if ch not found) -wxString wxString::Right(char ch) const +wxString wxString::AfterLast(char ch) const { wxString str; int iPos = Find(ch, TRUE); - if ( iPos == NOT_FOUND ) + if ( iPos == wxNOT_FOUND ) str = *this; else str = c_str() + iPos + 1; @@ -697,7 +701,7 @@ wxString wxString::Left(size_t nCount) const // get all characters before the first occurence of ch // (returns the whole string if ch not found) -wxString wxString::Left(char ch) const +wxString wxString::BeforeFirst(char ch) const { wxString str; for ( const char *pc = m_pchData; *pc != '\0' && *pc != ch; pc++ ) @@ -708,11 +712,11 @@ wxString wxString::Left(char ch) const /// get all characters before the last occurence of ch /// (returns empty string if ch not found) -wxString wxString::Before(char ch) const +wxString wxString::BeforeLast(char ch) const { wxString str; int iPos = Find(ch, TRUE); - if ( iPos != NOT_FOUND && iPos != 0 ) + if ( iPos != wxNOT_FOUND && iPos != 0 ) str = wxString(c_str(), iPos); return str; @@ -720,11 +724,11 @@ wxString wxString::Before(char ch) const /// get all characters after the first occurence of ch /// (returns empty string if ch not found) -wxString wxString::After(char ch) const +wxString wxString::AfterFirst(char ch) const { wxString str; int iPos = Find(ch); - if ( iPos != NOT_FOUND ) + if ( iPos != wxNOT_FOUND ) str = c_str() + iPos + 1; return str; @@ -871,7 +875,7 @@ wxString& wxString::Trim(bool bFromRight) psz++; // fix up data and length - int nDataLength = GetStringData()->nDataLength - (psz - m_pchData); + int nDataLength = GetStringData()->nDataLength - (psz - (const char*) m_pchData); memmove(m_pchData, psz, (nDataLength + 1)*sizeof(char)); GetStringData()->nDataLength = nDataLength; } @@ -911,7 +915,7 @@ wxString& wxString::Truncate(size_t uiLen) } // --------------------------------------------------------------------------- -// finding (return NOT_FOUND if not found and index otherwise) +// finding (return wxNOT_FOUND if not found and index otherwise) // --------------------------------------------------------------------------- // find a character @@ -919,7 +923,7 @@ int wxString::Find(char ch, bool bFromEnd) const { const char *psz = bFromEnd ? strrchr(m_pchData, ch) : strchr(m_pchData, ch); - return (psz == NULL) ? NOT_FOUND : psz - m_pchData; + return (psz == NULL) ? wxNOT_FOUND : psz - (const char*) m_pchData; } // find a sub-string (like strstr) @@ -927,7 +931,7 @@ int wxString::Find(const char *pszSub) const { const char *psz = strstr(m_pchData, pszSub); - return (psz == NULL) ? NOT_FOUND : psz - m_pchData; + return (psz == NULL) ? wxNOT_FOUND : psz - (const char*) m_pchData; } // --------------------------------------------------------------------------- @@ -1075,10 +1079,23 @@ bool wxString::Matches(const char *pszMask) const return *pszTxt == '\0'; } +// Count the number of chars +int wxString::Freq(char ch) const +{ + int count = 0; + int len = Len(); + for (int i = 0; i < len; i++) + { + if (GetChar(i) == ch) + count ++; + } + return count; +} + // --------------------------------------------------------------------------- // standard C++ library string functions // --------------------------------------------------------------------------- -#ifdef STD_STRING_COMPATIBILITY +#ifdef wxSTD_STRING_COMPATIBILITY wxString& wxString::insert(size_t nPos, const wxString& str) { @@ -1372,7 +1389,7 @@ int wxArrayString::Index(const char *sz, bool bCase, bool bFromEnd) const } } - return NOT_FOUND; + return wxNOT_FOUND; } // add item at the end @@ -1423,7 +1440,7 @@ void wxArrayString::Remove(const char *sz) { int iIndex = Index(sz); - wxCHECK_RET( iIndex != NOT_FOUND, + wxCHECK_RET( iIndex != wxNOT_FOUND, _("removing inexistent element in wxArrayString::Remove") ); Remove(iIndex);