Ejemplo n.º 1
0
void SimpleString::replace(const char* to, const char* with)
{
    int c = count(to);
    int len = size();
    int tolen = PlatformSpecificStrLen(to);
    int withlen = PlatformSpecificStrLen(with);

    int newsize = len + (withlen * c) - (tolen * c) + 1;

    if (newsize) {
        char* newbuf = allocString(newsize);
        for (int i = 0, j = 0; i < len;) {
            if (PlatformSpecificStrNCmp(&buffer[i], to, tolen) == 0) {
                PlatformSpecificStrNCpy(&newbuf[j], with, withlen);
                j += withlen;
                i += tolen;
            }
            else {
                newbuf[j] = buffer[i];
                j++;
                i++;
            }
        }
        deallocString(buffer);
        buffer = newbuf;
        buffer[newsize-1] = '\0';
    }
    else {
        buffer = getEmptryString();
        buffer [0] = '\0';
    }
}
Ejemplo n.º 2
0
void SimpleString::replace(const char* to, const char* with)
{
	size_t c = count(to);
	size_t len = size();
	size_t tolen = PlatformSpecificStrLen(to);
	size_t withlen = PlatformSpecificStrLen(with);

	size_t newsize = len + (withlen * c) - (tolen * c) + 1;

	if (newsize) {
		char* newbuf = allocStringBuffer(newsize);
		for (size_t i = 0, j = 0; i < len;) {
			if (PlatformSpecificStrNCmp(&buffer_[i], to, tolen) == 0) {
				StrNCpy(&newbuf[j], with, withlen + 1);
				j += withlen;
				i += tolen;
			}
			else {
				newbuf[j] = buffer_[i];
				j++;
				i++;
			}
		}
		deallocStringBuffer(buffer_);
		buffer_ = newbuf;
		buffer_[newsize - 1] = '\0';
	}
	else {
		buffer_ = getEmptyString();
		buffer_[0] = '\0';
	}
}