?? tstring.h
字號:
#ifndef __TString_H__
#define __TString_H__
#include "TypeDef.h"
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
/////////////////////////////////////////////////////////////////////////////
// Strings
extern char TafxChNil;
struct TStringData
{
long nRefs; // reference count
int nDataLength; // length of data (including terminator)
int nAllocLength; // length of allocation
// TCHAR data[nAllocLength]
char* data() // char* to managed data
{ return (char*)(this+1); }
};
class TString
{
public:
// Constructors
// constructs empty TString
TString();
// copy constructor
TString( const TString& stringSrc );
// from a single character
TString( char ch, int nRepeat = 1 );
// from an ANSI string (converts to TCHAR)
TString( TLPCSTR lpsz);
// from a UNICODE string (converts to TCHAR)
////TString(LPCWSTR lpsz);
// subset of characters from an ANSI string (converts to TCHAR)
TString( TLPCSTR lpch, int nLength );
// subset of characters from a UNICODE string (converts to TCHAR)
////CString(LPCWSTR lpch, int nLength);
// from unsigned characters
TString( const unsigned char * psz );
// Attributes & Operations
// get data length
int GetLength() const;
// TRUE if zero length
TBOOL IsEmpty() const;
// clear contents to empty
void Empty();
// return single character at zero-based index
char GetAt( int nIndex ) const;
// return single character at zero-based index
char operator[]( int nIndex ) const;
// set a single character at zero-based index
void SetAt(int nIndex, char ch);
// return pointer to const string
operator TLPCSTR() const;
// overloaded assignment
// ref-counted copy from another CString
const TString& operator=(const TString& stringSrc);
// set string content to single character
const TString& operator=(char ch);
// copy string content from ANSI string (converts to TCHAR)
const TString& operator=(TLPCSTR lpsz);
// copy string content from unsigned chars
const TString& operator=(const unsigned char* psz);
// string concatenation
// concatenate from another CString
const TString& operator+=(const TString& string);
// concatenate a single character
const TString& operator+=(char ch);
// concatenate a UNICODE character after converting it to TCHAR
const TString& operator+=(TLPCTSTR lpsz);
friend TString operator+(const TString& string1,
const TString& string2);
friend TString operator+(const TString& string, char ch);
friend TString operator+(char ch, const TString& string);
friend TString operator+(const TString& string, TLPCTSTR lpsz);
friend TString operator+(TLPCTSTR lpsz, const TString& string);
// string comparison
// straight character comparison
int Compare(TLPCTSTR lpsz) const;
// compare ignoring case
int CompareNoCase(TLPCTSTR lpsz) const;
// NLS aware comparison, case sensitive
int Collate(TLPCTSTR lpsz) const;
// NLS aware comparison, case insensitive
int CollateNoCase(TLPCTSTR lpsz) const;
// simple sub-string extraction
// return nCount characters starting at zero-based nFirst
TString Mid(int nFirst, int nCount) const;
// return all characters starting at zero-based nFirst
TString Mid(int nFirst) const;
// return first nCount characters in string
TString Left(int nCount) const;
// return nCount characters from end of string
TString Right(int nCount) const;
// characters from beginning that are also in passed string
TString SpanIncluding(TLPCTSTR lpszCharSet) const;
// characters from beginning that are not also in passed string
TString SpanExcluding(TLPCTSTR lpszCharSet) const;
// upper/lower/reverse conversion
// NLS aware conversion to uppercase
void MakeUpper();
// NLS aware conversion to lowercase
void MakeLower();
// reverse string right-to-left
void MakeReverse();
// trimming whitespace (either side)
// remove whitespace starting from right edge
void TrimRight();
// remove whitespace starting from left side
void TrimLeft();
// trimming anything (either side)
// remove continuous occurrences of chTarget starting from right
void TrimRight(char chTarget);
// remove continuous occcurrences of characters in passed string,
// starting from right
void TrimRight(TLPCTSTR lpszTargets);
// remove continuous occurrences of chTarget starting from left
void TrimLeft(char chTarget);
// remove continuous occcurrences of characters in
// passed string, starting from left
void TrimLeft(TLPCTSTR lpszTargets);
// advanced manipulation
// replace occurrences of chOld with chNew
int Replace(char chOld, char chNew);
// replace occurrences of substring lpszOld with lpszNew;
// empty lpszNew removes instances of lpszOld
int Replace(TLPCTSTR lpszOld, TLPCTSTR lpszNew);
// remove occurrences of chRemove
int Remove(char chRemove);
// insert character at zero-based index; concatenates
// if index is past end of string
int Insert(int nIndex, char ch);
// insert substring at zero-based index; concatenates
// if index is past end of string
int Insert(int nIndex, TLPCTSTR pstr);
// delete nCount characters starting at zero-based index
int Delete(int nIndex, int nCount = 1);
// searching
// find character starting at left, -1 if not found
int Find(char ch) const;
// find character starting at right
int ReverseFind(char ch) const;
// find character starting at zero-based index and going right
int Find(char ch, int nStart) const;
// find first instance of any character in passed string
int FindOneOf(TLPCTSTR lpszCharSet) const;
// find first instance of substring
int Find(TLPCTSTR lpszSub) const;
// find first instance of substring starting at zero-based index
int Find(TLPCTSTR lpszSub, int nStart) const;
// simple formatting
// printf-like formatting using passed string
void Format(TLPCSTR lpszFormat, ...){};
// printf-like formatting using referenced string resource
void Format(TUINT nFormatID, ...){};
// printf-like formatting using variable arguments parameter
void FormatV(TLPCSTR lpszFormat, va_list argList){};
// formatting for localization (uses FormatMessage API)
// format using FormatMessage API on passed string
void FormatMessage(TLPCSTR lpszFormat, ...){};
// format using FormatMessage API on referenced string resource
void FormatMessage(TUINT nFormatID, ...){};
// load from string resource
TBOOL LoadString(TUINT nID){ return true; };
// Access to string implementation buffer as "C" character array
// get pointer to modifiable buffer at least as long as nMinBufLength
TLPSTR GetBuffer(int nMinBufLength){ return TNULL; };
// release buffer, setting length to nNewLength (or to first nul if -1)
void ReleaseBuffer(int nNewLength = -1){};
// get pointer to modifiable buffer exactly as long as nNewLength
TLPSTR GetBufferSetLength(int nNewLength){ return TNULL; };
// release memory allocated to but unused by string
void FreeExtra(){};
// Use LockBuffer/UnlockBuffer to turn refcounting off
// turn refcounting back on
TLPSTR LockBuffer(){ return TNULL; };
// turn refcounting off
void UnlockBuffer(){};
// Implementation
public:
~TString(){};
int GetAllocLength() const{ return 0; };
protected:
TLPSTR m_pchData; // pointer to ref counted string data
// implementation helpers
TStringData* GetData() const{ return TNULL; };
void Init();
void AllocCopy(TString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const{};
void AllocBuffer(int nLen){};
void AssignCopy(int nSrcLen, TLPCSTR lpszSrcData){};
void ConcatCopy(int nSrc1Len, TLPCSTR lpszSrc1Data, int nSrc2Len, TLPCSTR lpszSrc2Data){};
void ConcatInPlace(int nSrcLen, TLPCSTR lpszSrcData){};
void CopyBeforeWrite(){};
void AllocBeforeWrite(int nLen){};
void Release(){};
static void Release(TStringData* pData){};
static int SafeStrlen(TLPCSTR lpsz);
static void FreeData(TStringData* pData){};
}; //TString
extern TLPCTSTR T_afxPchNil;
#define TafxEmptyString ((TString&)*(TString*)&T_afxPchNil)//TAfxGetEmptyString()
//#define TafxEmptyString TAfxGetEmptyString()
inline void TString::Init()
{
m_pchData = TafxEmptyString.m_pchData;
};
inline int TString::SafeStrlen(TLPCTSTR lpsz)
{ return (lpsz == NULL) ? 0 : strlen(lpsz);} //lstrlen used for unicode,strlen used for ansi
// from unsigned characters
inline TString::TString( const unsigned char * lpsz )
{ Init(); *this = (TLPCSTR)lpsz; }
// get data length
inline int TString::GetLength() const
{ return GetData()->nDataLength; }
// TRUE if zero length
inline TBOOL TString::IsEmpty() const
{ return GetData()->nDataLength == 0; }
// return single character at zero-based index
inline char TString::GetAt( int nIndex ) const
{
assert(nIndex >= 0);
assert(nIndex < GetData()->nDataLength);
return m_pchData[nIndex];
}
// return single character at zero-based index
inline char TString::operator[]( int nIndex ) const
{
// same as GetAt
assert(nIndex >= 0);
assert(nIndex < GetData()->nDataLength);
return m_pchData[nIndex];
}
// return pointer to const string
inline TString::operator TLPCSTR() const
{ return m_pchData; }
// copy string content from unsigned chars
inline const TString& TString::operator=(const unsigned char* lpsz)
{ *this = (TLPCSTR)lpsz; return *this; }
// string comparison=================================
// straight character comparison
inline int TString::Compare(TLPCTSTR lpsz) const
{
//ASSERT(AfxIsValidString(lpsz));
//return _tcscmp(m_pchData, lpsz);
return strcmp(m_pchData, lpsz);
} // MBCS/Unicode aware
// compare ignoring case
inline int TString::CompareNoCase(TLPCTSTR lpsz) const //等待改造???
{
//ASSERT(AfxIsValidString(lpsz));
//return _tcsicmp(m_pchData, lpsz);
return 0;//stricmp(m_pchData, lpsz); //以大小寫不敏感方式比較兩個串
} // MBCS/Unicode aware
// CString::Collate is often slower than Compare but is MBSC/Unicode
// aware as well as locale-sensitive with respect to sort order.
// NLS aware comparison, case sensitive
inline int TString::Collate(TLPCTSTR lpsz) const //等待改造???
{
//ASSERT(AfxIsValidString(lpsz));
//return _tcscoll(m_pchData, lpsz);
return 0;
} // locale sensitive
// NLS aware comparison, case insensitive
inline int TString::CollateNoCase(TLPCTSTR lpsz) const //等待改造???
{
//ASSERT(AfxIsValidString(lpsz));
//return _tcsicoll(m_pchData, lpsz);
return 0;
} // locale sensitive
#endif //__TString_H__
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -