59 {
60 const char *tmp =
str;
62 int found = 0;
63 int64_t length, reslen;
64 size_t oldlen = strlen(oldstr);
65 size_t newlen = strlen(newstr);
66 int limit = (
count != NULL && *
count > 0) ? *count : -1;
67
69 while ((tmp = strstr(tmp, oldstr)) != NULL && found != limit)
70 found++, tmp += oldlen;
71
72 length = strlen(
str) + found * (newlen - oldlen);
73 if ((
result = (
char *) palloc(length + 1)) == NULL) {
74 fprintf(stderr, "Not enough memory\n");
75 found = -1;
76 }
77 else {
79 limit = found;
80 reslen = 0;
81
82
83 while ((limit-- > 0) && (tmp = strstr(tmp, oldstr)) != NULL) {
86 strcpy(
result + (reslen += length), newstr);
87
88 reslen += newlen;
89 tmp += oldlen;
91 }
93 }
94
95 if (count != NULL) *
count = found;
97}
char result[OUT_DOUBLE_BUFFER_SIZE]