| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- #include <windows.h>
- #include <assert.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <tchar.h>
- #ifndef __JONES__STRING__
- #define __JONES__STRING__
- typedef const BYTE FAR * LPBTSTR;
- struct BStringData
- {
- long nRefs; //引用记数
- int nDataLength; //字符使用长度
- int nAllocLength; //分配长度
- BYTE* data() { return (BYTE*)(this+1); } //存放字符串的地方
- //this+1 相当与是BStringData[1];所以TCHAR* data()指的是BStringData[1]的地址
- };
- //基本和网络通讯的数据包差不多
- typedef struct tagAnsMarketData //统一的应答结构
- {
- WORD wStkNum; //数目
- BYTE iData[1]; //数据
- }ANS_MARKET_DATA,*PANS_MARKET_DATA;
- //下面是代码了
- class BString
- {
- public:
- //构造函数
- BString();
- BString(const BString& stringSrc);
- BString(BYTE ch, int nLength =1);
- BString(LPBTSTR lpsz); // BString(LPCSTR lpsz); ANSI下版本
- //BString(LPCWSTR lpsz);UNICODE下版本
- BString(LPBTSTR lpch, int nLength); //BString(LPCSTR lpch, int nLength);ANSI下版本
- //BString(LPCWSTR lpch, int nLength);//UNICODE下版本
- //BString(const BYTE* psz);
- ~BString();
- //BStringData的属性
- int GetLength() const; //得到字符长度
- int GetAllocLength() const; //得到分配的内存长度
- BOOL IsEmpty() const; //判断字符长度是否为0
- operator LPBTSTR() const; //类型转换
- void Empty(); //清空BStringData
- //操作符重载
- const BString& operator=(const BString& stringSrc);
- const BString& operator=(LPBTSTR lpsz);
- const BString& operator=(BYTE ch);
- const BString& operator+=(const BString& string);
- const BString& operator+=(BYTE ch);
- const BString& operator+=(LPBTSTR lpsz);
- BYTE operator[](int nIndex) const;
- friend BString operator+(const BString& string1,const BString& string2);
- friend BString operator+(const BString& string, BYTE ch);
- friend BString operator+(BYTE ch, const BString& string);
- friend BString operator+(const BString& string, LPBTSTR lpsz);
- friend BString operator+(LPBTSTR lpsz, const BString& string);
- //操作,脱离共享数据块
- int Delete(int nIndex, int nCount = 1);//删除从nIndex开始长度为nCount的数据
- int Insert(int nIndex, BYTE ch); //插入一个字符
- int Insert(int nIndex, LPBTSTR pstr); //插入一个字符串
- int Replace(LPBTSTR lpszOld, LPBTSTR lpszNew); //替换数据
- int Replace(BYTE chOld, BYTE chNew); //替换数据
- int Remove(BYTE chRemove); //移除一个字符
- void TrimRight(LPBTSTR lpszTargetList);
- void TrimRight(BYTE chTarget);//去掉右边chTarget
- void TrimRight(); //去掉右边空格
- void TrimLeft(LPBTSTR lpszTargets);
- void TrimLeft(BYTE chTarget); //去掉左边chTarget
- void TrimLeft(); //去掉左边空格
- //取某段字符串
- void SetAt(int nIndex, BYTE ch);
- BYTE GetAt(int nIndex) const;
- BString Mid(int nFirst) const; //取某段字符串
- BString Mid(int nFirst, int nCount) const; //取某段字符串
- BString Right(int nCount) const; //取右边字符串
- BString Left(int nCount) const; //取左边字符串
- void BString::MakeUpper(); //大写
- void BString::MakeLower(); //小写
- void BString::MakeReverse(); //????不知道干什么的 strrev
- //查找
- int Find(BYTE ch) const;
- int Find(BYTE ch, int nStart) const;
- int ReverseFind(BYTE ch) const;
- int Find(LPBTSTR lpszSub) const;
- int Find(LPBTSTR lpszSub, int nStart) const;
- int FindOneOf(LPBTSTR lpszCharSet) const;//得到第一个匹配lpszCharSet中其中一个字符的位置 调用_tcspbrk
- //高级操作
- BYTE* GetBuffer(int nMinBufLength); //重新分配内存,在拷贝原来的数据
- void ReleaseBuffer(int nNewLength=-1); //在[nNewLength]='\0',对内存大小没有改变
- BYTE* GetBufferSetLength(int nNewLength); //重新分配内存,在拷贝原来的数据
- void FreeExtra(); //深拷贝自己,然后--原来的引用记数器
- BYTE* LockBuffer(); //引用计数器=-1,加锁
- void UnlockBuffer(); //解锁,引用计数器=1
- //比较
- int Compare(LPBTSTR lpsz) const; //区分大小写比较
- int CompareNoCase(LPBTSTR lpsz) const; //不区分大小写比较
- //比较速度没有Compare快
- int Collate(LPBTSTR lpsz) const; //区分大小写比较
- int CollateNoCase(LPBTSTR lpsz) const; //不区分大小写比较
- //格式化字符串
- void Format(LPBTSTR lpszFormat, ...);//CSting中最长的函数了,完全是自己分析的(牛啊)
- private:
- void Init();
- BStringData* GetData() const; //通过m_pchData-1 得到BStringData
- void AllocBuffer(int nLen); //给BStringData分配内存,不带记数器
- void CopyBeforeWrite(); //带引用记数的复制自己深拷贝
- void AllocBeforeWrite(int nLen); //给BStringData分配内存,带记数器
- void AssignCopy(int nSrcLen, LPBTSTR lpszSrcData);//分配内存,并拷贝lpszSrcData内容
- //把nCopyIndex开始的nCopyLen长度的数据拷贝给dest,nExtraLen扩充的长度,次函数好像没下面用
- void AllocCopy(BString& dest, int nCopyLen, int nCopyIndex,int nExtraLen) const;
- void Release(); //--引用记数器并判断是否删除内存,如删除并初始化
- void FormatV(LPBTSTR lpszFormat, va_list argList);//格式化字符串
- void ConcatCopy(int nSrc1Len, LPBTSTR lpszSrc1Data,
- int nSrc2Len, LPBTSTR lpszSrc2Data);//连接数据lpszSrc1Data+lpszSrc2Data
- void ConcatInPlace(int nSrcLen, LPBTSTR lpszSrcData); //连接字符串
- static void Release(BStringData* pData); //--引用记数器并判断是否删除内存
- static void FreeData(BStringData* pData); //释放内存
- static int SafeStrlen(LPBTSTR lpsz); //得到长度
- BYTE* m_pchData; //指向BStringData的数据区
- };
- /*调用BString::Compare比较大小,如果比较中有CStirng的话用
- 调用operator LPBTSTR()转化类型为LPBTSTR
- */
- bool operator==(const BString& s1, const BString& s2);
- bool operator==(const BString& s1, LPBTSTR s2);
- bool operator==(LPBTSTR s1, const BString& s2);
- bool operator!=(const BString& s1, const BString& s2);
- bool operator!=(const BString& s1, LPBTSTR s2);
- bool operator!=(LPBTSTR s1, const BString& s2);
- bool operator<(const BString& s1, const BString& s2);
- bool operator<(const BString& s1, LPBTSTR s2);
- bool operator<(LPBTSTR s1, const BString& s2);
- bool operator>(const BString& s1, const BString& s2);
- bool operator>(const BString& s1, LPBTSTR s2);
- bool operator>(LPBTSTR s1, const BString& s2);
- bool operator<=(const BString& s1, const BString& s2);
- bool operator<=(const BString& s1, LPBTSTR s2);
- bool operator<=(LPBTSTR s1, const BString& s2);
- bool operator>=(const BString& s1, const BString& s2);
- bool operator>=(const BString& s1, LPBTSTR s2);
- bool operator>=(LPBTSTR s1, const BString& s2);
- //////////////////////////////////////////////////////////////////////
- //检测lpsz是否有效,调用了IsBadStringPtr
- BOOL AfxIsValidString(LPBTSTR lpsz, int nLength = -1);
- //检测lp是否能读写权限,调用了IsBadReadPtr,IsBadStringPtr
- BOOL AfxIsValidAddress(const void* lp,UINT nBytes, BOOL bReadWrite = TRUE);
- //CStirng数组操作
- void ConstructElements(BString* pElements, int nCount); //初始化CStirng数组
- void DestructElements(BString* pElements, int nCount); //删除CStirng数组
- void CopyElements(BString* pDest, const BString* pSrc, int nCount); //BString数组拷贝
- #endif
|