clang 简单的str_replace实现

自己写的一段:

//gool
char* str_replace(char* source, const  char* find, const char* replace){

	if (source == NULL || find == NULL || find == "")
		return strdup(source);
	
	int matchCount = 0;
	int nowIndex = 0;
	int findLength = strlen(find);
	int replaceLength = strlen(replace);
	int sourceLength = strlen(source);
	int resultLength = sourceLength;
	char* result;
	int i;
	for ( i = 0; i < sourceLength; i++)
	{
		if (nowIndex < findLength && source[i] == find[nowIndex]){
			nowIndex++;
			matchCount++;
		}
		else
		{
			if (matchCount == findLength)
			{
				source[i - 1] = '\0';
				nowIndex = 0;
				matchCount = 0;
				resultLength -= findLength - replaceLength;
			}
		}
	}

	nowIndex = 0;
	matchCount = 0;

	result = (char*)malloc(2 * (resultLength + 1));

	for ( i = 0; i < sourceLength; i++)
	{
		if (source[i] == '\0'){
			source[i] = find[0];
			while (matchCount < replaceLength)
			{
				*(result + nowIndex + matchCount) = replace[matchCount];
				matchCount++;
			}
			matchCount = 0;
			nowIndex += replaceLength;
			i += findLength -1;
		}
		else
		{
			*(result + nowIndex) = source[i];
			nowIndex++;
		}
	}
	*(result + nowIndex + 1) = '\0';
	return result;
}

  http://blog.csdn.net/glmmmm/article/details/9930969:

char *str_replace_2(const char *string, const char *substr, const char *replacement)
{
	char *tok = NULL;
	char *newstr = NULL;
	char *oldstr = NULL;

	/* if either substr or replacement is NULL, duplicate string a let caller handle it */
	if (substr == NULL || replacement == NULL)
		return strdup(string);

	newstr = strdup(string);
	while ((tok = strstr(newstr, substr)))
	{
		oldstr = newstr;
		newstr = (char*)malloc(strlen(oldstr) - strlen(substr) + strlen(replacement) + 1);
		/*failed to alloc mem, free old string and return NULL */
		if (newstr == NULL)
		{
			free(oldstr);
			return NULL;
		}
		memcpy(newstr, oldstr, tok - oldstr);
		memcpy(newstr + (tok - oldstr), replacement, strlen(replacement));
		memcpy(newstr + (tok - oldstr) + strlen(replacement), tok + strlen(substr), strlen(oldstr) - strlen(substr) - (tok - oldstr));
		memset(newstr + strlen(oldstr) - strlen(substr) + strlen(replacement), 0, 1);

		free(oldstr);
	}

	return newstr;
}

http://stackoverflow.com/questions/779875/what-is-the-function-to-replace-string-in-c:

char *str_replace_3(char *orig, char *rep, char *with) {
	char *result; // the return string
	char *ins;    // the next insert point
	char *tmp;    // varies
	int len_rep;  // length of rep
	int len_with; // length of with
	int len_front; // distance between rep and end of last rep
	int count;    // number of replacements

	if (!orig)
		return NULL;
	if (!rep)
		rep = "";
	len_rep = strlen(rep);
	if (!with)
		with = "";
	len_with = strlen(with);

	ins = orig;
	for (count = 0; tmp = strstr(ins, rep); ++count) {
		ins = tmp + len_rep;
	}

	// first time through the loop, all the variable are set correctly
	// from here on,
	//    tmp points to the end of the result string
	//    ins points to the next occurrence of rep in orig
	//    orig points to the remainder of orig after "end of rep"
	tmp = result = (char*)malloc(strlen(orig) + (len_with - len_rep) * count + 1);

	if (!result)
		return NULL;

	while (count--) {
		ins = strstr(orig, rep);
		len_front = ins - orig;
		tmp = strncpy(tmp, orig, len_front) + len_front;
		tmp = strcpy(tmp, with) + len_with;
		orig += len_front + len_rep; // move to next "end of rep"
	}
	strcpy(tmp, orig);
	return result;
}

  

平均测速:1. 84 clock/100000,2. 195 clock/100000,3.89 clock/100000

posted @ 2014-05-11 05:07  幻影gool  阅读(580)  评论(0编辑  收藏  举报