📄 contain.cxx
字号:
} } *ptr = '\0'; if ((len > 0) && (ptr[-1] == '\r')) ptr[-1] = '\0'; PAssert(MakeMinimumSize(), POutOfMemory);}PObject::Comparison PString::Compare(const PObject & obj) const{ PAssert(PIsDescendant(&obj, PString), PInvalidCast); return InternalCompare(0, P_MAX_INDEX, ((const PString &)obj).theArray);}PINDEX PString::HashFunction() const{ // Hash function from "Data Structures and Algorithm Analysis in C++" by // Mark Allen Weiss, with limit of only executing over first 8 characters to // increase speed when dealing with large strings. PINDEX hash = 0; for (PINDEX i = 0; i < 8 && theArray[i] != 0; i++) hash = (hash << 5) ^ tolower(theArray[i] & 0xff) ^ hash; return PABSINDEX(hash)%127;}BOOL PString::IsEmpty() const{ return (theArray == NULL) || (*theArray == '\0');}BOOL PString::SetSize(PINDEX newSize){ return InternalSetSize(newSize, TRUE);}BOOL PString::MakeUnique(){#if PCONTAINER_USES_CRITSEC PEnterAndLeave m(reference->critSec);#endif if (IsUnique()) return TRUE; InternalSetSize(GetSize(), TRUE); return FALSE;}PString PString::operator+(const char * cstr) const{ if (cstr == NULL) return *this; PINDEX olen = GetLength(); PINDEX alen = strlen(cstr)+1; PString str; str.SetSize(olen+alen); memmove(str.theArray, theArray, olen); memcpy(str.theArray+olen, cstr, alen); return str;}PString PString::operator+(char c) const{ PINDEX olen = GetLength(); PString str; str.SetSize(olen+2); memmove(str.theArray, theArray, olen); str.theArray[olen] = c; return str;}PString & PString::operator+=(const char * cstr){ if (cstr == NULL) return *this; PINDEX olen = GetLength(); PINDEX alen = strlen(cstr)+1; SetSize(olen+alen); memcpy(theArray+olen, cstr, alen); return *this;}PString & PString::operator+=(char ch){ PINDEX olen = GetLength(); SetSize(olen+2); theArray[olen] = ch; return *this;}PString PString::operator&(const char * cstr) const{ if (cstr == NULL) return *this; PINDEX alen = strlen(cstr)+1; if (alen == 1) return *this; PINDEX olen = GetLength(); PString str; PINDEX space = olen > 0 && theArray[olen-1]!=' ' && *cstr!=' ' ? 1 : 0; str.SetSize(olen+alen+space); memmove(str.theArray, theArray, olen); if (space != 0) str.theArray[olen] = ' '; memcpy(str.theArray+olen+space, cstr, alen); return str;}PString PString::operator&(char c) const{ PINDEX olen = GetLength(); PString str; PINDEX space = olen > 0 && theArray[olen-1] != ' ' && c != ' ' ? 1 : 0; str.SetSize(olen+2+space); memmove(str.theArray, theArray, olen); if (space != 0) str.theArray[olen] = ' '; str.theArray[olen+space] = c; return str;}PString & PString::operator&=(const char * cstr){ if (cstr == NULL) return *this; PINDEX alen = strlen(cstr)+1; if (alen == 1) return *this; PINDEX olen = GetLength(); PINDEX space = olen > 0 && theArray[olen-1]!=' ' && *cstr!=' ' ? 1 : 0; SetSize(olen+alen+space); if (space != 0) theArray[olen] = ' '; memcpy(theArray+olen+space, cstr, alen); return *this;}PString & PString::operator&=(char ch){ PINDEX olen = GetLength(); PINDEX space = olen > 0 && theArray[olen-1] != ' ' && ch != ' ' ? 1 : 0; SetSize(olen+2+space); if (space != 0) theArray[olen] = ' '; theArray[olen+space] = ch; return *this;}void PString::Delete(PINDEX start, PINDEX len){ if (start < 0 || len < 0) return; MakeUnique(); register PINDEX slen = GetLength(); if (start > slen) return; if (len > slen - start) SetAt(start, '\0'); else memmove(theArray+start, theArray+start+len, slen-start-len+1); MakeMinimumSize();}PString PString::operator()(PINDEX start, PINDEX end) const{ if (end < 0 || start < 0 || end < start) return Empty(); register PINDEX len = GetLength(); if (start > len) return Empty(); if (end >= len) { if (start == 0) return *this; end = len-1; } len = end - start + 1; return PString(theArray+start, len);}PString PString::Left(PINDEX len) const{ if (len <= 0) return Empty(); if (len >= GetLength()) return *this; return PString(theArray, len);}PString PString::Right(PINDEX len) const{ if (len <= 0) return Empty(); PINDEX srclen = GetLength(); if (len >= srclen) return *this; return PString(theArray+srclen-len, len);}PString PString::Mid(PINDEX start, PINDEX len) const{ if (len <= 0 || start < 0) return Empty(); if (start+len < start) // Beware of wraparound return operator()(start, P_MAX_INDEX); else return operator()(start, start+len-1);}bool PString::operator*=(const char * cstr) const{ if (cstr == NULL) return IsEmpty() != FALSE; const char * pstr = theArray; while (*pstr != '\0' && *cstr != '\0') { if (toupper(*pstr & 0xff) != toupper(*cstr & 0xff)) return FALSE; pstr++; cstr++; } return *pstr == *cstr;}PObject::Comparison PString::NumCompare(const PString & str, PINDEX count, PINDEX offset) const{ if (offset < 0 || count < 0) return LessThan; PINDEX len = str.GetLength(); if (count > len) count = len; return InternalCompare(offset, count, str);}PObject::Comparison PString::NumCompare(const char * cstr, PINDEX count, PINDEX offset) const{ if (offset < 0 || count < 0) return LessThan; PINDEX len = ::strlen(cstr); if (count > len) count = len; return InternalCompare(offset, count, cstr);}PObject::Comparison PString::InternalCompare(PINDEX offset, char c) const{ if (offset < 0) return LessThan; const int ch = theArray[offset] & 0xff; if (ch < (c & 0xff)) return LessThan; if (ch > (c & 0xff)) return GreaterThan; return EqualTo;}PObject::Comparison PString::InternalCompare( PINDEX offset, PINDEX length, const char * cstr) const{ if (offset < 0 || length < 0) return LessThan; if (offset == 0 && theArray == cstr) return EqualTo; if (offset < 0 || cstr == NULL) return IsEmpty() ? EqualTo : LessThan; int retval; if (length == P_MAX_INDEX) retval = strcmp(theArray+offset, cstr); else retval = strncmp(theArray+offset, cstr, length); if (retval < 0) return LessThan; if (retval > 0) return GreaterThan; return EqualTo;}PINDEX PString::Find(char ch, PINDEX offset) const{ if (offset < 0) return P_MAX_INDEX; 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{ if (cstr == NULL || *cstr == '\0' || offset < 0) return P_MAX_INDEX; 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] & 0xff); cstrSum += toupper(cstr[i] & 0xff); } // search for a matching substring while (offset+clen <= len) { if (strSum == cstrSum && InternalCompare(offset, clen, cstr) == EqualTo) return offset; strSum += toupper(theArray[offset+clen] & 0xff); strSum -= toupper(theArray[offset] & 0xff); offset++; } return P_MAX_INDEX;}PINDEX PString::FindLast(char ch, PINDEX offset) const{ PINDEX len = GetLength(); if (len == 0 || offset < 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{ if (cstr == NULL || *cstr == '\0' || offset < 0) return P_MAX_INDEX; PINDEX len = GetLength(); PINDEX clen = strlen(cstr); if (clen > len) 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] & 0xff); cstrSum += toupper(cstr[i] & 0xff); } // search for a matching substring while (strSum != cstrSum || InternalCompare(offset, clen, cstr) != EqualTo) { if (offset == 0) return P_MAX_INDEX; --offset; strSum += toupper(theArray[offset] & 0xff); strSum -= toupper(theArray[offset+clen] & 0xff); } return offset;}PINDEX PString::FindOneOf(const char * cset, PINDEX offset) const{ if (cset == NULL || *cset == '\0' || offset < 0) return P_MAX_INDEX; 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{ if (offset < 0) return P_MAX_INDEX; 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 < 0 || maxPos < 0 || 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){ if (offset < 0) return; 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){ if (len < 0 || pos < 0) return; register PINDEX slen = GetLength(); if (pos >= slen) operator+=(cstr); else { MakeUnique(); PINDEX clen = cstr != NULL ? strlen(cstr) : 0; PINDEX newlen = slen-len+clen; if (clen > len) SetSize(newlen+1); if (pos+len < slen) memmove(theArray+pos+clen, theArray+pos+len, slen-pos-len+1); if (clen > 0) memcpy(theArray+pos, cstr, clen); theArray[newlen] = '\0'; }}PStringArray
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -