X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/d22699b5ebc820af126478d8ef5da4385cef940b..7c4eef7a75398aab1285e3ad2971c59ea3130293:/src/common/string.cpp?ds=inline diff --git a/src/common/string.cpp b/src/common/string.cpp index 47729557c6..5782d1374b 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -85,10 +85,10 @@ static const struct { wxStringData data; wxChar dummy; -} g_strEmpty = { {-1, 0, 0}, _T('\0') }; +} g_strEmpty = { {-1, 0, 0}, T('\0') }; // empty C style string: points to 'string data' byte of g_strEmpty -extern const wxChar WXDLLEXPORT *g_szNul = &g_strEmpty.dummy; +extern const wxChar WXDLLEXPORT *wxEmptyString = &g_strEmpty.dummy; // ---------------------------------------------------------------------------- // conditional compilation @@ -284,7 +284,7 @@ wxString::wxString(const char *psz, wxMBConv& conv, size_t nLength) } } -#else +#else // ANSI #if wxUSE_WCHAR_T // from wide string @@ -302,9 +302,9 @@ wxString::wxString(const wchar_t *pwz) Init(); } } -#endif +#endif // wxUSE_WCHAR_T -#endif +#endif // Unicode/ANSI // --------------------------------------------------------------------------- // memory allocation @@ -327,7 +327,7 @@ void wxString::AllocBuffer(size_t nLen) pData->nDataLength = nLen; pData->nAllocLength = nLen + EXTRA_ALLOC; m_pchData = pData->data(); // data starts after wxStringData - m_pchData[nLen] = _T('\0'); + m_pchData[nLen] = T('\0'); } // must be called before changing this string @@ -379,7 +379,7 @@ void wxString::Alloc(size_t nLen) pData->nDataLength = 0; pData->nAllocLength = nLen; m_pchData = pData->data(); // data starts after wxStringData - m_pchData[0u] = _T('\0'); + m_pchData[0u] = T('\0'); } else if ( pData->IsShared() ) { pData->Unlock(); // memory not freed because shared @@ -461,7 +461,7 @@ void wxString::AssignCopy(size_t nSrcLen, const wxChar *pszSrcData) AllocBeforeWrite(nSrcLen); memcpy(m_pchData, pszSrcData, nSrcLen*sizeof(wxChar)); GetStringData()->nDataLength = nSrcLen; - m_pchData[nSrcLen] = _T('\0'); + m_pchData[nSrcLen] = T('\0'); } } @@ -563,7 +563,7 @@ void wxString::ConcatSelf(int nSrcLen, const wxChar *pszSrcData) // fast concatenation - all is done in our buffer memcpy(m_pchData + nLen, pszSrcData, nSrcLen*sizeof(wxChar)); - m_pchData[nNewLen] = _T('\0'); // put terminating '\0' + m_pchData[nNewLen] = T('\0'); // put terminating '\0' GetStringData()->nDataLength = nNewLen; // and fix the length } //else: the string to append was empty @@ -722,7 +722,7 @@ wxString wxString::Left(size_t nCount) const wxString wxString::BeforeFirst(wxChar ch) const { wxString str; - for ( const wxChar *pc = m_pchData; *pc != _T('\0') && *pc != ch; pc++ ) + for ( const wxChar *pc = m_pchData; *pc != T('\0') && *pc != ch; pc++ ) str += *pc; return str; @@ -762,7 +762,7 @@ size_t wxString::Replace(const wxChar *szOld, const wxChar *szNew, bool bReplace wxString strTemp; const wxChar *pCurrent = m_pchData; const wxChar *pSubstr; - while ( *pCurrent != _T('\0') ) { + while ( *pCurrent != T('\0') ) { pSubstr = wxStrstr(pCurrent, szOld); if ( pSubstr == NULL ) { // strTemp is unused if no replacements were made, so avoid the copy @@ -882,7 +882,7 @@ wxString& wxString::Trim(bool bFromRight) psz--; // truncate at trailing space start - *++psz = _T('\0'); + *++psz = T('\0'); GetStringData()->nDataLength = psz - m_pchData; } else @@ -924,7 +924,7 @@ wxString& wxString::Truncate(size_t uiLen) if ( uiLen < Len() ) { CopyBeforeWrite(); - *(m_pchData + uiLen) = _T('\0'); + *(m_pchData + uiLen) = T('\0'); GetStringData()->nDataLength = uiLen; } //else: nothing to do, string is already short enough @@ -958,7 +958,7 @@ int wxString::Find(const wxChar *pszSub) const wxString& wxString::operator<<(int i) { wxString res; - res.Printf(_T("%d"), i); + res.Printf(T("%d"), i); return (*this) << res; } @@ -966,7 +966,7 @@ wxString& wxString::operator<<(int i) wxString& wxString::operator<<(float f) { wxString res; - res.Printf(_T("%f"), f); + res.Printf(T("%f"), f); return (*this) << res; } @@ -974,7 +974,7 @@ wxString& wxString::operator<<(float f) wxString& wxString::operator<<(double d) { wxString res; - res.Printf(_T("%g"), d); + res.Printf(T("%g"), d); return (*this) << res; } @@ -1011,7 +1011,7 @@ int wxString::PrintfV(const wxChar* pszFormat, va_list argptr) Reinit(); for (size_t n = 0; pszFormat[n]; n++) - if (pszFormat[n] == _T('%')) { + if (pszFormat[n] == T('%')) { static char s_szFlags[256] = "%"; size_t flagofs = 1; bool adj_left = FALSE, in_prec = FALSE, @@ -1021,55 +1021,55 @@ int wxString::PrintfV(const wxChar* pszFormat, va_list argptr) do { #define CHECK_PREC if (in_prec && !prec_dot) { s_szFlags[flagofs++] = '.'; prec_dot = TRUE; } switch (pszFormat[++n]) { - case _T('\0'): + case T('\0'): done = TRUE; break; - case _T('%'): - *this += _T('%'); + case T('%'): + *this += T('%'); done = TRUE; break; - case _T('#'): - case _T('0'): - case _T(' '): - case _T('+'): - case _T('\''): + case T('#'): + case T('0'): + case T(' '): + case T('+'): + case T('\''): CHECK_PREC s_szFlags[flagofs++] = pszFormat[n]; break; - case _T('-'): + case T('-'): CHECK_PREC adj_left = TRUE; s_szFlags[flagofs++] = pszFormat[n]; break; - case _T('.'): + case T('.'): CHECK_PREC in_prec = TRUE; prec_dot = FALSE; max_width = 0; // dot will be auto-added to s_szFlags if non-negative number follows break; - case _T('h'): + case T('h'): ilen = -1; CHECK_PREC s_szFlags[flagofs++] = pszFormat[n]; break; - case _T('l'): + case T('l'): ilen = 1; CHECK_PREC s_szFlags[flagofs++] = pszFormat[n]; break; - case _T('q'): - case _T('L'): + case T('q'): + case T('L'): ilen = 2; CHECK_PREC s_szFlags[flagofs++] = pszFormat[n]; break; - case _T('Z'): + case T('Z'): ilen = 3; CHECK_PREC s_szFlags[flagofs++] = pszFormat[n]; break; - case _T('*'): + case T('*'): { int len = va_arg(argptr, int); if (in_prec) { @@ -1087,15 +1087,15 @@ int wxString::PrintfV(const wxChar* pszFormat, va_list argptr) flagofs += ::sprintf(s_szFlags+flagofs,"%d",len); } break; - case _T('1'): case _T('2'): case _T('3'): - case _T('4'): case _T('5'): case _T('6'): - case _T('7'): case _T('8'): case _T('9'): + case T('1'): case T('2'): case T('3'): + case T('4'): case T('5'): case T('6'): + case T('7'): case T('8'): case T('9'): { int len = 0; CHECK_PREC - while ((pszFormat[n]>=_T('0')) && (pszFormat[n]<=_T('9'))) { + while ((pszFormat[n]>=T('0')) && (pszFormat[n]<=T('9'))) { s_szFlags[flagofs++] = pszFormat[n]; - len = len*10 + (pszFormat[n] - _T('0')); + len = len*10 + (pszFormat[n] - T('0')); n++; } if (in_prec) max_width = len; @@ -1103,12 +1103,12 @@ int wxString::PrintfV(const wxChar* pszFormat, va_list argptr) n--; // the main loop pre-increments n again } break; - case _T('d'): - case _T('i'): - case _T('o'): - case _T('u'): - case _T('x'): - case _T('X'): + case T('d'): + case T('i'): + case T('o'): + case T('u'): + case T('x'): + case T('X'): CHECK_PREC s_szFlags[flagofs++] = pszFormat[n]; s_szFlags[flagofs] = '\0'; @@ -1140,11 +1140,11 @@ int wxString::PrintfV(const wxChar* pszFormat, va_list argptr) *this += wxString(s_szScratch); done = TRUE; break; - case _T('e'): - case _T('E'): - case _T('f'): - case _T('g'): - case _T('G'): + case T('e'): + case T('E'): + case T('f'): + case T('g'): + case T('G'): CHECK_PREC s_szFlags[flagofs++] = pszFormat[n]; s_szFlags[flagofs] = '\0'; @@ -1158,7 +1158,7 @@ int wxString::PrintfV(const wxChar* pszFormat, va_list argptr) *this += wxString(s_szScratch); done = TRUE; break; - case _T('p'): + case T('p'): { void *val = va_arg(argptr, void *); CHECK_PREC @@ -1169,7 +1169,7 @@ int wxString::PrintfV(const wxChar* pszFormat, va_list argptr) done = TRUE; } break; - case _T('c'): + case T('c'): { wxChar val = va_arg(argptr, int); // we don't need to honor padding here, do we? @@ -1177,7 +1177,7 @@ int wxString::PrintfV(const wxChar* pszFormat, va_list argptr) done = TRUE; } break; - case _T('s'): + case T('s'): if (ilen == -1) { // wx extension: we'll let %hs mean non-Unicode strings char *val = va_arg(argptr, char *); @@ -1188,26 +1188,26 @@ int wxString::PrintfV(const wxChar* pszFormat, va_list argptr) size_t len = wxSTRING_MAXLEN; if (val) { for (len = 0; val[len] && (len - -size_t wxMBConvGdk::MB2WC(wchar_t *buf, const char *psz, size_t n) const -{ - if (buf) { - return gdk_mbstowcs((GdkWChar *)buf, psz, n); - } else { - GdkWChar *nbuf = new GdkWChar[n=strlen(psz)]; - size_t len = gdk_mbstowcs(nbuf, psz, n); - delete [] nbuf; - return len; - } -} - -size_t wxMBConvGdk::WC2MB(char *buf, const wchar_t *psz, size_t n) const -{ - char *mbstr = gdk_wcstombs((GdkWChar *)psz); - size_t len = mbstr ? strlen(mbstr) : 0; - if (buf) { - if (len > n) len = n; - memcpy(buf, psz, len); - if (len < n) buf[len] = 0; - } - return len; -} -#endif // GTK > 1.0 - -// ---------------------------------------------------------------------------- -// UTF-7 -// ---------------------------------------------------------------------------- - -WXDLLEXPORT_DATA(wxMBConvUTF7) wxConvUTF7; - -#if 0 -static char utf7_setD[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789'(),-./:?"; -static char utf7_setO[]="!\"#$%&*;<=>@[]^_`{|}"; -static char utf7_setB[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; -#endif - -// TODO: write actual implementations of UTF-7 here -size_t wxMBConvUTF7::MB2WC(wchar_t * WXUNUSED(buf), - const char * WXUNUSED(psz), - size_t WXUNUSED(n)) const -{ - return 0; -} - -size_t wxMBConvUTF7::WC2MB(char * WXUNUSED(buf), - const wchar_t * WXUNUSED(psz), - size_t WXUNUSED(n)) const -{ - return 0; -} - -// ---------------------------------------------------------------------------- -// UTF-8 -// ---------------------------------------------------------------------------- - -WXDLLEXPORT_DATA(wxMBConvUTF8) wxConvUTF8; - -static unsigned long utf8_max[]={0x7f,0x7ff,0xffff,0x1fffff,0x3ffffff,0x7fffffff,0xffffffff}; - -size_t wxMBConvUTF8::MB2WC(wchar_t *buf, const char *psz, size_t n) const -{ - size_t len = 0; - - while (*psz && ((!buf) || (len>cnt); - while (cnt--) { - cc = *psz++; - if ((cc&0xC0)!=0x80) { - // invalid UTF-8 sequence - return (size_t)-1; - } - res=(res<<6)|(cc&0x3f); - } - if (res<=utf8_max[ocnt]) { - // illegal UTF-8 encoding - return (size_t)-1; - } - if (buf) *buf++=res; - len++; - } - } - } - if (buf && (lenutf8_max[cnt]; cnt++); - if (!cnt) { - // plain ASCII char - if (buf) *buf++=cc; - len++; - } else { - len+=cnt+1; - if (buf) { - *buf++=(-128>>cnt)|((cc>>(cnt*6))&(0x3f>>cnt)); - while (cnt--) - *buf++=0x80|((cc>>(cnt*6))&0x3f); - } - } - } - if (buf && (lennames.Add(token.GetNextToken()); - } - else if (cmd == _T("")) - cset->names.Add(token.GetNextToken()); - else if (cmd == _T("")) - comchar = token.GetNextToken(); - else if (cmd == _T("")) - escchar = token.GetNextToken(); - else if (cmd == _T("")) { - delete cset; - cset = (wxCharacterSet *) NULL; - break; // we don't support multibyte charsets ourselves (yet) - } - else if (cmd == _T("CHARMAP")) { - cset->data = (wchar_t *)calloc(256, sizeof(wchar_t)); - in_charset = TRUE; - } - else if (cmd == _T("END")) { - if (token.GetNextToken() == _T("CHARMAP")) - in_charset = FALSE; - } - else if (in_charset) { - // format: /x00 NULL (NUL) - // /x41 LATIN CAPITAL LETTER A - wxString hex = token.GetNextToken(); - // skip whitespace (why doesn't wxStringTokenizer do this?) - while (wxIsEmpty(hex) && token.HasMoreTokens()) hex = token.GetNextToken(); - wxString uni = token.GetNextToken(); - // skip whitespace again - while (wxIsEmpty(uni) && token.HasMoreTokens()) uni = token.GetNextToken(); - if ((hex.Len() > 2) && (wxString(hex.GetChar(0)) == escchar) && (hex.GetChar(1) == _T('x')) && - (uni.Left(2) == _T("=0) { - unsigned long uni1 = ::wxHexToDec(uni.Mid(2,2)); - unsigned long uni2 = ::wxHexToDec(uni.Mid(4,2)); - cset->data[pos] = (uni1 << 16) | uni2; - // wxFprintf(stderr,_T("char %02x mapped to %04x (%c)\n"),pos,cset->data[pos],cset->data[pos]); - } - } - } - } - if (cset) { - cset->names.Shrink(); - wxCharsets.Add(cset); - } - } - } -#endif - wxCharsets.Shrink(); -} - -static wxCharacterSet *wxFindCharacterSet(const wxChar *charset) -{ - if (!charset) return (wxCharacterSet *)NULL; - wxLoadCharacterSets(); - for (size_t n=0; n4) { - if (wxString(charset,4) == _T("8859")) { - codeset << _T("8859-"); - if (*charset == _T('-')) charset++; - } - } - } - codeset << charset; - codeset.MakeUpper(); - m_name = wxStrdup(codeset.c_str()); - m_deferred = TRUE; -#endif - } -} - -void wxCSConv::LoadNow() -{ -// wxPrintf(_T("Conversion request\n")); - if (m_deferred) { - if (!m_name) { -#ifdef __UNIX__ - wxChar *lang = wxGetenv(_T("LANG")); - wxChar *dot = lang ? wxStrchr(lang, _T('.')) : (wxChar *)NULL; - if (dot) SetName(dot+1); -#endif - } - m_cset = wxFindCharacterSet(m_name); - m_deferred = FALSE; - } -} - -size_t wxCSConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const -{ - ((wxCSConv *)this)->LoadNow(); // discard constness - if (buf) { - if (m_cset) { - for (size_t c=0; cdata[(unsigned char)(psz[c])]; - } else { - // latin-1 (direct) - for (size_t c=0; cLoadNow(); // discard constness - if (buf) { - if (m_cset) { - for (size_t c=0; cdata[n] != psz[c]); n++); - buf[c] = (n>0xff) ? '?' : n; - } - } else { - // latin-1 (direct) - for (size_t c=0; c0xff) ? '?' : psz[c]; - } - return n; - } - return wcslen(psz); -} - -#endif//wxUSE_WCHAR_T - -#if wxUSE_WCHAR_T -const wxWCharBuffer wxMBConv::cMB2WC(const char *psz) const - { - if (psz) { - size_t nLen = MB2WC((wchar_t *) NULL, psz, 0); - wxWCharBuffer buf(nLen); - MB2WC(WCSTRINGCAST buf, psz, nLen); - return buf; - } else return wxWCharBuffer((wchar_t *) NULL); - } - -const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *psz) const - { - if (psz) { - size_t nLen = WC2MB((char *) NULL, psz, 0); - wxCharBuffer buf(nLen); - WC2MB(MBSTRINGCAST buf, psz, nLen); - return buf; - } else return wxCharBuffer((char *) NULL); - } - -#endif//wxUSE_WCHAR_T -