DUILIB拆解 CStdString
StdString.h
#ifndef __UIBASE_H__ #define __UIBASE_H__ #pragma once class CStdString { public: enum { MAX_LOCAL_STRING_LEN = 63 }; CStdString(); CStdString(const TCHAR ch); CStdString(const CStdString& src); CStdString(LPCTSTR lpsz, int nLen = -1); ~CStdString(); void Empty(); int GetLength() const; bool IsEmpty() const; TCHAR GetAt(int nIndex) const; void Append(LPCTSTR pstr); void Assign(LPCTSTR pstr, int nLength = -1); LPCTSTR GetData(); void SetAt(int nIndex, TCHAR ch); operator LPCTSTR() const; TCHAR operator[] (int nIndex) const; const CStdString& operator=(const CStdString& src); const CStdString& operator=(const TCHAR ch); const CStdString& operator=(LPCTSTR pstr); #ifdef _UNICODE const CStdString& CStdString::operator=(LPCSTR lpStr); const CStdString& CStdString::operator+=(LPCSTR lpStr); #else const CStdString& CStdString::operator=(LPCWSTR lpwStr); const CStdString& CStdString::operator+=(LPCWSTR lpwStr); #endif CStdString operator+(const CStdString& src) const; CStdString operator+(LPCTSTR pstr) const; const CStdString& operator+=(const CStdString& src); const CStdString& operator+=(LPCTSTR pstr); const CStdString& operator+=(const TCHAR ch); bool operator == (LPCTSTR str) const; bool operator != (LPCTSTR str) const; bool operator <= (LPCTSTR str) const; bool operator < (LPCTSTR str) const; bool operator >= (LPCTSTR str) const; bool operator > (LPCTSTR str) const; int Compare(LPCTSTR pstr) const; int CompareNoCase(LPCTSTR pstr) const; void MakeUpper(); void MakeLower(); CStdString Left(int nLength) const; CStdString Mid(int iPos, int nLength = -1) const; CStdString Right(int nLength) const; int Find(TCHAR ch, int iPos = 0) const; int Find(LPCTSTR pstr, int iPos = 0) const; int ReverseFind(TCHAR ch) const; int Replace(LPCTSTR pstrFrom, LPCTSTR pstrTo); int __cdecl Format(LPCTSTR pstrFormat, ...); int __cdecl SmallFormat(LPCTSTR pstrFormat, ...); protected: LPTSTR m_pstr; TCHAR m_szBuffer[MAX_LOCAL_STRING_LEN + 1]; }; #endif // __UIBASE_H__
StdString.cpp
#include "StdAfx.h" #include "StdString.h" #ifdef _DEBUG #include <shlwapi.h> #pragma comment(lib, "shlwapi.lib") #endif CStdString::CStdString() : m_pstr(m_szBuffer) { m_szBuffer[0] = \'\0\'; } CStdString::CStdString(const TCHAR ch) : m_pstr(m_szBuffer) { m_szBuffer[0] = ch; m_szBuffer[1] = \'\0\'; } CStdString::CStdString(LPCTSTR lpsz, int nLen) : m_pstr(m_szBuffer) { ASSERT(!::IsBadStringPtr(lpsz,-1) || lpsz==NULL); m_szBuffer[0] = \'\0\'; Assign(lpsz, nLen); } CStdString::CStdString(const CStdString& src) : m_pstr(m_szBuffer) { m_szBuffer[0] = \'\0\'; Assign(src.m_pstr); } CStdString::~CStdString() { if( m_pstr != m_szBuffer ) free(m_pstr); } int CStdString::GetLength() const { return (int) _tcslen(m_pstr); } CStdString::operator LPCTSTR() const { return m_pstr; } void CStdString::Append(LPCTSTR pstr) { int nNewLength = GetLength() + (int) _tcslen(pstr); if( nNewLength >= MAX_LOCAL_STRING_LEN ) { if( m_pstr == m_szBuffer ) { m_pstr = static_cast<LPTSTR>(malloc((nNewLength + 1) * sizeof(TCHAR))); _tcscpy(m_pstr, m_szBuffer); _tcscat(m_pstr, pstr); } else { m_pstr = static_cast<LPTSTR>(realloc(m_pstr, (nNewLength + 1) * sizeof(TCHAR))); _tcscat(m_pstr, pstr); } } else { if( m_pstr != m_szBuffer ) { free(m_pstr); m_pstr = m_szBuffer; } _tcscat(m_szBuffer, pstr); } } void CStdString::Assign(LPCTSTR pstr, int cchMax) { if( pstr == NULL ) pstr = _T(""); cchMax = (cchMax < 0 ? (int) _tcslen(pstr) : cchMax); if( cchMax < MAX_LOCAL_STRING_LEN ) { if( m_pstr != m_szBuffer ) { free(m_pstr); m_pstr = m_szBuffer; } } else if( cchMax > GetLength() || m_pstr == m_szBuffer ) { if( m_pstr == m_szBuffer ) m_pstr = NULL; m_pstr = static_cast<LPTSTR>(realloc(m_pstr, (cchMax + 1) * sizeof(TCHAR))); } _tcsncpy(m_pstr, pstr, cchMax); m_pstr[cchMax] = \'\0\'; } bool CStdString::IsEmpty() const { return m_pstr[0] == \'\0\'; } void CStdString::Empty() { if( m_pstr != m_szBuffer ) free(m_pstr); m_pstr = m_szBuffer; m_szBuffer[0] = \'\0\'; } LPCTSTR CStdString::GetData() { return m_pstr; } TCHAR CStdString::GetAt(int nIndex) const { return m_pstr[nIndex]; } TCHAR CStdString::operator[] (int nIndex) const { return m_pstr[nIndex]; } const CStdString& CStdString::operator=(const CStdString& src) { Assign(src); return *this; } const CStdString& CStdString::operator=(LPCTSTR lpStr) { ASSERT(!::IsBadStringPtr(lpStr,-1)); Assign(lpStr); return *this; } #ifdef _UNICODE const CStdString& CStdString::operator=(LPCSTR lpStr) { ASSERT(!::IsBadStringPtrA(lpStr,-1)); int cchStr = (int) strlen(lpStr) + 1; LPWSTR pwstr = (LPWSTR) _alloca(cchStr); if( pwstr != NULL ) ::MultiByteToWideChar(::GetACP(), 0, lpStr, -1, pwstr, cchStr) ; Assign(pwstr); return *this; } const CStdString& CStdString::operator+=(LPCSTR lpStr) { ASSERT(!::IsBadStringPtrA(lpStr,-1)); int cchStr = (int) strlen(lpStr) + 1; LPWSTR pwstr = (LPWSTR) _alloca(cchStr); if( pwstr != NULL ) ::MultiByteToWideChar(::GetACP(), 0, lpStr, -1, pwstr, cchStr) ; Append(pwstr); return *this; } #else const CStdString& CStdString::operator=(LPCWSTR lpwStr) { ASSERT(!::IsBadStringPtrW(lpwStr,-1)); int cchStr = ((int) wcslen(lpwStr) * 2) + 1; LPSTR pstr = (LPSTR) _alloca(cchStr); if( pstr != NULL ) ::WideCharToMultiByte(::GetACP(), 0, lpwStr, -1, pstr, cchStr, NULL, NULL); Assign(pstr); return *this; } const CStdString& CStdString::operator+=(LPCWSTR lpwStr) { ASSERT(!::IsBadStringPtrW(lpwStr,-1)); int cchStr = ((int) wcslen(lpwStr) * 2) + 1; LPSTR pstr = (LPSTR) _alloca(cchStr); if( pstr != NULL ) ::WideCharToMultiByte(::GetACP(), 0, lpwStr, -1, pstr, cchStr, NULL, NULL); Append(pstr); return *this; } #endif // _UNICODE const CStdString& CStdString::operator=(const TCHAR ch) { Empty(); m_szBuffer[0] = ch; m_szBuffer[1] = \'\0\'; return *this; } CStdString CStdString::operator+(const CStdString& src) const { CStdString sTemp = *this; sTemp.Append(src); return sTemp; } CStdString CStdString::operator+(LPCTSTR lpStr) const { ASSERT(!::IsBadStringPtr(lpStr,-1)); CStdString sTemp = *this; sTemp.Append(lpStr); return sTemp; } const CStdString& CStdString::operator+=(const CStdString& src) { Append(src); return *this; } const CStdString& CStdString::operator+=(LPCTSTR lpStr) { ASSERT(!::IsBadStringPtr(lpStr,-1)); Append(lpStr); return *this; } const CStdString& CStdString::operator+=(const TCHAR ch) { TCHAR str[] = { ch, \'\0\' }; Append(str); return *this; } bool CStdString::operator == (LPCTSTR str) const { return (Compare(str) == 0); }; bool CStdString::operator != (LPCTSTR str) const { return (Compare(str) != 0); }; bool CStdString::operator <= (LPCTSTR str) const { return (Compare(str) <= 0); }; bool CStdString::operator < (LPCTSTR str) const { return (Compare(str) < 0); }; bool CStdString::operator >= (LPCTSTR str) const { return (Compare(str) >= 0); }; bool CStdString::operator > (LPCTSTR str) const { return (Compare(str) > 0); }; void CStdString::SetAt(int nIndex, TCHAR ch) { ASSERT(nIndex>=0 && nIndex<GetLength()); m_pstr[nIndex] = ch; } int CStdString::Compare(LPCTSTR lpsz) const { return _tcscmp(m_pstr, lpsz); } int CStdString::CompareNoCase(LPCTSTR lpsz) const { return _tcsicmp(m_pstr, lpsz); } void CStdString::MakeUpper() { _tcsupr(m_pstr); } void CStdString::MakeLower() { _tcslwr(m_pstr); } CStdString CStdString::Left(int iLength) const { if( iLength < 0 ) iLength = 0; if( iLength > GetLength() ) iLength = GetLength(); return CStdString(m_pstr, iLength); } CStdString CStdString::Mid(int iPos, int iLength) const { if( iLength < 0 ) iLength = GetLength() - iPos; if( iPos + iLength > GetLength() ) iLength = GetLength() - iPos; if( iLength <= 0 ) return CStdString(); return CStdString(m_pstr + iPos, iLength); } CStdString CStdString::Right(int iLength) const { int iPos = GetLength() - iLength; if( iPos < 0 ) { iPos = 0; iLength = GetLength(); } return CStdString(m_pstr + iPos, iLength); } int CStdString::Find(TCHAR ch, int iPos /*= 0*/) const { ASSERT(iPos>=0 && iPos<=GetLength()); if( iPos != 0 && (iPos < 0 || iPos >= GetLength()) ) return -1; LPCTSTR p = _tcschr(m_pstr + iPos, ch); if( p == NULL ) return -1; return (int)(p - m_pstr); } int CStdString::Find(LPCTSTR pstrSub, int iPos /*= 0*/) const { ASSERT(!::IsBadStringPtr(pstrSub,-1)); ASSERT(iPos>=0 && iPos<=GetLength()); if( iPos != 0 && (iPos < 0 || iPos > GetLength()) ) return -1; LPCTSTR p = _tcsstr(m_pstr + iPos, pstrSub); if( p == NULL ) return -1; return (int)(p - m_pstr); } int CStdString::ReverseFind(TCHAR ch) const { LPCTSTR p = _tcsrchr(m_pstr, ch); if( p == NULL ) return -1; return (int)(p - m_pstr); } int CStdString::Replace(LPCTSTR pstrFrom, LPCTSTR pstrTo) { CStdString sTemp; int nCount = 0; int iPos = Find(pstrFrom); if( iPos < 0 ) return 0; int cchFrom = (int) _tcslen(pstrFrom); int cchTo = (int) _tcslen(pstrTo); while( iPos >= 0 ) { sTemp = Left(iPos); sTemp += pstrTo; sTemp += Mid(iPos + cchFrom); Assign(sTemp); iPos = Find(pstrFrom, iPos + cchTo); nCount++; } return nCount; } int CStdString::Format(LPCTSTR pstrFormat, ...) { CStdString sFormat = pstrFormat; // Do ordinary printf replacements // NOTE: Documented max-length of wvsprintf() is 1024 TCHAR szBuffer[1025] = { 0 }; va_list argList; va_start(argList, pstrFormat); int iRet = ::wvsprintf(szBuffer, sFormat, argList); va_end(argList); Assign(szBuffer); return iRet; } int CStdString::SmallFormat(LPCTSTR pstrFormat, ...) { CStdString sFormat = pstrFormat; TCHAR szBuffer[64] = { 0 }; va_list argList; va_start(argList, pstrFormat); int iRet = ::wvsprintf(szBuffer, sFormat, argList); va_end(argList); Assign(szBuffer); return iRet; }
void CTestStringDlg::OnBnClickedOk() { CStdString str; str = L"amtf"; ::MessageBox(0,str,0,0); }
版权声明:本文为chunyou128原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。