📄 contain.cxx
字号:
BOOL PString::operator*=(const char * cstr) const
{
PAssertNULL(cstr);
const char * pstr = theArray;
while (*pstr != '\0' && *cstr != '\0') {
if (toupper(*pstr) != toupper(*cstr))
return FALSE;
pstr++;
cstr++;
}
return *pstr == *cstr;
}
PObject::Comparison PString::InternalCompare(PINDEX offset, char c) const
{
char ch = theArray[offset];
if (ch < c)
return LessThan;
if (ch > c)
return GreaterThan;
return EqualTo;
}
PObject::Comparison PString::InternalCompare(
PINDEX offset, PINDEX length, const char * cstr) const
{
if (offset == 0 && theArray == cstr)
return EqualTo;
int retval;
if (length == P_MAX_INDEX)
retval = strcmp(theArray+offset, PAssertNULL(cstr));
else
retval = strncmp(theArray+offset, PAssertNULL(cstr), length);
if (retval < 0)
return LessThan;
if (retval > 0)
return GreaterThan;
return EqualTo;
}
PINDEX PString::Find(char ch, PINDEX offset) const
{
register PINDEX len = GetLength();
while (offset < len) {
if (InternalCompare(offset, ch) == EqualTo)
return offset;
offset++;
}
return P_MAX_INDEX;
}
PINDEX PString::Find(const char * cstr, PINDEX offset) const
{
PAssertNULL(cstr);
PAssert(*cstr != '\0', PInvalidParameter);
PINDEX len = GetLength();
PINDEX clen = strlen(cstr);
if (clen > len)
return P_MAX_INDEX;
if (offset > len - clen)
return P_MAX_INDEX;
if (len - clen < 10) {
while (offset+clen <= len) {
if (InternalCompare(offset, clen, cstr) == EqualTo)
return offset;
offset++;
}
return P_MAX_INDEX;
}
int strSum = 0;
int cstrSum = 0;
for (PINDEX i = 0; i < clen; i++) {
strSum += toupper(theArray[offset+i]);
cstrSum += toupper(cstr[i]);
}
// search for a matching substring
while (offset+clen <= len) {
if (strSum == cstrSum && InternalCompare(offset, clen, cstr) == EqualTo)
return offset;
strSum += toupper(theArray[offset+clen]);
strSum -= toupper(theArray[offset++]);
}
return P_MAX_INDEX;
}
PINDEX PString::FindLast(char ch, PINDEX offset) const
{
PINDEX len = GetLength();
if (len == 0)
return P_MAX_INDEX;
if (offset >= len)
offset = len-1;
while (InternalCompare(offset, ch) != EqualTo) {
if (offset == 0)
return P_MAX_INDEX;
offset--;
}
return offset;
}
PINDEX PString::FindLast(const char * cstr, PINDEX offset) const
{
PAssertNULL(cstr);
PAssert(*cstr != '\0', PInvalidParameter);
PINDEX len = GetLength();
PINDEX clen = strlen(cstr);
if (clen > len)
return P_MAX_INDEX;
if (offset == 0)
return P_MAX_INDEX;
if (offset > len - clen)
offset = len - clen;
int strSum = 0;
int cstrSum = 0;
for (PINDEX i = 0; i < clen; i++) {
strSum += toupper(theArray[offset+i]);
cstrSum += toupper(cstr[i]);
}
// search for a matching substring
while (offset > 0) {
if (strSum == cstrSum && InternalCompare(offset, clen, cstr) == EqualTo)
return offset;
strSum += toupper(theArray[--offset]);
strSum -= toupper(theArray[offset+clen]);
}
return P_MAX_INDEX;
}
PINDEX PString::FindOneOf(const char * cset, PINDEX offset) const
{
PAssertNULL(cset);
PINDEX len = GetLength();
while (offset < len) {
const char * p = cset;
while (*p != '\0') {
if (InternalCompare(offset, *p) == EqualTo)
return offset;
p++;
}
offset++;
}
return P_MAX_INDEX;
}
PINDEX PString::FindRegEx(const PRegularExpression & regex, PINDEX offset) const
{
PINDEX pos = 0;
PINDEX len = 0;
if (FindRegEx(regex, pos, len, offset))
return pos;
return P_MAX_INDEX;
}
BOOL PString::FindRegEx(const PRegularExpression & regex,
PINDEX & pos,
PINDEX & len,
PINDEX offset,
PINDEX maxPos) const
{
if (offset >= GetLength())
return FALSE;
if (!regex.Execute(&theArray[offset], pos, len, 0))
return FALSE;
pos += offset;
if (pos+len > maxPos)
return FALSE;
return TRUE;
}
void PString::Replace(const PString & target,
const PString & subs,
BOOL all, PINDEX offset)
{
MakeUnique();
PINDEX tlen = target.GetLength();
PINDEX slen = subs.GetLength();
do {
PINDEX pos = Find(target, offset);
if (pos == P_MAX_INDEX)
return;
Splice(subs, pos, tlen);
offset = pos + slen;
} while (all);
}
void PString::Splice(const char * cstr, PINDEX pos, PINDEX len)
{
register PINDEX slen = GetLength();
if (pos >= slen)
operator+=(cstr);
else {
MakeUnique();
PINDEX clen = strlen(PAssertNULL(cstr));
PINDEX newlen = slen-len+clen;
if (clen > len)
SetSize(newlen+1);
if (pos+len < slen)
PSTRING_MOVE(theArray, pos+clen, theArray, pos+len, slen-pos-len+1);
PSTRING_COPY(theArray+pos, cstr, clen);
theArray[newlen] = '\0';
}
}
PStringArray
PString::Tokenise(const char * separators, BOOL onePerSeparator) const
{
PStringArray tokens;
if (IsEmpty()) // No tokens
return tokens;
PINDEX token = 0;
PINDEX p1 = 0;
PINDEX p2 = FindOneOf(separators);
if (p2 == 0) {
if (onePerSeparator) { // first character is a token separator
tokens[token] = PString();
token++; // make first string in array empty
p1 = 1;
p2 = FindOneOf(separators, 1);
}
else {
do {
p1 = p2 + 1;
} while ((p2 = FindOneOf(separators, p1)) == p1);
}
}
while (p2 != P_MAX_INDEX) {
if (p2 > p1)
tokens[token] = operator()(p1, p2-1);
else
tokens[token] = PString();
token++;
// Get next separator. If not one token per separator then continue
// around loop to skip over all the consecutive separators.
do {
p1 = p2 + 1;
} while ((p2 = FindOneOf(separators, p1)) == p1 && !onePerSeparator);
}
tokens[token] = operator()(p1, P_MAX_INDEX);
return tokens;
}
PStringArray PString::Lines() const
{
PStringArray lines;
if (IsEmpty())
return lines;
PINDEX line = 0;
PINDEX p1 = 0;
PINDEX p2;
while ((p2 = FindOneOf("\r\n", p1)) != P_MAX_INDEX) {
lines[line++] = operator()(p1, p2-1);
p1 = p2 + 1;
if (theArray[p2] == '\r' && theArray[p1] == '\n') // CR LF pair
p1++;
}
if (p1 < GetLength())
lines[line] = operator()(p1, P_MAX_INDEX);
return lines;
}
PString PString::LeftTrim() const
{
const char * lpos = theArray;
while (isspace(*lpos))
lpos++;
return PString(lpos);
}
PString PString::RightTrim() const
{
char * rpos = theArray+GetLength()-1;
if (isspace(*rpos))
return *this;
while (isspace(*rpos)) {
if (rpos == theArray)
return PString();
rpos--;
}
#if defined(P_MACOSX) || defined(P_MACOS) // make Apple gnu compiler happy
PString retval(theArray, rpos - theArray);
return retval;
#else
return PString(theArray, rpos - theArray);
#endif
}
PString PString::Trim() const
{
const char * lpos = theArray;
while (isspace(*lpos))
lpos++;
if (*lpos == '\0')
return PString();
const char * rpos = theArray+GetLength()-1;
if (!isspace(*rpos))
return PString(lpos);
while (isspace(*rpos))
rpos--;
return PString(lpos, rpos - lpos + 1);
}
PString PString::ToLower() const
{
PString newStr(theArray);
for (char *cpos = newStr.theArray; *cpos != '\0'; cpos++) {
if (isupper(*cpos))
*cpos = (char)tolower(*cpos);
}
return newStr;
}
PString PString::ToUpper() const
{
PString newStr(theArray);
for (char *cpos = newStr.theArray; *cpos != '\0'; cpos++) {
if (islower(*cpos))
*cpos = (char)toupper(*cpos);
}
return newStr;
}
long PString::AsInteger(unsigned base) const
{
PAssert(base >= 2 && base <= 36, PInvalidParameter);
char * dummy;
return strtol(theArray, &dummy, base);
}
DWORD PString::AsUnsigned(unsigned base) const
{
PAssert(base >= 2 && base <= 36, PInvalidParameter);
char * dummy;
return strtoul(theArray, &dummy, base);
}
double PString::AsReal() const
{
#ifndef __HAS_NO_FLOAT
char * dummy;
return strtod(theArray, &dummy);
#else
return 0.0;
#endif
}
PBYTEArray PString::ToPascal() const
{
PINDEX len = GetLength();
PAssert(len < 256, "Cannot convert to PASCAL string");
BYTE buf[256];
buf[0] = (BYTE)len;
#ifdef PHAS_UNICODE
WORD * ptr = (WORD *)theArray;
while (len > 0) {
buf[len] = (BYTE)(*ptr < 256 ? *ptr : 255);
len--;
}
#else
memcpy(&buf[1], theArray, len);
#endif
return PBYTEArray(buf, len+1);
}
PString PString::ToLiteral() const
{
PString str('"');
for (char * p = theArray; *p != '\0'; p++) {
if (*p == '"')
str += "\\\"";
else if (isprint(*p))
str += *p;
else {
PINDEX i;
for (i = 0; i < PARRAYSIZE(PStringEscapeValue); i++) {
if (*p == PStringEscapeValue[i]) {
str += PString('\\') + PStringEscapeCode[i];
break;
}
}
if (i >= PARRAYSIZE(PStringEscapeValue))
str.sprintf("\\%03o", *p & 0xff);
}
}
return str + '"';
}
PString & PString::sprintf(const char * fmt, ...)
{
va_list args;
va_start(args, fmt);
return vsprintf(fmt, args);
}
#ifdef __GNUC__
#define _vsnprintf vsnprintf
#endif
PString & PString::vsprintf(const char * fmt, va_list arg)
{
PINDEX len = theArray != NULL ? GetLength() : 0;
PINDEX size = 0;
do {
size += 1000;
PAssert(SetSize(size), POutOfMemory);
} while (_vsnprintf(theArray+len, size-len, fmt, arg) == -1);
PAssert(MakeMinimumSize(), POutOfMemory);
return *this;
}
PString psprintf(const char * fmt, ...)
{
PString str;
va_list args;
va_start(args, fmt);
return str.vsprintf(fmt, args);
}
PString pvsprintf(const char * fmt, va_list arg)
{
PString str;
return str.vsprintf(fmt, arg);
}
///////////////////////////////////////////////////////////////////////////////
PObject * PCaselessString::Clone() const
{
return new PCaselessString(*this);
}
PObject::Comparison PCaselessString::InternalCompare(PINDEX offset, char c) const
{
int c1 = toupper(theArray[offset]);
int c2 = toupper(c);
if (c1 < c2)
return LessThan;
if (c1 > c2)
return GreaterThan;
return EqualTo;
}
PObject::Comparison PCaselessString::InternalCompare(
PINDEX offset, PINDEX length, const char * cstr) const
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -