Files
envi-code/SourceCode/Code2026/AdsXRx/UString.cpp
T

638 lines
8.9 KiB
C++
Raw Normal View History

2026-10-04 00:04:20 +08:00
//-----------------------------------------------------------------------------+
2026-09-28 15:28:50 +08:00
// Copyright (C), 1998-2007, SH Software Co. Ltd.
// = FileName : UString.cpp
// = Version : ver2.0
// = Author : zjq
// = CreateDate : 2002-09-09
2026-10-04 00:04:20 +08:00
// = Description: 类的定义
2026-09-28 15:28:50 +08:00
// = Maintainers:
//
//-----------------------------------------------------------------------------+
#include "StdAfx.h"
#include "UString.h"
#include "AString.h"
#include "ListQuene.h"
void UString::Copy(const wchar_t *p, long pos)
{
if (p == NULL)
{
return;
}
long size(0);
for (size = 0; p[size] != NULL; size++)
{
;
}
long length1 = pos + size;
if (length1 > Length())
{
SetLength(length1);
}
memcpy(pData + pos,p,size * sizeof(wchar_t));
}
void UString::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];
}
}
}
UString::UString(const wchar_t *format)
: Array<wchar_t>(1)
{
Clear();
Copy(format);
}
UString& UString::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;
}
UString::UString(const char* s)
: Array<wchar_t>(1)
{
Clear();
if (s == NULL)
{
return;
}
USES_CONVERSION;
*this = A2W(s);
}
UString::~UString()
{
}
void UString::MakeUpper()
{
for (long i = 0; i < Length(); i++)
{
wchar_t &c = (*this)[i];
if (c >= 'a' && c <= 'z')
{
c -= 32;
}
}
}
UString& UString::operator << (double d)
{
UString s;
s.Format(L"%f",d);
Copy(UString(s),end);
long i = end - 1;
while (pData[i] == '0' && pData[i - 1] != '.')
{
i--;
}
SetEnd(i + 1);
return *this;
}
long UString::ReverseFind(wchar_t c)const
{
for (long i = end - 1; i >= 0; i--)
{
if (pData[i] == c)
{
return i;
}
}
return NOT_FOUND;
}
long UString::Find(const UString &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);
long i(from), j(0);
for (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;
}
}
2026-10-04 00:04:20 +08:00
//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,重新比较;
2026-09-28 15:28:50 +08:00
void UString::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 UString::Find(const UString &s,Quene<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 UString::Replace(const UString &s, const UString &d, long from, long to)
{
if (to == -1)
{
to = Length() - 1;
}
ListQuene<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;
}
2026-10-04 00:04:20 +08:00
//支持通配符的查找替换
2026-09-28 15:28:50 +08:00
long UString::FindEx(const UString &sFindRule, UString &res,int flag,long from,long to)const
{
return NOT_FOUND;
}
void UString::ReplaceEx(const UString &sFindRule,const UString &sReplaceRule,int flag)
{
}
bool UString::Insert(long i, const UString &s)
{
if (i >= 0 && i <= end)
{
UString temp = Right(end - i);
SetEnd(i);
*this << s << temp;
return true;
}
else
{
return false;
}
}
UString::UString(long size)
: Array<wchar_t>(size + 1)
{
Clear();
}
UString::UString(const UString &string)
: Array<wchar_t>(1)
{
Clear();
Copy(string,0,string.Length());
}
UString& UString::operator = (UString &string)
{
Clear();
Copy(string,0,string.Length());
return *this;
}
UString& UString::operator = (const wchar_t *p)
{
Clear();
Copy(p);
return *this;
}
void UString::Realloc(long size,bool bCopy)
{
Array<wchar_t>::Realloc(size + 1, bCopy);
SetAt(size, NULL);
}
void UString::FreeExtra()
{
Array<wchar_t>::Realloc(Length() + 1);
}
long UString::IsValidIndex(long index)const
{
return index >= 0 && index < end;
}
long UString::Length()const
{
return end;
}
void UString::Clear()
{
SetEnd(0);
}
long UString::IsEmpty()const
{
return end <= 0;
}
bool UString::IsIntNumber()const
{
int temp;
*this >> temp;
return *this == UString("0") || temp != 0;
}
bool UString::IsFloatNumber()const
{
double temp;
*this >> temp;
return *this == UString(".0") || *this == UString("0.0") || *this == UString("0.") || temp != 0;
}
UString& UString::operator << (wchar_t c)
{
Insert(end,c);
return *this;
}
UString& UString::operator << (int i)
{
wchar_t s[32];
_itow(i,s,10);
Copy(s,end);
return *this;
}
UString& UString::operator << (const UString &s)
{
Copy(s,end,s.Length());
return *this;
}
UString& UString::operator << (const wchar_t *p)
{
Copy(p,end);
return *this;
}
const UString& UString::operator >> (int &i)const
{
i = _wtoi(pData);
return *this;
}
const UString& UString::operator>>(double &d)const
{
d = (double)(*this);
return *this;
}
const UString& UString::operator>>(UString &s)const
{
s << *this;
return *this;
}
const UString& UString::operator>>(wchar_t *p)const
{
memcpy(p, pData, Length() + 1);
return *this;
}
void UString::DelRight(long count)
{
SetEnd(end - count);
}
void UString::DelLeft(long count)
{
Copy(count, 0, end - count);
SetEnd(end - count);
}
void UString::Del(long from, long to)
{
Copy(to + 1, from, end - to + 1);
SetEnd(end - to + from - 1);
}
void UString::Del(long i)
{
Copy(i + 1, i, end - i - 1);
SetEnd(end - 1);
}
void UString::Insert(long i, wchar_t c)
{
Copy(i, i + 1, end - i);
pData[i] = c;
}
bool UString::operator == (const wchar_t *p)const
{
return wcscmp(pData,p) == 0;
}
bool UString::operator != (const wchar_t *p)const
{
return wcscmp(pData,p) != 0;
}
bool UString::operator>(const wchar_t *p)const
{
return wcscmp(pData,p) > 0;
}
bool UString::operator<(const wchar_t *p)const
{
return wcscmp(pData,p) < 0;
}
int UString::operator - (const wchar_t *p)const
{
return wcscmp(pData,p);
}
UString::operator const wchar_t*()const
{
return pData;
}
UString::operator double()const
{
return (double)AString(pData);
}
UString::operator float()const
{
return (float)AString(pData);
}
UString::operator int()const
{
return _wtoi(pData);
}
UString UString::operator + (const UString &string)const
{
UString temp(Length() + string.Length());
temp << *this << string;
return temp;
}
UString UString::Right(long count)const
{
UString temp;
temp.Copy(pData + end - count, 0, count);
return temp;
}
UString UString::Left(long count)const
{
UString temp;
temp.Copy(pData, 0, count);
return temp;
}
UString UString::Mid(long from, long to)const
{
UString temp;
temp.Copy(pData + from, 0, to - from + 1);
return temp;
}
void UString::Reverse(long start, long to)
{
if (to == -1)
{
to = end - 1;
}
Array<wchar_t>::Reverse(start,to);
}
long UString::Find(wchar_t c, long from, long to)const
{
if (to == -1)
{
to = end - 1;
}
return Array<wchar_t>::Find(c, from, to);
}
long UString::Del(const UString &s)
{
return Replace(s,UString());
}
void UString::SetEnd(long index)
{
pData[index] = NULL;
end = index;
}
void UString::SetLength(long l)
{
if (l + 1 > GetLength())
{
Realloc(l);
}
SetEnd(l);
}
void UString::Copy(const wchar_t *p, long pos, long size)
{
long length1 = pos + size;
if (length1 > Length())
{
SetLength(length1);
}
memcpy(pData + pos, p, size * 2);
}