401 lines
7.5 KiB
C++
401 lines
7.5 KiB
C++
//-----------------------------------------------------------------------------+
|
|
// Copyright (C), 1998-2007, Beijing Tangent Software Co. Ltd.
|
|
// = FileName : TAString.h
|
|
// = Version : ver2.0
|
|
// = Author : xlc
|
|
// = CreateDate : 2002-09-09
|
|
// = Description: TAString类的声明 通用字符串类,以NULL结尾
|
|
// = Maintainers:
|
|
//
|
|
//-----------------------------------------------------------------------------+
|
|
#ifndef _TASTRING_H_
|
|
#define _TASTRING_H_
|
|
|
|
#include "TArray.h"
|
|
|
|
class TAString : public TArray< char >
|
|
{
|
|
public:
|
|
TAString(long size = 0);
|
|
TAString(const TAString& string);
|
|
TAString(const char* format);
|
|
TAString(const wchar_t *);
|
|
|
|
TAString& Format(const char* format,...);
|
|
|
|
TAString& operator = (TAString& string);
|
|
TAString& operator = (const char* p);
|
|
|
|
virtual ~TAString();
|
|
|
|
//buffer
|
|
void Clear();
|
|
void Realloc(long size, bool bCopy = true);
|
|
void FreeExtra();
|
|
|
|
//attribute
|
|
long Length() const;
|
|
long IsValidIndex(long index) const;
|
|
long IsEmpty() const;
|
|
bool IsIntNumber() const;
|
|
bool IsFloatNumber() const;
|
|
|
|
//stream operator
|
|
TAString& operator << (char c);
|
|
TAString& operator << (int i);
|
|
TAString& operator << (double d);
|
|
TAString& operator << (const TAString& s);
|
|
TAString& operator << (const char* p);
|
|
|
|
const TAString& operator >> (int& i) const;
|
|
const TAString& operator >> (double& d) const;
|
|
const TAString& operator >> (TAString& s) const;
|
|
const TAString& operator >> (char* p) const;
|
|
|
|
//delete/insert
|
|
void DelRight(long count);
|
|
void DelLeft(long count);
|
|
void Del(long from, long to);
|
|
void Del(long i);
|
|
|
|
void Insert(long i, char c);
|
|
bool Insert(long i, const TAString& s);
|
|
|
|
//operator
|
|
bool operator == (const char* p) const;
|
|
bool operator != (const char* p) const;
|
|
bool operator > (const char* p) const;
|
|
bool operator < (const char* p) const;
|
|
int operator - (const char* p) const;
|
|
|
|
operator const char*() const;
|
|
operator double() const;
|
|
operator float() const;
|
|
operator int() const;
|
|
TAString operator + (const TAString& string) const;
|
|
|
|
//access
|
|
TAString Right(long count) const;
|
|
TAString Left(long count) const;
|
|
TAString Mid(long from,long to) const;
|
|
|
|
TAString& Extract(const char* format,...);
|
|
|
|
void Reverse(long start = 0,long to = -1);
|
|
void MakeUpper();
|
|
|
|
//search
|
|
long Find(char c,long from = 0,long to = -1) const;
|
|
long Find(const TAString& s, long from = 0,long to = -1) const;
|
|
long Find(const TAString& s, TQuene<long>& quene, long from = 0, long to = -1) const;
|
|
|
|
//支持通配符的查找替换
|
|
long FindEx(const TAString& sFindRule,TAString& res, int flag = 0, long from = 0, long to = -1) const;
|
|
long ReverseFind(char c) const;
|
|
long Replace(const TAString& s, const TAString& d, long from = 0, long to = -1);
|
|
long Del(const TAString& s);
|
|
void CreateNextTable(long *next) const;
|
|
void ReplaceEx(const TAString& sFindRule, const TAString& sReplaceRule, int flag = 0);
|
|
|
|
protected:
|
|
void SetEnd(long index);
|
|
void SetLength(long l);
|
|
|
|
void Copy(const char *p, long pos, long size);
|
|
void Copy(long from, long to, long size);
|
|
void Copy(const char *p, long pos = 0);
|
|
|
|
protected:
|
|
long m_nEnd;
|
|
|
|
};
|
|
|
|
inline TAString::TAString(long size)
|
|
: TArray<char>(size + 1)
|
|
{
|
|
Clear();
|
|
}
|
|
|
|
inline TAString::TAString(const TAString& string)
|
|
: TArray<char> (1)
|
|
{
|
|
Clear();
|
|
Copy(string,0,string.Length());
|
|
}
|
|
|
|
inline TAString::TAString(const char* format)
|
|
: TArray<char> (1)
|
|
{
|
|
Clear();
|
|
Copy(format);
|
|
}
|
|
|
|
inline TAString& TAString::operator = (TAString&string)
|
|
{
|
|
Clear();
|
|
Copy(string,0,string.Length());
|
|
return *this;
|
|
}
|
|
|
|
inline TAString& TAString::operator = (const char*p)
|
|
{
|
|
Clear();
|
|
Copy(p);
|
|
return *this;
|
|
}
|
|
|
|
inline void TAString::Realloc(long size,bool bCopy)
|
|
{
|
|
TArray<char>::Realloc(size + 1,bCopy);
|
|
SetAt(size,NULL);
|
|
}
|
|
inline void TAString::FreeExtra()
|
|
{
|
|
TArray<char>::Realloc(Length() + 1);
|
|
}
|
|
|
|
//attribute
|
|
inline long TAString::IsValidIndex(long index) const
|
|
{
|
|
return index >= 0 && index < m_nEnd;
|
|
}
|
|
|
|
inline long TAString::Length() const
|
|
{
|
|
return m_nEnd;
|
|
}
|
|
|
|
inline void TAString::Clear()
|
|
{
|
|
SetEnd(0);
|
|
}
|
|
|
|
inline long TAString::IsEmpty() const
|
|
{
|
|
return m_nEnd <= 0;
|
|
}
|
|
|
|
inline bool TAString::IsIntNumber() const
|
|
{
|
|
int temp;
|
|
*this >> temp;
|
|
return *this == "0" || temp != 0;
|
|
}
|
|
|
|
inline bool TAString::IsFloatNumber() const
|
|
{
|
|
double temp;
|
|
*this >> temp;
|
|
return *this == ".0" || *this == "0.0" || *this == "0." || temp != 0;
|
|
}
|
|
|
|
//stream operator
|
|
inline TAString& TAString::operator << (char c)
|
|
{
|
|
Insert(m_nEnd,c);
|
|
return *this;
|
|
}
|
|
|
|
inline TAString& TAString::operator << (int i)
|
|
{
|
|
char s[32];
|
|
_itoa(i,s,10);
|
|
Copy(s,m_nEnd);
|
|
return *this;
|
|
}
|
|
|
|
inline TAString& TAString::operator << (const TAString&s)
|
|
{
|
|
Copy(s, m_nEnd, s.Length());
|
|
return *this;
|
|
}
|
|
|
|
inline TAString& TAString::operator << (const char*p)
|
|
{
|
|
Copy(p,m_nEnd);
|
|
return *this;
|
|
}
|
|
|
|
inline const TAString& TAString::operator >> (int&i) const
|
|
{
|
|
i = atoi(pData);
|
|
return *this;
|
|
}
|
|
|
|
inline const TAString& TAString::operator >> (double&d) const
|
|
{
|
|
d = atof(pData);
|
|
return *this;
|
|
}
|
|
|
|
inline const TAString& TAString::operator >> (TAString&s) const
|
|
{
|
|
s << *this;
|
|
return *this;
|
|
}
|
|
|
|
inline const TAString& TAString::operator>>(char*p) const
|
|
{
|
|
memcpy(p, pData, Length() + 1);
|
|
return *this;
|
|
}
|
|
|
|
//delete/insert
|
|
inline void TAString::DelRight(long count)
|
|
{
|
|
SetEnd(m_nEnd - count);
|
|
}
|
|
|
|
inline void TAString::DelLeft(long count)
|
|
{
|
|
Copy(count, 0, m_nEnd - count);
|
|
SetEnd(m_nEnd - count);
|
|
}
|
|
|
|
inline void TAString::Del(long from,long to)
|
|
{
|
|
Copy(to + 1, from, m_nEnd - to + 1);
|
|
SetEnd(m_nEnd - to + from - 1);
|
|
}
|
|
|
|
inline void TAString::Del(long i)
|
|
{
|
|
Copy(i + 1, i, m_nEnd - i - 1);
|
|
SetEnd(m_nEnd - 1);
|
|
}
|
|
|
|
inline void TAString::Insert(long i,char c)
|
|
{
|
|
Copy(i, i + 1,m_nEnd - i);
|
|
pData[i] = c;
|
|
}
|
|
|
|
inline bool TAString::operator == (const char*p) const
|
|
{
|
|
return strcmp(pData,p) == 0;
|
|
}
|
|
|
|
inline bool TAString::operator != (const char*p) const
|
|
{
|
|
return strcmp(pData,p) != 0;
|
|
}
|
|
|
|
inline bool TAString::operator > (const char*p) const
|
|
{
|
|
return strcmp(pData,p) > 0;
|
|
}
|
|
|
|
inline bool TAString::operator < (const char*p) const
|
|
{
|
|
return strcmp(pData,p) < 0;
|
|
}
|
|
|
|
inline int TAString::operator - (const char*p) const
|
|
{
|
|
return strcmp(pData,p);
|
|
}
|
|
|
|
inline TAString::operator const char*() const
|
|
{
|
|
return pData;
|
|
}
|
|
|
|
inline TAString::operator double() const
|
|
{
|
|
return atof(pData);
|
|
}
|
|
|
|
inline TAString::operator float() const
|
|
{
|
|
return (float)atof(pData);
|
|
}
|
|
|
|
inline TAString::operator int() const
|
|
{
|
|
return atoi(pData);
|
|
}
|
|
|
|
inline TAString TAString::operator + (const TAString& string) const
|
|
{
|
|
TAString temp(Length() + string.Length());
|
|
temp << *this << string;
|
|
return temp;
|
|
}
|
|
|
|
//access
|
|
inline TAString TAString::Right(long count) const
|
|
{
|
|
TAString temp;
|
|
temp.Copy(pData + m_nEnd - count, 0, count);
|
|
return temp;
|
|
}
|
|
|
|
inline TAString TAString::Left(long count) const
|
|
{
|
|
TAString temp;
|
|
temp.Copy(pData,0,count);
|
|
return temp;
|
|
}
|
|
|
|
inline TAString TAString::Mid(long from,long to) const
|
|
{
|
|
TAString temp;
|
|
temp.Copy(pData + from, 0, to - from + 1);
|
|
return temp;
|
|
}
|
|
|
|
inline void TAString::Reverse(long start,long to)
|
|
{
|
|
if(to == -1)
|
|
{
|
|
to = m_nEnd - 1;
|
|
}
|
|
|
|
TArray<char>::Reverse(start,to);
|
|
}
|
|
|
|
inline long TAString::Find(char c,long from,long to) const
|
|
{
|
|
if(to == -1)
|
|
{
|
|
to = m_nEnd - 1;
|
|
}
|
|
|
|
return TArray<char>::Find(c,from,to);
|
|
}
|
|
|
|
inline long TAString::Del(const TAString&s)
|
|
{
|
|
return Replace(s,_T(""));
|
|
}
|
|
|
|
inline void TAString::SetEnd(long index)
|
|
{
|
|
pData[index] = NULL;
|
|
m_nEnd = index;
|
|
}
|
|
|
|
inline void TAString::SetLength(long l)
|
|
{
|
|
if(l + 1 > GetLength())
|
|
{
|
|
Realloc(l);
|
|
}
|
|
|
|
SetEnd(l);
|
|
}
|
|
|
|
|
|
inline void TAString::Copy(const char *p,long pos,long size)
|
|
{
|
|
long length1 = pos + size;
|
|
if(length1 > Length())
|
|
{
|
|
SetLength(length1);
|
|
}
|
|
memcpy(pData + pos,p,size);
|
|
}
|
|
|
|
|
|
|
|
#endif |