size_t DwString::find_last_of(const char* aBuf, size_t aPos, size_t aLen) const { assert(aBuf != 0); if (aBuf == 0) return (size_t)-1; if (mLength == 0) return (size_t)-1; size_t pos = DW_MIN(aPos, mLength - 1); if (aLen == 0) return pos; char table[256]; memset(table, 0, sizeof(table)); for (size_t j=0; j < aLen; ++j) { table[aBuf[j]&0xff] = 1; } const char* buf = mRep->mBuffer + mStart; for (size_t k=0; k <= pos; ++k) { size_t i = pos - k; if (table[buf[i]&0xff]) return i; } return (size_t)-1; }
size_t DwString::rfind(const char *aBuf, size_t aPos, size_t aLen) const { assert(aBuf != 0); if(aBuf == 0) return (size_t) - 1; if(aLen > mLength) return (size_t) - 1; size_t pos = DW_MIN(aPos, mLength - aLen); if(aLen == 0) return pos; const char *buf = mRep->mBuffer + mStart; for(size_t i = 0; i <= pos; ++i) { size_t k = pos - i; size_t j = 0; while(j < aLen && aBuf[j] == buf[k]) { ++j; ++k; } if(j == aLen) return pos - i; } return (size_t) - 1; }
static int dw_strcmp(const char* s1, size_t len1, const char* s2, size_t len2) { assert(s1 != 0); assert(s2 != 0); size_t len = DW_MIN(len1, len2); for (size_t i=0; i < len; ++i) { if (s1[i] < s2[i]) { return -1; } else if (s1[i] > s2[i]) { return 1; } } if (len1 < len2) { return -1; } else if (len1 > len2) { return 1; } return 0; }
static int dw_strasciicasecmp(const char* s1, size_t len1, const char* s2, size_t len2) { assert(s1 != 0); assert(s2 != 0); size_t len = DW_MIN(len1, len2); for (size_t i=0; i < len; ++i) { int c1 = dw_asciitolower( s1[i] ); int c2 = dw_asciitolower( s2[i] ); if ( c1 < c2 ) return -1; else if ( c1 > c2 ) return 1; } if (len1 < len2) { return -1; } else if (len1 > len2) { return 1; } return 0; }
void DwString::_replace(size_t aPos1, size_t aLen1, size_t aLen2, char aChar) { assert(aPos1 <= mLength); size_t pos1 = DW_MIN(aPos1, mLength); size_t len1 = DW_MIN(aLen1, mLength - pos1); assert(mStart + mLength - len1 < ((size_t) - 1) - aLen2); size_t len2 = DW_MIN(aLen2, ((size_t) - 1) - (mStart + mLength - len1)); size_t i; char *to; const char *from; size_t newLen = mLength - len1 + len2; // Is new string empty? if(newLen == 0) { if(mRep != sEmptyRep) { delete_rep_safely(mRep); mRep = new_rep_reference(sEmptyRep); mStart = 0; mLength = 0; } } // Is buffer shared? Is buffer too small? else if(mRep->mRefCount > 1 || newLen >= mRep->mSize) { size_t size = newLen + 1; char *newBuf = mem_alloc(&size); assert(newBuf != 0); if(newBuf != 0) { to = newBuf; from = mRep->mBuffer + mStart; for(i = 0; i < pos1; ++i) *to++ = *from++; for(i = 0; i < len2; ++i) *to++ = aChar; from = mRep->mBuffer + mStart + pos1 + len1; for(i = 0; i < mLength - pos1 - len1; ++i) *to++ = *from++; *to = 0; DwStringRep *rep = new DwStringRep(newBuf, size); assert(rep != 0); if(rep != 0) { delete_rep_safely(mRep); mRep = rep; mStart = 0; mLength = newLen; } } } // Is the replacement smaller than the replaced? else if(len2 < len1) { to = mRep->mBuffer + mStart + pos1; for(i = 0; i < len2; ++i) *to++ = aChar; from = mRep->mBuffer + mStart + pos1 + len1; for(i = 0; i < mLength - pos1 - len1; ++i) *to++ = *from++; *to = 0; mLength = newLen; } // Is there enough room at end of buffer? else if(mStart + newLen < mRep->mSize) { to = mRep->mBuffer + mStart + newLen; from = mRep->mBuffer + mStart + mLength - 1; *to-- = 0; for(i = 0; i < mLength - pos1 - len1; ++i) *to-- = *from--; for(i = 0; i < len2; ++i) *to-- = aChar; mLength = newLen; } // Is there enough room at beginning of buffer? else if(len2 - len1 <= mStart) { to = mRep->mBuffer + mStart - (len2 - len1); from = mRep->mBuffer + mStart; for(i = 0; i < pos1; ++i) *to++ = *from++; for(i = 0; i < len2; ++i) *to++ = aChar; mStart -= len2 - len1; mLength = newLen; } // There's enough room, but we must move characters. else { to = mRep->mBuffer + newLen; from = mRep->mBuffer + mStart + mLength - 1; *to-- = 0; for(i = 0; i < mLength - pos1 - len1; ++i) *to-- = *from--; to = mRep->mBuffer; from = mRep->mBuffer + mStart; for(i = 0; i < pos1; ++i) *to++ = *from++; for(i = 0; i < len2; ++i) *to++ = aChar; mStart = 0; mLength = newLen; } }