Files
gjm 164968b62e chore
把非utf8-bom编码的cpp/h文件改为 utf8 bom 编码, msvc识别utf8编码时,如果不是bom格式的,会使用当前cp_oem来解码.
2026-10-04 00:04:20 +08:00

387 lines
6.9 KiB
C++

#pragma once
#include "SHArray.h"
#include "SHQuene.h"
class AString : public SHArray< char >
{
public:
AString(long size = 0);
AString(const AString& string);
AString(const char* format);
AString(const wchar_t *);
AString& Format(const char* format,...);
AString& operator = (AString& string);
AString& operator = (const char* p);
virtual ~AString();
//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
AString& operator << (char c);
AString& operator << (int i);
AString& operator << (double d);
AString& operator << (const AString& s);
AString& operator << (const char* p);
const AString& operator >> (int& i) const;
const AString& operator >> (double& d) const;
const AString& operator >> (AString& s) const;
const AString& 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 AString& 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;
AString operator + (const AString& string) const;
//access
AString Right(long count) const;
AString Left(long count) const;
AString Mid(long from,long to) const;
AString& 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 AString& s, long from = 0,long to = -1) const;
long Find(const AString& s, SHQuene<long>& quene, long from = 0, long to = -1) const;
//支持通配符的查找替换
long FindEx(const AString& sFindRule,AString& res, int flag = 0, long from = 0, long to = -1) const;
long ReverseFind(char c) const;
long Replace(const AString& s, const AString& d, long from = 0, long to = -1);
long Del(const AString& s);
void CreateNextTable(long *next) const;
void ReplaceEx(const AString& sFindRule, const AString& 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 AString::AString(long size)
: SHArray<char>(size + 1)
{
Clear();
}
inline AString::AString(const AString& string)
: SHArray<char> (1)
{
Clear();
Copy(string,0,string.Length());
}
inline AString::AString(const char* format)
: SHArray<char> (1)
{
Clear();
Copy(format);
}
inline AString& AString::operator = (AString&string)
{
Clear();
Copy(string,0,string.Length());
return *this;
}
inline AString& AString::operator = (const char*p)
{
Clear();
Copy(p);
return *this;
}
inline void AString::Realloc(long size,bool bCopy)
{
SHArray<char>::Realloc(size + 1,bCopy);
SetAt(size,NULL);
}
inline void AString::FreeExtra()
{
SHArray<char>::Realloc(Length() + 1);
}
//attribute
inline long AString::IsValidIndex(long index) const
{
return index >= 0 && index < m_nEnd;
}
inline long AString::Length() const
{
return m_nEnd;
}
inline void AString::Clear()
{
SetEnd(0);
}
inline long AString::IsEmpty() const
{
return m_nEnd <= 0;
}
inline bool AString::IsIntNumber() const
{
int temp;
*this >> temp;
return *this == "0" || temp != 0;
}
inline bool AString::IsFloatNumber() const
{
double temp;
*this >> temp;
return *this == ".0" || *this == "0.0" || *this == "0." || temp != 0;
}
//stream operator
inline AString& AString::operator << (char c)
{
Insert(m_nEnd,c);
return *this;
}
inline AString& AString::operator << (int i)
{
char s[32];
_itoa(i,s,10);
Copy(s,m_nEnd);
return *this;
}
inline AString& AString::operator << (const AString&s)
{
Copy(s, m_nEnd, s.Length());
return *this;
}
inline AString& AString::operator << (const char*p)
{
Copy(p,m_nEnd);
return *this;
}
inline const AString& AString::operator >> (int&i) const
{
i = atoi(pData);
return *this;
}
inline const AString& AString::operator >> (double&d) const
{
d = atof(pData);
return *this;
}
inline const AString& AString::operator >> (AString&s) const
{
s << *this;
return *this;
}
inline const AString& AString::operator>>(char*p) const
{
memcpy(p, pData, Length() + 1);
return *this;
}
//delete/insert
inline void AString::DelRight(long count)
{
SetEnd(m_nEnd - count);
}
inline void AString::DelLeft(long count)
{
Copy(count, 0, m_nEnd - count);
SetEnd(m_nEnd - count);
}
inline void AString::Del(long from,long to)
{
Copy(to + 1, from, m_nEnd - to + 1);
SetEnd(m_nEnd - to + from - 1);
}
inline void AString::Del(long i)
{
Copy(i + 1, i, m_nEnd - i - 1);
SetEnd(m_nEnd - 1);
}
inline void AString::Insert(long i,char c)
{
Copy(i, i + 1,m_nEnd - i);
pData[i] = c;
}
inline bool AString::operator == (const char*p) const
{
return strcmp(pData,p) == 0;
}
inline bool AString::operator != (const char*p) const
{
return strcmp(pData,p) != 0;
}
inline bool AString::operator > (const char*p) const
{
return strcmp(pData,p) > 0;
}
inline bool AString::operator < (const char*p) const
{
return strcmp(pData,p) < 0;
}
inline int AString::operator - (const char*p) const
{
return strcmp(pData,p);
}
inline AString::operator const char*() const
{
return pData;
}
inline AString::operator double() const
{
return atof(pData);
}
inline AString::operator float() const
{
return (float)atof(pData);
}
inline AString::operator int() const
{
return atoi(pData);
}
inline AString AString::operator + (const AString& string) const
{
AString temp(Length() + string.Length());
temp << *this << string;
return temp;
}
//access
inline AString AString::Right(long count) const
{
AString temp;
temp.Copy(pData + m_nEnd - count, 0, count);
return temp;
}
inline AString AString::Left(long count) const
{
AString temp;
temp.Copy(pData,0,count);
return temp;
}
inline AString AString::Mid(long from,long to) const
{
AString temp;
temp.Copy(pData + from, 0, to - from + 1);
return temp;
}
inline void AString::Reverse(long start,long to)
{
if(to == -1)
{
to = m_nEnd - 1;
}
SHArray<char>::Reverse(start,to);
}
inline long AString::Find(char c,long from,long to) const
{
if(to == -1)
{
to = m_nEnd - 1;
}
return SHArray<char>::Find(c,from,to);
}
inline long AString::Del(const AString&s)
{
return Replace(s,_T(""));
}
inline void AString::SetEnd(long index)
{
pData[index] = NULL;
m_nEnd = index;
}
inline void AString::SetLength(long l)
{
if(l + 1 > GetLength())
{
Realloc(l);
}
SetEnd(l);
}
inline void AString::Copy(const char *p,long pos,long size)
{
long length1 = pos + size;
if(length1 > Length())
{
SetLength(length1);
}
memcpy(pData + pos,p,size);
}