📄 stdstring.h
字号:
CStdString& operator=(TCHAR); // takes single character of TCHAR type
CStdString& operator=(TOTHER); // takes single character of the "other" type
#ifdef STDSTRING_INC_COMDEF
CStdString& operator=(const _bstr_t& bstr); // takes the _bstr_t MS compiler COM support class
#endif
// These overloads are also needed to fix the MSVC assignment bug (only for MS' STL -- KB: Q172398).
// *** Thanks to Pete The Plumber for catching this one ***
CStdString& assign(const CStdString& str, CStdString::size_type nStart, CStdString::size_type nChars);
CStdString& assign(PCTSTR pT, CStdString::size_type nChars);
CStdString& assign(CStdString::size_type nChars, CStdString::value_type val);
CStdString& assign(const CStdString& str);
CStdString& assign(PCTSTR pT);
CStdString& assign(CStdString::const_iterator iterFirst, CStdString::const_iterator iterLast);
// comparison (equality)
bool operator==(const CStdString& str) const;
bool operator==(const STRBASE& str) const;
bool operator==(const STROTHER& str) const;
bool operator==(PCTSTR pT) const;
bool operator==(PCOSTR pO) const;
bool operator==(TCHAR t) const;
bool operator==(TOTHER t) const;
bool Equals(PCTSTR pT, bool bUseCase=false) const; // allows (& defaults to) case INsensitive comparison
// bool WildcardEquals(PCTSTR pT, bool bUseCase=false) const; // allows (& defaults to) case INsensitive comparison and allows wildcards
// comparison operators (inequality)
bool operator!=(const CStdString& str) const;
bool operator!=(const STRBASE& str) const;
bool operator!=(const STROTHER& str) const;
bool operator!=(PCTSTR pT) const;
bool operator!=(PCOSTR pO) const;
bool operator!=(TCHAR t) const;
bool operator!=(TOTHER t) const;
// comparison operator (less than)
bool operator<(const CStdString& str) const;
bool operator<(PCTSTR str) const;
// concatenation.
const CStdString& operator+=(const CStdString& str);
const CStdString& operator+=(const STRBASE& str);
const CStdString& operator+=(const STROTHER& str);
const CStdString& operator+=(PCTSTR pT);
const CStdString& operator+=(PCOSTR pO);
const CStdString& operator+=(TCHAR t);
const CStdString& operator+=(TOTHER t);
#ifdef STDSTRING_INC_COMDEF
const CStdString& operator+=(const _bstr_t& bstr);
#endif
// addition operators -- global friend functions.
friend CStdString operator+(const CStdString& str1, const CStdString& str2);
friend CStdString operator+(const CStdString& str, TCHAR ch);
friend CStdString operator+(TCHAR ch, const CStdString& str);
friend CStdString operator+(const CStdString& str, TOTHER ch);
friend CStdString operator+(TOTHER ch, const CStdString& str);
friend CStdString operator+(const CStdString& str, PCTSTR sz);
friend CStdString operator+(PCTSTR sz, const CStdString& str);
friend CStdString operator+(const CStdString& str, PCOSTR sz);
friend CStdString operator+(PCOSTR sz, const CStdString& str);
#ifdef STDSTRING_INC_COMDEF
friend CStdString operator+(const _bstr_t& bstr, const CStdString& str);
friend CStdString operator+(const CStdString& str, const _bstr_t& bstr);
#endif
// Conversion to "other" type of string (i.e. to string if we're a wstring, to wstring if we're a string)
STROTHER Other() const { USES_CONVERSION; return T2CO(c_str()); };
// Functions that make us almost as easy to use as MFC's CString
operator PCTSTR() const; // implicit cast to const TCHAR*
bool Load(UINT nId); // load using resource id
void Format(PCTSTR szFormat, ...); // format using literal string
void Format(UINT nId, ...); // format using resource string identifier
void FormatV(PCTSTR szFormat, va_list argList); // format using a va_list instead of ...
CStdString& TrimRight(); // chop off all whitespace characters on right
CStdString& TrimLeft(); // chop off all whitespace characters on left
CStdString& ToUpper(); // make uppercase
CStdString& ToLower(); // make lowercase
bool IsEmpty() const { return empty(); };
// Array-indexing operators. I thought we did not need to redefine these, but they don't work unless we
// redefine them here. (Thanks to Julian Selman for pointing this out)
TCHAR& operator[](int nIdx);
const TCHAR& operator[](int nIdx) const;
#ifdef STDSTRING_INC_COMDEF
HRESULT StreamSave(IStream* pStream) const; // write to an IStream
HRESULT StreamLoad(IStream* pStream); // read from an IStream;
ULONG StreamSize() const; // how many bytes needed to write to an IStream?
#endif
// static bool Wildstrcmp (PCTSTR c_s, PCTSTR c_mask);
// These static functions simplify copying one C-style string to another, either one or two byte.
static void CopyString(PCTSTR p_szSource, PTSTR p_szDest, int p_nMaxChars=0);
static void CopyString(PCOSTR p_szSource, POSTR p_szDest, int p_nMaxChars=0);
static void CopyString(PCSTR p_szSource, PWSTR p_szDest, int p_nMaxChars=0);
static void CopyString(PCWSTR p_szSource, PSTR, int p_nMaxChars=0);
// If this is NOT an MFC build, provide a SetResourceHandle function so that those who call
// the versions of Load(), Format(), or the constructor that take resource IDs can provide
// an alternate HINST of a DLL to search. This is not exactly the list of link libraries
// that MFC provides but it's better than nothing. It's also not thread safe but I
static void SetResourceHandle(HINSTANCE hNew);
static HINSTANCE GetResourceHandle();
protected:
#if !defined(_MFC_VER)
static HINSTANCE hInstResource; // implementation of Get/SetResourceHandle in non-MFC builds
#endif
};
#define _TRES(pctstr) (LOWORD((DWORD)(pctstr))) // shorthand conversion from PCTSTR to string resource ID
// CStdString inline constructors
inline CStdString::CStdString() {}
inline CStdString::CStdString(const STRBASE& str) { *this = str; };
//inline CStdString::CStdString(PCTSTR pT) { *this = (pT == NULL ? szTNull : pT); };
inline CStdString::CStdString(PCOSTR pO) { *this = (pO == NULL ? szONull : pO); };
inline CStdString::CStdString(const CStdString& str) { *this = str; };
inline CStdString::CStdString(const STROTHER& str) { *this = str; };
inline CStdString::CStdString(const_iterator first, const_iterator last) : STRBASE(first, last) {};
inline CStdString::CStdString(size_type nSize, value_type ch, const allocator_type& al) : STRBASE(nSize, ch, al) { }
#ifdef STDSTRING_INC_COMDEF
inline CStdString::CStdString(const _bstr_t& bstr) { *this = static_cast<PCTSTR>(bstr); }
#endif
// CStdString inline assignment operators -- the erase() function
// call fixes the MSVC assignment bug (see knowledge base article Q172398).
inline CStdString& CStdString::operator=(const CStdString& str)
{ if ( str.data() != data() ) { erase(); STRBASE::assign(str.data());} return *this; };
inline CStdString& CStdString::operator=(const STRBASE& str)
{ if ( str.data() != data() ) { erase(); STRBASE::assign(str.data());} return *this; };
inline CStdString& CStdString::operator=(const STROTHER& str)
{ erase(); USES_CONVERSION; STRBASE::assign(O2CT(str.data())); return *this; };
inline CStdString& CStdString::operator=(PCTSTR pT)
{ if ( pT != data() ) { erase(); if ( pT != NULL ) STRBASE::assign(pT); } return *this; };
inline CStdString& CStdString::operator=(PCOSTR pO)
{ erase(); if ( pO != NULL ) { USES_CONVERSION; STRBASE::assign(O2CT(pO)); } return *this; };
inline CStdString& CStdString::operator=(TOTHER t)
{ erase(); USES_CONVERSION; STRBASE::assign(O2CT(&t)); return *this; };
inline CStdString& CStdString::operator=(TCHAR t)
{ erase(); STRBASE::assign(1,t); return *this; };
#ifdef STDSTRING_INC_COMDEF
inline CStdString& CStdString::operator=(const _bstr_t& bstr)
{ return operator=(static_cast<PCTSTR>(bstr)); }
#endif
// These overloads are also needed to fix the MSVC assignment bug (KB: Q172398)
// *** Thanks to Pete The Plumber for catching this one ***
inline CStdString& CStdString::assign(const CStdString& str, CStdString::size_type nStart, CStdString::size_type nChars)
{ if( str.data() != data() ) erase(); STRBASE::assign(str, nStart, nChars); return *this; }
inline CStdString& CStdString::assign(PCTSTR pT, CStdString::size_type nChars)
{ if( pT != data() ) erase(); STRBASE::assign(pT, nChars); return *this; }
inline CStdString& CStdString::assign(CStdString::size_type nChars, CStdString::value_type val)
{ erase(); STRBASE::assign(nChars, val); return *this; }
inline CStdString& CStdString::assign(const CStdString& str)
{ return assign(str, 0, npos); }
inline CStdString& CStdString::assign(PCTSTR pT)
{ return assign(pT, CStdString::traits_type::length(pT)); }
inline CStdString& CStdString::assign(CStdString::const_iterator iterFirst, CStdString::const_iterator iterLast)
{ replace(begin(), end(), iterFirst, iterLast); return *this; }
// CStdString inline comparison (equality)
inline bool CStdString::operator==(const CStdString& str) const
{ return compare(str.c_str()) == 0; };
inline bool CStdString::operator==(const STRBASE& str) const
{ return compare(str.data()) == 0; };
inline bool CStdString::operator==(const STROTHER& str) const
{ USES_CONVERSION; return compare(O2CT(str.data())) == 0; };
inline bool CStdString::operator==(PCTSTR pT) const
{ return compare(pT) == 0; };
inline bool CStdString::operator==(PCOSTR pO) const
{ USES_CONVERSION; return compare(O2CT(pO)) == 0; };
inline bool CStdString::operator==(TCHAR t) const
{ return operator==(STRBASE(1,t)); };
inline bool CStdString::operator==(TOTHER t) const
{ USES_CONVERSION; return operator==(CStdString(O2CT(&t))); };
inline bool CStdString::Equals(PCTSTR pT, bool bUseCase/*=false*/) const // defaults to case insensitive
{ return bUseCase ? compare(pT) == 0 : _tcsicmp(CStdString(*this), pT) == 0; } // get copy, THEN compare (thread safe)
//inline bool CStdString::WildcardEquals(PCTSTR pT, bool bUseCase/*=false*/) const // defaults to case insensitive
//{ CStdString me(*this), mask(pT); if( !bUseCase ) { me.ToUpper(); mask.ToUpper(); } return Wildstrcmp(me, mask); } // get copy, THEN compare (thread safe)
// CStdString inline comparison operators (inequality)
inline bool CStdString::operator!=(const CStdString& str) const
{ return compare(str.c_str()) != 0; };
inline bool CStdString::operator!=(const STRBASE& str) const
{ return compare(str.data()) != 0; };
inline bool CStdString::operator!=(const STROTHER& str) const
{ USES_CONVERSION; return compare(O2CT(str.data())) != 0; };
inline bool CStdString::operator!=(PCTSTR pT) const
{ return compare(pT) != 0; };
inline bool CStdString::operator!=(PCOSTR pO) const
{ USES_CONVERSION; return compare(O2CT(pO)) != 0; };
inline bool CStdString::operator!=(TCHAR t) const
{ return operator!=(STRBASE(1,t)); };
inline bool CStdString::operator!=(TOTHER t) const
{ USES_CONVERSION; return operator!=(CStdString(O2CT(&t))); };
// CStdString comparison operator (less than)
inline bool CStdString::operator<(const CStdString& str) const
{ return compare(str) < 0; };
inline bool CStdString::operator<(PCTSTR str) const
{ return compare(str) < 0; };
// CStdString inline concatenation.
inline const CStdString& CStdString::operator+=(const CStdString& str)
{ append(str); return *this; };
inline const CStdString& CStdString::operator+=(const STRBASE& str)
{ append(str); return *this; };
inline const CStdString& CStdString::operator+=(const STROTHER& str)
{ USES_CONVERSION; *this += O2CT(str.c_str()); return *this; };
inline const CStdString& CStdString::operator+=(PCTSTR pT)
{ if ( pT != NULL ) append(pT); return *this; };
inline const CStdString& CStdString::operator+=(PCOSTR pO)
{ USES_CONVERSION; if ( pO != NULL ) append(O2CT(pO)); return *this; };
inline const CStdString& CStdString::operator+=(TCHAR t)
{ append(1, t); return *this; };
inline const CStdString& CStdString::operator+=(TOTHER t)
{ USES_CONVERSION; append(O2CT(&t)); return *this; };
#ifdef STDSTRING_INC_COMDEF
inline const CStdString& CStdString::operator+=(const _bstr_t& bstr)
{ return operator+=(static_cast<PCTSTR>(bstr)); }
#endif
// CStdString friend addition functions defined as inline
inline CStdString operator+(const CStdString& str1, const CStdString& str2)
{ CStdString strRet(str1); return strRet.append(str2); }
inline CStdString operator+(const CStdString& str, TCHAR ch)
{ CStdString strRet(str); return strRet.append(1, ch); }
inline CStdString operator+(TCHAR ch, const CStdString& str)
{ CStdString strRet(1, ch); return strRet.append(str); }
inline CStdString operator+(const CStdString& str, TOTHER ch)
{ return str + STROTHER(1, ch).data(); }
inline CStdString operator+(TOTHER ch, const CStdString& str)
{ CStdString strRet; strRet = ch; return strRet.append(str); }
inline CStdString operator+(const CStdString& str, PCTSTR sz)
{ CStdString strRet(str); return sz == NULL ? strRet : strRet.append(sz); }
inline CStdString operator+(PCTSTR sz, const CStdString& str)
{ CStdString strRet(sz); return strRet.append(str); }
inline CStdString operator+(const CStdString& str, PCOSTR sz)
{ CStdString strRet(str); return strRet.append(CStdString(sz)); }
inline CStdString operator+(PCOSTR sz, const CStdString& str)
{ CStdString strRet(sz); return strRet.append(str); }
#ifdef STDSTRING_INC_COMDEF
inline CStdString operator+(const _bstr_t& bstr, const CStdString& str)
{ return CStdString(static_cast<PCTSTR>(bstr)) + str; }
inline CStdString operator+(const CStdString& str, const _bstr_t& bstr)
{ return str + static_cast<PCTSTR>(bstr); }
#endif
inline TCHAR& CStdString::operator[](int nIdx) { return STRBASE::operator[](nIdx); }
inline const TCHAR& CStdString::operator[](int nIdx) const { return STRBASE::operator[](nIdx); }
inline CStdString::operator PCTSTR() const { return c_str(); }; // implicit cast to const TCHAR*
// In MFC builds, define some global serialization operators
#ifdef _MFC_VER
W32DLLSPEC CArchive& AFXAPI operator>>(CArchive& ar, CStdString& string);
W32DLLSPEC CArchive& AFXAPI operator<<(CArchive& ar, const CStdString& string);
inline void CStdString::SetResourceHandle(HINSTANCE hNew) { AfxSetResourceHandle(hNew); }
inline HINSTANCE CStdString::GetResourceHandle() { return AfxGetResourceHandle(); };
#else
inline void CStdString::SetResourceHandle(HINSTANCE hNew) { CStdString::hInstResource = hNew; }
inline HINSTANCE CStdString::GetResourceHandle() { return CStdString::hInstResource; };
#endif
// Some code still refers to old names of this class -- Account for this.
#define CSTLString CStdString
#define CW32String CStdString
// WUSysMessage -- return the system string corresponding to a system error or HRESULT value.
W32DLLSPEC CStdString WUSysMessage(DWORD dwError, DWORD p_dwLangId=MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT));
// WUFormat -- creates and formats a CStdString with one line of code instead of the two it normally takes
W32DLLSPEC CStdString WUFormat(PCTSTR szFormat, ...);
W32DLLSPEC CStdString WUFormat(UINT nId, ...);
// ------------------------------------------------------------------------------------------------------
// FUNCTIONAL COMPARATORS:
// REMARKS:
// These structs are derived from the std::binary_function template. They give us functional
// classes (which may be used in Standard C++ Library collections and algorithms) that perform
// case-insensitive comparisons of CStdString objects. This is useful for maps in which the key
// may be the proper string but in the wrong case.
// ------------------------------------------------------------------------------------------------------
#define StdStringLessNoCase _SSLNC // define shorter name to avoid compiler warning 4786
#define StdStringEqualsNoCase _SSENC // define shorter name to avoid compiler warning 4786
struct W32DLLSPEC StdStringLessNoCase : std::binary_function<CStdString, CStdString, bool>
{
bool operator()(const CStdString& strFirst, const CStdString& strSecond) const
{ return _tcsicmp(strFirst, strSecond) < 0; }
};
struct W32DLLSPEC StdStringEqualsNoCase : std::binary_function<CStdString, CStdString, bool>
{
bool operator()(const CStdString& strFirst, const CStdString& strSecond) const
{ return _tcsicmp(strFirst, strSecond) == 0; }
};
#endif // #ifndef _STDSTRING_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -