📄 stdstring.h
字号:
#define SSA2W(pa) (\
((_pa = pa) == 0) ? 0 : (\
_cvt = (strlen(_pa)+1),\
StdCodeCvt((PWSTR) _alloca(_cvt*2), _pa, _cvt)))
#define SSW2A(pw) (\
((_pw = pw) == 0) ? 0 : (\
_cvt = (wcslen(_pw)+1)*2,\
StdCodeCvt((LPSTR) _alloca(_cvt), _pw, _cvt)))
#endif
#define SSA2CW(pa) ((PCWSTR)SSA2W((pa)))
#define SSW2CA(pw) ((PCSTR)SSW2A((pw)))
#ifdef UNICODE
#define SST2A SSW2A
#define SSA2T SSA2W
#define SST2CA SSW2CA
#define SSA2CT SSA2CW
inline PWSTR SST2W(PTSTR p) { return p; }
inline PTSTR SSW2T(PWSTR p) { return p; }
inline PCWSTR SST2CW(PCTSTR p) { return p; }
inline PCTSTR SSW2CT(PCWSTR p) { return p; }
#else
#define SST2W SSA2W
#define SSW2T SSW2A
#define SST2CW SSA2CW
#define SSW2CT SSW2CA
inline PSTR SST2A(PTSTR p) { return p; }
inline PTSTR SSA2T(PSTR p) { return p; }
inline PCSTR SST2CA(PCTSTR p) { return p; }
inline PCTSTR SSA2CT(PCSTR p) { return p; }
#endif // #ifdef UNICODE
#if defined(UNICODE)
// in these cases the default (TCHAR) is the same as OLECHAR
inline PCOLESTR SST2COLE(PCTSTR p) { return p; }
inline PCTSTR SSOLE2CT(PCOLESTR p) { return p; }
inline POLESTR SST2OLE(PTSTR p) { return p; }
inline PTSTR SSOLE2T(POLESTR p) { return p; }
#elif defined(OLE2ANSI)
// in these cases the default (TCHAR) is the same as OLECHAR
inline PCOLESTR SST2COLE(PCTSTR p) { return p; }
inline PCTSTR SSOLE2CT(PCOLESTR p) { return p; }
inline POLESTR SST2OLE(PTSTR p) { return p; }
inline PTSTR SSOLE2T(POLESTR p) { return p; }
#else
//CharNextW doesn't work on Win95 so we use this
#define SST2COLE(pa) SSA2CW((pa))
#define SST2OLE(pa) SSA2W((pa))
#define SSOLE2CT(po) SSW2CA((po))
#define SSOLE2T(po) SSW2A((po))
#endif
#ifdef OLE2ANSI
#define SSW2OLE SSW2A
#define SSOLE2W SSA2W
#define SSW2COLE SSW2CA
#define SSOLE2CW SSA2CW
inline POLESTR SSA2OLE(PSTR p) { return p; }
inline PSTR SSOLE2A(POLESTR p) { return p; }
inline PCOLESTR SSA2COLE(PCSTR p) { return p; }
inline PCSTR SSOLE2CA(PCOLESTR p){ return p; }
#else
#define SSA2OLE SSA2W
#define SSOLE2A SSW2A
#define SSA2COLE SSA2CW
#define SSOLE2CA SSW2CA
inline POLESTR SSW2OLE(PWSTR p) { return p; }
inline PWSTR SSOLE2W(POLESTR p) { return p; }
inline PCOLESTR SSW2COLE(PCWSTR p) { return p; }
inline PCWSTR SSOLE2CW(PCOLESTR p){ return p; }
#endif
// Above we've defined macros that look like MS' but all have
// an 'SS' prefix. Now we need the real macros. We'll either
// get them from the macros above or from MFC/ATL. If
// SS_NO_CONVERSION is #defined, we'll forgo them
#ifndef SS_NO_CONVERSION
#if defined (USES_CONVERSION)
#define _NO_STDCONVERSION // just to be consistent
#else
#ifdef _MFC_VER
#include <afxconv.h>
#define _NO_STDCONVERSION // just to be consistent
#else
#define USES_CONVERSION SSCVT
#define A2CW SSA2CW
#define W2CA SSW2CA
#define T2A SST2A
#define A2T SSA2T
#define T2W SST2W
#define W2T SSW2T
#define T2CA SST2CA
#define A2CT SSA2CT
#define T2CW SST2CW
#define W2CT SSW2CT
#define ocslen sslen
#define ocscpy sscpy
#define T2COLE SST2COLE
#define OLE2CT SSOLE2CT
#define T2OLE SST2COLE
#define OLE2T SSOLE2CT
#define A2OLE SSA2OLE
#define OLE2A SSOLE2A
#define W2OLE SSW2OLE
#define OLE2W SSOLE2W
#define A2COLE SSA2COLE
#define OLE2CA SSOLE2CA
#define W2COLE SSW2COLE
#define OLE2CW SSOLE2CW
#endif // #ifdef _MFC_VER
#endif // #ifndef USES_CONVERSION
#endif // #ifndef SS_NO_CONVERSION
// Define ostring - generic name for std::basic_string<OLECHAR>
#if !defined(ostring) && !defined(OSTRING_DEFINED)
typedef std::basic_string<OLECHAR> ostring;
#define OSTRING_DEFINED
#endif
#endif // #ifndef SS_ANSI
// StdCodeCvt when there's no conversion to be done
inline PSTR StdCodeCvt(PSTR pDst, PCSTR pSrc, int nChars)
{
if ( nChars > 0 )
{
pDst[0] = '\0';
std::basic_string<char>::traits_type::copy(pDst, pSrc, nChars);
// std::char_traits<char>::copy(pDst, pSrc, nChars);
if ( nChars > 0 )
pDst[nChars] = '\0';
}
return pDst;
}
inline PSTR StdCodeCvt(PSTR pDst, PCUSTR pSrc, int nChars)
{
return StdCodeCvt(pDst, (PCSTR)pSrc, nChars);
}
inline PUSTR StdCodeCvt(PUSTR pDst, PCSTR pSrc, int nChars)
{
return (PUSTR)StdCodeCvt((PSTR)pDst, pSrc, nChars);
}
inline PWSTR StdCodeCvt(PWSTR pDst, PCWSTR pSrc, int nChars)
{
if ( nChars > 0 )
{
pDst[0] = '\0';
std::basic_string<wchar_t>::traits_type::copy(pDst, pSrc, nChars);
// std::char_traits<wchar_t>::copy(pDst, pSrc, nChars);
if ( nChars > 0 )
pDst[nChars] = '\0';
}
return pDst;
}
// Define tstring -- generic name for std::basic_string<TCHAR>
#if !defined(tstring) && !defined(TSTRING_DEFINED)
typedef std::basic_string<TCHAR> tstring;
#define TSTRING_DEFINED
#endif
// a very shorthand way of applying the fix for KB problem Q172398
// (basic_string assignment bug)
#if defined ( _MSC_VER ) && ( _MSC_VER < 1200 )
#define Q172398(x) (x).erase()
#else
#define Q172398(x)
#endif
// =============================================================================
// INLINE FUNCTIONS ON WHICH CSTDSTRING RELIES
//
// Usually for generic text mapping, we rely on preprocessor macro definitions
// to map to string functions. However the CStdStr<> template cannot use
// macro-based generic text mappings because its character types do not get
// resolved until template processing which comes AFTER macro processing. In
// other words, UNICODE is of little help to us in the CStdStr template
//
// Therefore, to keep the CStdStr declaration simple, we have these inline
// functions. The template calls them often. Since they are inline (and NOT
// exported when this is built as a DLL), they will probably be resolved away
// to nothing.
//
// Without these functions, the CStdStr<> template would probably have to broken
// out into two, almost identical classes. Either that or it would be a huge,
// convoluted mess, with tons of "if" statements all over the place checking the
// size of template parameter CT.
//
// In several cases, you will see two versions of each function. One version is
// the more portable, standard way of doing things, while the other is the
// non-standard, but often significantly faster Visual C++ way.
// =============================================================================
// If they defined SS_NO_REFCOUNT, then we must convert all assignments
#ifdef SS_NO_REFCOUNT
#define SSREF(x) (x).c_str()
#else
#define SSREF(x) (x)
#endif
// -----------------------------------------------------------------------------
// sslen: strlen/wcslen wrappers
// -----------------------------------------------------------------------------
template<typename CT> inline int sslen(const CT* pT)
{
return 0 == pT ? 0 : std::basic_string<CT>::traits_type::length(pT);
// return 0 == pT ? 0 : std::char_traits<CT>::length(pT);
}
inline SS_NOTHROW int sslen(const std::string& s)
{
return s.length();
}
inline SS_NOTHROW int sslen(const std::wstring& s)
{
return s.length();
}
// -----------------------------------------------------------------------------
// sstolower/sstoupper -- convert characters to upper/lower case
// -----------------------------------------------------------------------------
#if !defined(SS_ANSI) || defined(SS_NO_LOCALE)
inline char sstoupper(char ch) { return (char)::toupper(ch); }
inline wchar_t sstoupper(wchar_t ch){ return (wchar_t)::towupper(ch); }
inline char sstolower(char ch) { return (char)::tolower(ch); }
inline wchar_t sstolower(wchar_t ch){ return (wchar_t)::tolower(ch); }
#else
template<typename CT>
inline CT sstolower(const CT& t, const std::locale& lo =std::locale())
{
return std::tolower<CT>(t, loc);
}
template<typename CT>
inline CT sstoupper(const CT& t, const std::locale& lo =std::locale())
{
return std::toupper<CT>(t, loc);
}
#endif
// -----------------------------------------------------------------------------
// ssasn: assignment functions -- assign "sSrc" to "sDst"
// -----------------------------------------------------------------------------
typedef std::string::size_type SS_SIZETYPE; // just for shorthand, really
typedef std::string::pointer SS_PTRTYPE;
typedef std::wstring::size_type SW_SIZETYPE;
typedef std::wstring::pointer SW_PTRTYPE;
inline void ssasn(std::string& sDst, const std::string& sSrc)
{
if ( sDst.c_str() != sSrc.c_str() )
{
sDst.erase();
sDst.assign(SSREF(sSrc));
}
}
inline void ssasn(std::string& sDst, PCSTR pA)
{
// Watch out for NULLs, as always.
if ( 0 == pA )
{
sDst.erase();
}
// If pA actually points to part of sDst, we must NOT erase(), but
// rather take a substring
else if ( pA >= sDst.c_str() && pA <= sDst.c_str() + sDst.size() )
{
sDst =sDst.substr(static_cast<SS_SIZETYPE>(pA-sDst.c_str()));
}
// Otherwise (most cases) apply the assignment bug fix, if applicable
// and do the assignment
else
{
Q172398(sDst);
sDst.assign(pA);
}
}
inline void ssasn(std::string& sDst, const std::wstring& sSrc)
{
int nLen = sSrc.size();
sDst.resize(nLen * 2 + 1);
StdCodeCvt(const_cast<SS_PTRTYPE>(sDst.data()), sSrc.c_str(), nLen);
sDst.resize(nLen);
}
inline void ssasn(std::string& sDst, PCWSTR pW)
{
int nLen = sslen(pW);
sDst.resize(nLen * 2 + 1);
StdCodeCvt(const_cast<SS_PTRTYPE>(sDst.data()), pW, nLen);
sDst.resize(nLen);
}
inline void ssasn(std::string& sDst, const int nNull)
{
UNUSED(nNull);
ASSERT(nNull==0);
sDst.assign("");
}
inline void ssasn(std::wstring& sDst, const std::wstring& sSrc)
{
if ( sDst.c_str() != sSrc.c_str() )
{
sDst.erase();
sDst.assign(SSREF(sSrc));
}
}
inline void ssasn(std::wstring& sDst, PCWSTR pW)
{
// Watch out for NULLs, as always.
if ( 0 == pW )
{
sDst.erase();
}
// If pW actually points to part of sDst, we must NOT erase(), but
// rather take a substring
else if ( pW >= sDst.c_str() && pW <= sDst.c_str() + sDst.size() )
{
sDst = sDst.substr(static_cast<SW_SIZETYPE>(pW-sDst.c_str()));
}
// Otherwise (most cases) apply the assignment bug fix, if applicable
// and do the assignment
else
{
Q172398(sDst);
sDst.assign(pW);
}
}
#undef StrSizeType
inline void ssasn(std::wstring& sDst, const std::string& sSrc)
{
int nLen = sSrc.size();
sDst.resize(nLen+1);
StdCodeCvt(const_cast<SW_PTRTYPE>(sDst.data()), sSrc.c_str(), nLen+1);
sDst.resize(wcslen(sDst.data()));
}
inline void ssasn(std::wstring& sDst, PCSTR pA)
{
int nLen = sslen(pA);
sDst.resize(nLen+1);
StdCodeCvt(const_cast<SW_PTRTYPE>(sDst.data()), pA, nLen+1);
sDst.resize(wcslen(sDst.data()));
}
inline void ssasn(std::wstring& sDst, const int nNull)
{
UNUSED(nNull);
ASSERT(nNull==0);
sDst.assign(L"");
}
// -----------------------------------------------------------------------------
// ssadd: string object concatenation -- add second argument to first
// -----------------------------------------------------------------------------
inline void ssadd(std::string& sDst, const std::wstring& sSrc)
{
int nSrcLen = sSrc.size();
int nDstLen = sDst.size();
int nEndLen = nSrcLen + nDstLen;
sDst.resize(nEndLen + 1);
StdCodeCvt(const_cast<SS_PTRTYPE>(sDst.data()+nDstLen), sSrc.c_str(), nSrcLen);
sDst.resize(nEndLen);
}
inline void ssadd(std::string& sDst, const std::string& sSrc)
{
if ( &sDst == &sSrc )
sDst.reserve(2*sDst.size());
sDst.append(sSrc.c_str());
}
inline void ssadd(std::string& sDst, PCWSTR pW)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -