📄 cstring.hpp
字号:
/** * 字符串操作对象 Version 1.0 * * Create: 2004-07-24 Modify: 2004-07-24 Complete: 2004-07-24 */// Notes: 本类从MFC函数库复制#ifndef __CSTRING__#define __CSTRING__#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <memory.h>#include <ctype.h>#ifndef LPCSTR #define LPCSTR const char* #define LPTSTR char*#endif#ifndef BYTE #define BYTE unsigned char#endif#ifndef BOOL #define BOOL int#endif#ifndef TRUE #define TRUE 1 #define FALSE 0#endif#include "safebuffer.hpp" // 2004-07-24 aps-lny SafeBuffer class is moved to safebuffer.cpp// 字符串存储类 struct CStringData{ long nRefs; // reference count int nDataLength; // length of data (including terminator) int nAllocLength; // length of allocation char* data() // TCHAR* to managed data { return (char*)(this+1); }};class CString {public:// Constructors // constructs empty CString CString(); // copy constructor CString(const CString& CStringSrc); // from a single character CString(char ch, int nRepeat = 1); // from an ANSI CString (converts to char) CString(LPCSTR lpsz); // subset of characters from an ANSI CString (converts to char) CString(LPCSTR lpch, int nLength); // from unsigned characters CString(const unsigned char* psz);// Attributes & Operations // get data length int GetLength() const { return GetData()->nDataLength; } // TRUE if zero length BOOL IsEmpty() const { return GetData()->nDataLength == 0; } // clear contents to empty void Empty(); // type convert operator LPCSTR() const { return m_pchData; } // overloaded assignment // ref-counted copy from another CString const CString& operator=(const CString& CStringSrc); // set CString content to single character const CString& operator=(char ch); // copy CString content from ANSI CString (converts to char) const CString& operator=(LPCSTR lpsz); // copy CString content from unsigned chars const CString& operator=(const unsigned char* psz); // CString concatenation // concatenate from another CString const CString& operator+=(const CString& CString); // concatenate a single character const CString& operator+=(char ch); // concatenate a UNICODE character after converting it to char const CString& operator+=(LPCSTR lpsz); friend CString operator+(const CString& CString1, const CString& CString2); friend CString operator+(const CString& CString, char ch); friend CString operator+(char ch, const CString& CString); friend CString operator+(const CString& CString, LPCSTR lpsz); friend CString operator+(LPCSTR lpsz, const CString& CString); // CString comparison // straight character comparison int Compare(LPCSTR lpsz) const { return strcmp(m_pchData, lpsz); } // simple sub-CString extraction // return nCount characters starting at zero-based nFirst CString Mid(int nFirst, int nCount) const; // return all characters starting at zero-based nFirst CString Mid(int nFirst) const; // return first nCount characters in CString CString Left(int nCount) const; // return nCount characters from end of CString CString Right(int nCount) const; // upper/lower/reverse conversion // NLS aware conversion to uppercase void MakeUpper(); // NLS aware conversion to lowercase void MakeLower(); // trimming whitespace (either side) // remove whitespace starting from right edge void TrimRight(); // remove whitespace starting from left side void TrimLeft(); void Trim(); // advanced manipulation // replace occurrences of chOld with chNew int Replace(LPCSTR lpszOld, LPCSTR lpszNew); // remove occurrences of chRemove int Remove(char chRemove); // insert character at zero-based index; concatenates // if index is past end of CString 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 CString int FindOneOf(LPCSTR lpszCharSet) const; // find first instance of subCString int Find(LPCSTR lpszSub) const; // find first instance of subCString starting at zero-based index int Find(LPCSTR lpszSub, int nStart) const; // simple formatting // printf-like formatting using passed CString void Format(LPCSTR lpszFormat, ...); LPTSTR GetBuffer(int nMinBufLength); // 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 LPTSTR GetBufferSetLength(int nNewLength); // release memory allocated to but unused by string void FreeExtra(); // Use LockBuffer/UnlockBuffer to turn refcounting off // turn refcounting back on LPTSTR LockBuffer(); // turn refcounting off void UnlockBuffer();// Implementationpublic: ~CString(); int GetAllocLength() const;protected: LPTSTR m_pchData; // pointer to ref counted string data // implementation helpers void Init(); void AllocCopy(CString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const; void AllocBuffer(int nLen); void AssignCopy(int nSrcLen, LPCSTR lpszSrcData); void ConcatCopy(int nSrc1Len, LPCSTR lpszSrc1Data, int nSrc2Len, LPCSTR lpszSrc2Data); void ConcatInPlace(int nSrcLen, LPCSTR lpszSrcData); void CopyBeforeWrite(); void AllocBeforeWrite(int nLen); void Release(); static void Release(CStringData* pData); static int SafeStrlen(LPCSTR lpsz) {return (lpsz == NULL) ? 0 : strlen(lpsz); } static void FreeData(CStringData* pData); CStringData* GetData() const { return ((CStringData*)m_pchData)-1; }};inline BOOL operator==(const CString& s1, const CString& s2){ return s1.Compare(s2) == 0; }inline BOOL operator<=(const CString& s1, const CString& s2){ return s1.Compare(s2) <= 0; }inline BOOL operator<(const CString& s1, const CString& s2){ return s1.Compare(s2) < 0; }inline BOOL operator>=(const CString& s1, const CString& s2){ return s1.Compare(s2) >= 0; }inline BOOL operator>(const CString& s1, const CString& s2){ return s1.Compare(s2) > 0; }inline BOOL operator==(const CString& s1, LPCSTR s2){ return s1.Compare(s2) == 0; }inline BOOL operator==(LPCSTR s1, const CString& s2){ return s2.Compare(s1) == 0; }inline BOOL operator!=(const CString& s1, const CString& s2){ return s1.Compare(s2) != 0; }inline BOOL operator!=(const CString& s1, LPCSTR s2) { return s1.Compare(s2) != 0; }inline BOOL operator!=(LPCSTR s1, const CString& s2){ return s2.Compare(s1) != 0; }inlineCString Space(int nLength) { return CString(' ', nLength); }class NameList {public: NameList(LPCSTR pSeparator = ";"){m_strSeparator = pSeparator; } NameList(NameList& names); NameList& operator=(NameList& names); NameList& operator=(CString& s); void Put(LPCSTR pItem) { m_strList += pItem; m_strList += m_strSeparator; } CString Get(); int GetItemCount(); BOOL IsEmpty() { return m_strList.IsEmpty(); } CString ToString();private: CString m_strList; CString m_strSeparator;};inlineNameList::NameList(NameList& names){ m_strList = names.m_strList; m_strSeparator = names.m_strSeparator;}inlineNameList& NameList::operator=(NameList& names) { m_strList = names.m_strList; m_strSeparator = names.m_strSeparator; return *this; }inlineCString NameList::Get(){ int iPos = m_strList.Find(m_strSeparator); if (iPos == -1) return CString(); CString sRet = m_strList.Left(iPos); m_strList = m_strList.Mid(iPos + strlen(m_strSeparator)); return sRet;}inline CString NameList::ToString(){ return m_strList;}inlineNameList& NameList::operator=(CString& s){ m_strList = s; return *this;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -