📄 stdstring.h
字号:
{
int nSrcLen = sslen(pW);
int nDstLen = sDst.size();
int nEndLen = nSrcLen + nDstLen;
sDst.resize(nEndLen + 1);
StdCodeCvt(const_cast<SS_PTRTYPE>(sDst.data()+nDstLen), pW, nSrcLen+1);
sDst.resize(nEndLen);
}
inline void ssadd(std::string& sDst, PCSTR pA)
{
if ( pA )
{
// If the string being added is our internal string or a part of our
// internal string, then we must NOT do any reallocation without
// first copying that string to another object (since we're using a
// direct pointer)
if ( pA >= sDst.c_str() && pA <= sDst.c_str()+sDst.length())
{
if ( sDst.capacity() <= sDst.size()+sslen(pA) )
sDst.append(std::string(pA));
else
sDst.append(pA);
}
else
{
sDst.append(pA);
}
}
}
inline void ssadd(std::wstring& sDst, const std::wstring& sSrc)
{
if ( &sDst == &sSrc )
sDst.reserve(2*sDst.size());
sDst.append(sSrc.c_str());
}
inline void ssadd(std::wstring& sDst, const std::string& sSrc)
{
int nSrcLen = sSrc.size();
int nDstLen = sDst.size();
int nEndLen = nSrcLen + nDstLen;
sDst.resize(nEndLen+1);
StdCodeCvt(const_cast<SW_PTRTYPE>(sDst.data()+nDstLen), sSrc.c_str(), nSrcLen+1);
sDst.resize(nEndLen);
}
inline void ssadd(std::wstring& sDst, PCSTR pA)
{
int nSrcLen = sslen(pA);
int nDstLen = sDst.size();
int nEndLen = nSrcLen + nDstLen;
sDst.resize(nEndLen + 1);
StdCodeCvt(const_cast<SW_PTRTYPE>(sDst.data()+nDstLen), pA, nSrcLen+1);
sDst.resize(nEndLen);
}
inline void ssadd(std::wstring& sDst, PCWSTR pW)
{
if ( pW )
{
// If the string being added is our internal string or a part of our
// internal string, then we must NOT do any reallocation without
// first copying that string to another object (since we're using a
// direct pointer)
if ( pW >= sDst.c_str() && pW <= sDst.c_str()+sDst.length())
{
if ( sDst.capacity() <= sDst.size()+sslen(pW) )
sDst.append(std::wstring(pW));
else
sDst.append(pW);
}
else
{
sDst.append(pW);
}
}
}
// -----------------------------------------------------------------------------
// ssicmp: comparison (case insensitive )
// -----------------------------------------------------------------------------
#if defined(SS_ANSI) || !defined(_MSC_VER)
#ifdef SS_NO_LOCALE
template<typename CT>
inline int ssicmp(const CT* pA1, const CT* pA2)
{
CT f;
CT l;
do
{
f = sstolower(*(pA1++));
l = sstolower(*(pA2++));
} while ( (f) && (f == l) );
return (int)(f - l);
}
#else
template<typename CT>
inline int ssicmp(const CT* pA1, const CT* pA2)
{
std::locale loc;
const std::ctype<CT>& ct = SS_USE_FACET(loc, std::ctype<CT>);
CT f;
CT l;
do
{
f = ct.tolower(*(pA1++));
l = ct.tolower(*(pA2++));
} while ( (f) && (f == l) );
return (int)(f - l);
}
#endif
#else
#ifdef _MBCS
inline long sscmp(PCSTR pA1, PCSTR pA2)
{
return _mbscmp((PCUSTR)pA1, (PCUSTR)pA2);
}
inline long ssicmp(PCSTR pA1, PCSTR pA2)
{
return _mbsicmp((PCUSTR)pA1, (PCUSTR)pA2);
}
#else
inline long sscmp(PCSTR pA1, PCSTR pA2)
{
return strcmp(pA1, pA2);
}
inline long ssicmp(PCSTR pA1, PCSTR pA2)
{
return _stricmp(pA1, pA2);
}
#endif
inline long sscmp(PCWSTR pW1, PCWSTR pW2)
{
return wcscmp(pW1, pW2);
}
inline long ssicmp(PCWSTR pW1, PCWSTR pW2)
{
return _wcsicmp(pW1, pW2);
}
#endif
// -----------------------------------------------------------------------------
// ssupr/sslwr: Uppercase/Lowercase conversion functions
// -----------------------------------------------------------------------------
#if defined(SS_ANSI) || !defined(_MSC_VER)
#ifdef SS_NO_LOCALE
template<typename CT>
inline void sslwr(CT* pT, size_t nLen)
{
for ( CT* p = pT; static_cast<size_t>(p - pT) < nLen; ++p)
*p = (CT)sstolower(*p);
}
template<typename CT>
inline void ssupr(CT* pT, size_t nLen)
{
for ( CT* p = pT; static_cast<size_t>(p - pT) < nLen; ++p)
*p = (CT)sstoupper(*p);
}
#else
template<typename CT>
inline void sslwr(CT* pT, size_t nLen)
{
SS_USE_FACET(std::locale(), std::ctype<CT>).tolower(pT, pT+nLen);
}
template<typename CT>
inline void ssupr(CT* pT, size_t nLen)
{
SS_USE_FACET(std::locale(), std::ctype<CT>).toupper(pT, pT+nLen);
}
#endif
#else // #else we must be on Win32
#ifdef _MBCS
inline void ssupr(PSTR pA, size_t /*nLen*/)
{
_mbsupr((PUSTR)pA);
}
inline void sslwr(PSTR pA, size_t /*nLen*/)
{
_mbslwr((PUSTR)pA);
}
#else
inline void ssupr(PSTR pA, size_t /*nLen*/)
{
_strupr(pA);
}
inline void sslwr(PSTR pA, size_t /*nLen*/)
{
_strlwr(pA);
}
#endif
inline void ssupr(PWSTR pW, size_t /*nLen*/)
{
_wcsupr(pW);
}
inline void sslwr(PWSTR pW, size_t /*nLen*/)
{
_wcslwr(pW);
}
#endif // #ifdef SS_ANSI
// -----------------------------------------------------------------------------
// vsprintf/vswprintf or _vsnprintf/_vsnwprintf equivalents. In standard
// builds we can't use _vsnprintf/_vsnwsprintf because they're MS extensions.
// -----------------------------------------------------------------------------
#if defined(SS_ANSI) || !defined(_MSC_VER)
// Borland's headers put some ANSI "C" functions in the 'std' namespace.
// Promote them to the global namespace so we can use them here.
#if defined(__BORLANDC__)
using std::vsprintf;
using std::vswprintf;
#endif
inline int ssvsprintf(PSTR pA, size_t /*nCount*/, PCSTR pFmtA, va_list vl)
{
return vsprintf(pA, pFmtA, vl);
}
inline int ssvsprintf(PWSTR pW, size_t nCount, PCWSTR pFmtW, va_list vl)
{
// JMO: Some distributions of the "C" have a version of vswprintf that
// takes 3 arguments (e.g. Microsoft, Borland, GNU). Others have a
// version which takes 4 arguments (an extra "count" argument in the
// second position. The best stab I can take at this so far is that if
// you are NOT running with MS, Borland, or GNU, then I'll assume you
// have the version that takes 4 arguments.
//
// I'm sure that these checks don't catch every platform correctly so if
// you get compiler errors on one of the lines immediately below, it's
// probably because your implemntation takes a different number of
// arguments. You can comment out the offending line (and use the
// alternate version) or you can figure out what compiler flag to check
// and add that preprocessor check in. Regardless, if you get an error
// on these lines, I'd sure like to hear from you about it.
// #if !defined(__MWERKS__) && !defined(__SUNPRO_CC_COMPAT) && !defined(__SUNPRO_CC)
#if !defined(_MSC_VER) && !defined (__BORLANDC__) && !defined(__GNUC__)
return vswprintf(pW, nCount, pFmtW, vl);
#else
nCount;
return vswprintf(pW, pFmtW, vl);
#endif
}
#else
inline int ssnprintf(PSTR pA, size_t nCount, PCSTR pFmtA, va_list vl)
{
return _vsnprintf(pA, nCount, pFmtA, vl);
}
inline int ssnprintf(PWSTR pW, size_t nCount, PCWSTR pFmtW, va_list vl)
{
return _vsnwprintf(pW, nCount, pFmtW, vl);
}
#endif
// -----------------------------------------------------------------------------
// ssload: Type safe, overloaded ::LoadString wrappers
// There is no equivalent of these in non-Win32-specific builds. However, I'm
// thinking that with the message facet, there might eventually be one
// -----------------------------------------------------------------------------
#if defined (SS_WIN32) && !defined(SS_ANSI)
inline int ssload(HMODULE hInst, UINT uId, PSTR pBuf, int nMax)
{
return ::LoadStringA(hInst, uId, pBuf, nMax);
}
inline int ssload(HMODULE hInst, UINT uId, PWSTR pBuf, int nMax)
{
return ::LoadStringW(hInst, uId, pBuf, nMax);
}
#endif
// -----------------------------------------------------------------------------
// sscoll/ssicoll: Collation wrappers
// Note -- with MSVC I have reversed the arguments order here because the
// functions appear to return the opposite of what they should
// -----------------------------------------------------------------------------
#if defined(SS_ANSI) || !defined(_MSC_VER)
#ifdef SS_NO_LOCALE
inline int sscoll(PCSTR sz1, int /*nLen1*/, PCSTR sz2, int /*nLen2*/)
{
return strcoll(sz2, sz1);
}
inline int sscoll(PCWSTR sz1, int /*nLen1*/, PCWSTR sz2, int /*nLen2*/)
{
return wcscoll(sz2, sz1);
}
template<typename CT>
inline int ssicoll(const CT* sz1, int nLen1, const CT* sz2, int nLen2)
{
std::basic_string<CT> str1(sz1, nLen1);
std::basic_string<CT> str2(sz2, nLen2);
// JMO: Note this is a complete hack. But so is SS_NO_LOCALE
sslwr((CT*)str1.data(), nLen1);
sslwr((CT*)str2.data(), nLen2);
return sscoll(str2.c_str(), str2.size(), str1.c_str(), str1.size());
}
#else
template <typename CT>
inline int sscoll(const CT* sz1, int nLen1, const CT* sz2, int nLen2)
{
const std::collate<CT>& coll =
SS_USE_FACET(std::locale(), std::collate<CT>);
return coll.compare(sz2, sz2+nLen2, sz1, sz1+nLen1);
}
template <typename CT>
inline int ssicoll(const CT* sz1, int nLen1, const CT* sz2, int nLen2)
{
const std::locale loc;
const std::collate<CT>& coll = SS_USE_FACET(loc, std::collate<CT>);
// Some implementations seem to have trouble using the collate<>
// facet typedefs so we'll just default to basic_string and hope
// that's what the collate facet uses (which it generally should)
// std::collate<CT>::string_type s1(sz1);
// std::collate<CT>::string_type s2(sz2);
std::basic_string<CT> s1(sz1);
std::basic_string<CT> s2(sz2);
sslwr(const_cast<CT*>(s1.c_str()), nLen1);
sslwr(const_cast<CT*>(s2.c_str()), nLen2);
return coll.compare(s2.c_str(), s2.c_str()+nLen2,
s1.c_str(), s1.c_str()+nLen1);
}
#endif
#else
#ifdef _MBCS
inline int sscoll(PCSTR sz1, int /*nLen1*/, PCSTR sz2, int /*nLen2*/)
{
return _mbscoll((PCUSTR)sz1, (PCUSTR)sz2);
}
inline int ssicoll(PCSTR sz1, int /*nLen1*/, PCSTR sz2, int /*nLen2*/)
{
return _mbsicoll((PCUSTR)sz1, (PCUSTR)sz2);
}
#else
inline int sscoll(PCSTR sz1, int /*nLen1*/, PCSTR sz2, int /*nLen2*/)
{
return strcoll(sz1, sz2);
}
inline int ssicoll(PCSTR sz1, int /*nLen1*/, PCSTR sz2, int /*nLen2*/)
{
return _stricoll(sz1, sz2);
}
#endif
inline int sscoll(PCWSTR sz1, int /*nLen1*/, PCWSTR sz2, int /*nLen2*/)
{
return wcscoll(sz1, sz2);
}
inline int ssicoll(PCWSTR sz1, int /*nLen1*/, PCWSTR sz2, int /*nLen2*/)
{
return _wcsicoll(sz1, sz2);
}
#endif
// -----------------------------------------------------------------------------
// ssfmtmsg: FormatMessage equivalents. Needed because I added a CString facade
// Again -- no equivalent of these on non-Win32 builds but their might one day
// be one if the message facet gets implemented
// -----------------------------------------------------------------------------
#if defined (SS_WIN32) && !defined(SS_ANSI)
inline DWORD ssfmtmsg(DWORD dwFlags, LPCVOID pSrc, DWORD dwMsgId,
DWORD dwLangId, PSTR pBuf, DWORD nSize,
va_list* vlArgs)
{
return FormatMessageA(dwFlags, pSrc, dwMsgId, dwLangId,
pBuf, nSize,vlArgs);
}
inline DWORD ssfmtmsg(DWORD dwFlags, LPCVOID pSrc, DWORD dwMsgId,
DWORD dwLangId, PWSTR pBuf, DWORD nSize,
va_list* vlArgs)
{
return FormatMessageW(dwFlags, pSrc, dwMsgId, dwLangId,
pBuf, nSize,vlArgs);
}
#endif
// FUNCTION: sscpy. Copies up to 'nMax' characters from pSrc to pDst.
// -----------------------------------------------------------------------------
// FUNCTION: sscpy
// inline int sscpy(PSTR pDst, PCSTR pSrc, int nMax=-1);
// inline int sscpy(PUSTR pDst, PCSTR pSrc, int nMax=-1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -