637 lines
8.9 KiB
C++
637 lines
8.9 KiB
C++
//-----------------------------------------------------------------------------+
|
||
// Copyright (C), 1998-2007, Beijing Tangent Software Co. Ltd.
|
||
// = FileName : TUString.cpp
|
||
// = Version : ver2.0
|
||
// = Author : xlc
|
||
// = CreateDate : 2002-09-09
|
||
// = Description: 类的定义
|
||
// = Maintainers:
|
||
//
|
||
//-----------------------------------------------------------------------------+
|
||
#include "..\StdAfx.h"
|
||
#include "TUString.h"
|
||
#include "TAString.h"
|
||
#include "TListQuene.h"
|
||
#include "def.h"
|
||
|
||
void TUString::Copy(const wchar_t *p, long pos)
|
||
{
|
||
if (p == NULL)
|
||
{
|
||
return;
|
||
}
|
||
|
||
for (long size = 0; p[size] != NULL; size++)
|
||
{
|
||
;
|
||
}
|
||
|
||
long length1 = pos + size;
|
||
if (length1 > Length())
|
||
{
|
||
SetLength(length1);
|
||
}
|
||
|
||
memcpy(pData + pos,p,size * sizeof(wchar_t));
|
||
}
|
||
|
||
void TUString::Copy(long from, long to, long size)
|
||
{
|
||
long length1 = to + size;
|
||
if (length1 > Length())
|
||
{
|
||
SetLength(length1);
|
||
}
|
||
|
||
wchar_t *p1 = pData + from,*p2 = pData + to;
|
||
if (from > to)
|
||
{
|
||
for (long i = 0; i < size; i++)
|
||
{
|
||
p2[i] = p1[i];
|
||
}
|
||
}
|
||
else if (to > from)
|
||
{
|
||
for (long i = size - 1; i >= 0; i--)
|
||
{
|
||
p2[i] = p1[i];
|
||
}
|
||
}
|
||
}
|
||
|
||
TUString::TUString(const wchar_t *format)
|
||
: TArray<wchar_t>(1)
|
||
{
|
||
Clear();
|
||
Copy(format);
|
||
}
|
||
|
||
TUString& TUString::Format(const wchar_t *format,...)
|
||
{
|
||
Clear();
|
||
|
||
Realloc(1024, false);
|
||
|
||
va_list vlist;
|
||
va_start(vlist, format);
|
||
vswprintf(pData, format, vlist);
|
||
va_end(vlist);
|
||
|
||
end = wcslen(pData);
|
||
|
||
return *this;
|
||
}
|
||
|
||
TUString::TUString(const char* s)
|
||
: TArray<wchar_t>(1)
|
||
{
|
||
Clear();
|
||
if (s == NULL)
|
||
{
|
||
return;
|
||
}
|
||
|
||
USES_CONVERSION;
|
||
*this = A2W(s);
|
||
}
|
||
|
||
TUString::~TUString()
|
||
{
|
||
|
||
}
|
||
|
||
void TUString::MakeUpper()
|
||
{
|
||
for (long i = 0; i < Length(); i++)
|
||
{
|
||
wchar_t &c = (*this)[i];
|
||
if (c >= 'a' && c <= 'z')
|
||
{
|
||
c -= 32;
|
||
}
|
||
}
|
||
}
|
||
|
||
TUString& TUString::operator << (double d)
|
||
{
|
||
TUString s;
|
||
s.Format(L"%f",d);
|
||
|
||
Copy(TUString(s),end);
|
||
|
||
long i = end - 1;
|
||
while (pData[i] == '0' && pData[i - 1] != '.')
|
||
{
|
||
i--;
|
||
}
|
||
|
||
SetEnd(i + 1);
|
||
|
||
return *this;
|
||
}
|
||
|
||
long TUString::ReverseFind(wchar_t c)const
|
||
{
|
||
for (long i = end - 1; i >= 0; i--)
|
||
{
|
||
if (pData[i] == c)
|
||
{
|
||
return i;
|
||
}
|
||
}
|
||
|
||
return NOT_FOUND;
|
||
}
|
||
|
||
long TUString::Find(const TUString &s, long from, long to)const
|
||
{
|
||
if (s.IsEmpty())
|
||
{
|
||
return NOT_FOUND;
|
||
}
|
||
|
||
if (to == -1)
|
||
{
|
||
to = Length() - 1;
|
||
}
|
||
|
||
long *next = new long[s.Length()];
|
||
s.CreateNextTable(next);
|
||
|
||
for (long i = from, j = 0; i <= to && j < s.Length(); )
|
||
{
|
||
if (pData[i] == s[j])
|
||
{
|
||
i++;
|
||
j++;
|
||
}
|
||
else
|
||
{
|
||
j = next[j];
|
||
if (j == -1)
|
||
{
|
||
j = 0;
|
||
i++;
|
||
}
|
||
}
|
||
}
|
||
|
||
delete[] next;
|
||
|
||
if (j == s.Length())
|
||
{
|
||
return i - j;
|
||
}
|
||
else
|
||
{
|
||
return NOT_FOUND;
|
||
}
|
||
}
|
||
|
||
//next(i)=j 当j>0,j为使得p(0)...p(j - 1) == p(i - j)...p(i - 1)的最大整数;
|
||
//当j>=0,表示当前被比较字符与当前索引为i的比较字符不相同时,当前比较字符移到j处继续比较;
|
||
//当j=-1,表示当前被比较字符与当前索引为i的比较字符不相同时,
|
||
//当前被比较字符前移一位,当前比较字符索引回0,重新比较;
|
||
void TUString::CreateNextTable(long *next)const
|
||
{
|
||
if (IsEmpty())
|
||
{
|
||
return;
|
||
}
|
||
|
||
next[0] = -1;
|
||
if (Length() == 1)
|
||
{
|
||
return;
|
||
}
|
||
next[1] = 0;
|
||
|
||
long lastIndex = Length() - 1;
|
||
for (long i = 1, j = 0; i < lastIndex; )
|
||
{
|
||
if (pData[i] == pData[j])
|
||
{
|
||
i++;
|
||
j++;
|
||
|
||
if (pData[i] == pData[j])
|
||
{
|
||
next[i] = next[j];
|
||
}
|
||
else
|
||
{
|
||
next[i] = j;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
j = next[j];
|
||
if (j == -1)
|
||
{
|
||
j = 0;
|
||
i++;
|
||
next[i] = 0;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
long TUString::Find(const TUString &s,TQuene<long> &quene, long from, long to)const
|
||
{
|
||
if (s.IsEmpty())
|
||
{
|
||
return NOT_FOUND;
|
||
}
|
||
|
||
if (to == -1)
|
||
{
|
||
to = Length() - 1;
|
||
}
|
||
|
||
long *next = new long[s.Length()];
|
||
s.CreateNextTable(next);
|
||
|
||
for (long i = from, j = 0; i <= to; )
|
||
{
|
||
if (pData[i] == s[j])
|
||
{
|
||
i++;
|
||
j++;
|
||
|
||
if (j == s.Length())
|
||
{
|
||
quene.EnQuene(i - j);
|
||
j = 0;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
j = next[j];
|
||
if (j == -1)
|
||
{
|
||
j = 0;
|
||
i++;
|
||
}
|
||
}
|
||
}
|
||
|
||
delete[] next;
|
||
|
||
return quene.GetLength();
|
||
}
|
||
|
||
long TUString::Replace(const TUString &s, const TUString &d, long from, long to)
|
||
{
|
||
if (to == -1)
|
||
{
|
||
to = Length() - 1;
|
||
}
|
||
|
||
TListQuene<long> quene;
|
||
long num = Find(s,quene,from,to);
|
||
if (num != 0)
|
||
{
|
||
long oldLength = Length();
|
||
length = oldLength + num * (d.Length() - s.Length()) + 1;
|
||
|
||
wchar_t *pTemp = pData;
|
||
pData = new wchar_t[length];
|
||
|
||
Clear();
|
||
|
||
long i,j = 0;
|
||
while (quene.DeQuene(i))
|
||
{
|
||
Copy(pTemp + j, end, i - j);
|
||
*this << d;
|
||
|
||
j = i + s.Length();
|
||
}
|
||
|
||
Copy(pTemp + j, end, oldLength - j);
|
||
delete[] pTemp;
|
||
}
|
||
|
||
return num;
|
||
}
|
||
|
||
//支持通配符的查找替换
|
||
long TUString::FindEx(const TUString &sFindRule, TUString &res,int flag,long from,long to)const
|
||
{
|
||
return NOT_FOUND;
|
||
}
|
||
|
||
void TUString::ReplaceEx(const TUString &sFindRule,const TUString &sReplaceRule,int flag)
|
||
{
|
||
|
||
}
|
||
|
||
bool TUString::Insert(long i, const TUString &s)
|
||
{
|
||
if (i >= 0 && i <= end)
|
||
{
|
||
TUString temp = Right(end - i);
|
||
|
||
SetEnd(i);
|
||
*this << s << temp;
|
||
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
TUString::TUString(long size)
|
||
: TArray<wchar_t>(size + 1)
|
||
{
|
||
Clear();
|
||
}
|
||
|
||
TUString::TUString(const TUString &string)
|
||
: TArray<wchar_t>(1)
|
||
{
|
||
Clear();
|
||
Copy(string,0,string.Length());
|
||
}
|
||
|
||
TUString& TUString::operator = (TUString &string)
|
||
{
|
||
Clear();
|
||
Copy(string,0,string.Length());
|
||
|
||
return *this;
|
||
}
|
||
|
||
TUString& TUString::operator = (const wchar_t *p)
|
||
{
|
||
Clear();
|
||
Copy(p);
|
||
|
||
return *this;
|
||
}
|
||
|
||
void TUString::Realloc(long size,bool bCopy)
|
||
{
|
||
TArray<wchar_t>::Realloc(size + 1, bCopy);
|
||
SetAt(size, NULL);
|
||
}
|
||
|
||
void TUString::FreeExtra()
|
||
{
|
||
TArray<wchar_t>::Realloc(Length() + 1);
|
||
}
|
||
|
||
long TUString::IsValidIndex(long index)const
|
||
{
|
||
return index >= 0 && index < end;
|
||
}
|
||
|
||
long TUString::Length()const
|
||
{
|
||
return end;
|
||
}
|
||
|
||
void TUString::Clear()
|
||
{
|
||
SetEnd(0);
|
||
}
|
||
|
||
long TUString::IsEmpty()const
|
||
{
|
||
return end <= 0;
|
||
}
|
||
|
||
bool TUString::IsIntNumber()const
|
||
{
|
||
int temp;
|
||
*this >> temp;
|
||
|
||
return *this == TUString("0") || temp != 0;
|
||
}
|
||
|
||
bool TUString::IsFloatNumber()const
|
||
{
|
||
double temp;
|
||
*this >> temp;
|
||
|
||
return *this == TUString(".0") || *this == TUString("0.0") || *this == TUString("0.") || temp != 0;
|
||
}
|
||
|
||
TUString& TUString::operator << (wchar_t c)
|
||
{
|
||
Insert(end,c);
|
||
|
||
return *this;
|
||
}
|
||
|
||
TUString& TUString::operator << (int i)
|
||
{
|
||
wchar_t s[32];
|
||
_itow(i,s,10);
|
||
Copy(s,end);
|
||
|
||
return *this;
|
||
}
|
||
|
||
TUString& TUString::operator << (const TUString &s)
|
||
{
|
||
Copy(s,end,s.Length());
|
||
|
||
return *this;
|
||
}
|
||
|
||
TUString& TUString::operator << (const wchar_t *p)
|
||
{
|
||
Copy(p,end);
|
||
|
||
return *this;
|
||
}
|
||
|
||
const TUString& TUString::operator >> (int &i)const
|
||
{
|
||
i = _wtoi(pData);
|
||
|
||
return *this;
|
||
}
|
||
|
||
const TUString& TUString::operator>>(double &d)const
|
||
{
|
||
d = (double)(*this);
|
||
|
||
return *this;
|
||
}
|
||
|
||
const TUString& TUString::operator>>(TUString &s)const
|
||
{
|
||
s << *this;
|
||
|
||
return *this;
|
||
}
|
||
|
||
const TUString& TUString::operator>>(wchar_t *p)const
|
||
{
|
||
memcpy(p, pData, Length() + 1);
|
||
|
||
return *this;
|
||
}
|
||
|
||
void TUString::DelRight(long count)
|
||
{
|
||
SetEnd(end - count);
|
||
}
|
||
|
||
void TUString::DelLeft(long count)
|
||
{
|
||
Copy(count, 0, end - count);
|
||
SetEnd(end - count);
|
||
}
|
||
|
||
void TUString::Del(long from, long to)
|
||
{
|
||
Copy(to + 1, from, end - to + 1);
|
||
SetEnd(end - to + from - 1);
|
||
}
|
||
|
||
void TUString::Del(long i)
|
||
{
|
||
Copy(i + 1, i, end - i - 1);
|
||
SetEnd(end - 1);
|
||
}
|
||
|
||
void TUString::Insert(long i, wchar_t c)
|
||
{
|
||
Copy(i, i + 1, end - i);
|
||
pData[i] = c;
|
||
}
|
||
|
||
bool TUString::operator == (const wchar_t *p)const
|
||
{
|
||
return wcscmp(pData,p) == 0;
|
||
}
|
||
|
||
bool TUString::operator != (const wchar_t *p)const
|
||
{
|
||
return wcscmp(pData,p) != 0;
|
||
}
|
||
|
||
bool TUString::operator>(const wchar_t *p)const
|
||
{
|
||
return wcscmp(pData,p) > 0;
|
||
}
|
||
|
||
bool TUString::operator<(const wchar_t *p)const
|
||
{
|
||
return wcscmp(pData,p) < 0;
|
||
}
|
||
|
||
int TUString::operator - (const wchar_t *p)const
|
||
{
|
||
return wcscmp(pData,p);
|
||
}
|
||
|
||
TUString::operator const wchar_t*()const
|
||
{
|
||
return pData;
|
||
}
|
||
|
||
TUString::operator double()const
|
||
{
|
||
return (double)TAString(pData);
|
||
}
|
||
|
||
TUString::operator float()const
|
||
{
|
||
return (float)TAString(pData);
|
||
}
|
||
|
||
TUString::operator int()const
|
||
{
|
||
return _wtoi(pData);
|
||
}
|
||
|
||
TUString TUString::operator + (const TUString &string)const
|
||
{
|
||
TUString temp(Length() + string.Length());
|
||
temp << *this << string;
|
||
|
||
return temp;
|
||
}
|
||
|
||
TUString TUString::Right(long count)const
|
||
{
|
||
TUString temp;
|
||
temp.Copy(pData + end - count, 0, count);
|
||
|
||
return temp;
|
||
}
|
||
|
||
TUString TUString::Left(long count)const
|
||
{
|
||
TUString temp;
|
||
temp.Copy(pData, 0, count);
|
||
|
||
return temp;
|
||
}
|
||
|
||
TUString TUString::Mid(long from, long to)const
|
||
{
|
||
TUString temp;
|
||
temp.Copy(pData + from, 0, to - from + 1);
|
||
|
||
return temp;
|
||
}
|
||
|
||
void TUString::Reverse(long start, long to)
|
||
{
|
||
if (to == -1)
|
||
{
|
||
to = end - 1;
|
||
}
|
||
|
||
TArray<wchar_t>::Reverse(start,to);
|
||
}
|
||
|
||
long TUString::Find(wchar_t c, long from, long to)const
|
||
{
|
||
if (to == -1)
|
||
{
|
||
to = end - 1;
|
||
}
|
||
|
||
return TArray<wchar_t>::Find(c, from, to);
|
||
}
|
||
|
||
long TUString::Del(const TUString &s)
|
||
{
|
||
return Replace(s,TUString());
|
||
}
|
||
|
||
void TUString::SetEnd(long index)
|
||
{
|
||
pData[index] = NULL;
|
||
end = index;
|
||
}
|
||
|
||
void TUString::SetLength(long l)
|
||
{
|
||
if (l + 1 > GetLength())
|
||
{
|
||
Realloc(l);
|
||
}
|
||
|
||
SetEnd(l);
|
||
}
|
||
|
||
void TUString::Copy(const wchar_t *p, long pos, long size)
|
||
{
|
||
long length1 = pos + size;
|
||
if (length1 > Length())
|
||
{
|
||
SetLength(length1);
|
||
}
|
||
|
||
memcpy(pData + pos, p, size * 2);
|
||
} |