首页 > 代码库 > clang 简单的str_replace实现

clang 简单的str_replace实现

自己写的一段:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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