Exemplo n.º 1
0
unsigned char String::reserve(unsigned int size){
  if (buffer && capacity >= size) return 1;
  if (changeBuffer(size)) {
    if (len == 0) buffer[0] = 0;
    return 1;
  }
  return 0;
}
Exemplo n.º 2
0
void String::replace(const String &find, const String &replace)
{
    if ( (len == 0) || (find.len == 0) ) {
        return;
    }
    int  diff      = replace.len - find.len;
    char *readFrom = buffer;
    char *foundAt;
    if ( diff == 0 ) {
        while ( ( foundAt = strstr(readFrom, find.buffer) ) != NULL ) {
            memcpy(foundAt, replace.buffer, replace.len);
            readFrom = foundAt + replace.len;
        }
    } else if ( diff < 0 ) {
        char *writeTo = buffer;
        while ( ( foundAt = strstr(readFrom, find.buffer) ) != NULL ) {
            unsigned int n = foundAt - readFrom;
            memcpy(writeTo, readFrom,       n);
            writeTo += n;
            memcpy(writeTo, replace.buffer, replace.len);
            writeTo += replace.len;
            readFrom = foundAt + find.len;
            len     += diff;
        }
        strcpy(writeTo, readFrom);
    } else {
        unsigned int size = len; // compute size needed for result
        while ( ( foundAt = strstr(readFrom, find.buffer) ) != NULL ) {
            readFrom = foundAt + find.len;
            size    += diff;
        }
        if ( size == len ) {
            return;
        }
        if ( (size > capacity) && !changeBuffer(size) ) {
            return; // XXX: tell user!
        }
        int index = len - 1;
        while ( index >= 0 && ( index = lastIndexOf(find, index) ) >= 0 ) {
            readFrom = buffer + index + find.len;
            memmove( readFrom + diff, readFrom, len - (readFrom - buffer) );
            len        += diff;
            buffer[len] = 0;
            memcpy(buffer + index, replace.buffer, replace.len);
            index--;
        }
    }
}
Exemplo n.º 3
0
void ExternalInputSource::reallocateBuffer(size_t newSize)
{
  Char *newBuf = new Char[newSize];
  
  memcpy(newBuf, buf_, bufSize_*sizeof(Char));
  bufSize_ = newSize;
  changeBuffer(newBuf, buf_);
  bufLim_ = newBuf + (bufLim_ - buf_);
  if (nLeftOver_ > 0) {
    char *s = (char *)(newBuf + bufSize_) - nLeftOver_;
    memmove(s,
	    newBuf + (leftOver_ - (char *)buf_),
	    nLeftOver_);
    leftOver_ = s;
  }
  delete [] buf_;
  buf_ = newBuf;
}