utils: Add strreplace function

This commit is contained in:
Tobias Brunner
2014-01-23 10:18:23 +01:00
parent f44b1eb444
commit ccb6758e5b
3 changed files with 155 additions and 2 deletions
+53 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2008-2012 Tobias Brunner
* Copyright (C) 2008-2013 Tobias Brunner
* Copyright (C) 2005-2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
@@ -141,6 +141,58 @@ char* translate(char *str, const char *from, const char *to)
return str;
}
/**
* Described in header.
*/
char* strreplace(const char *str, const char *search, const char *replace)
{
size_t len, slen, rlen, count = 0;
char *res, *pos, *found, *dst;
if (!str || !*str || !search || !*search || !replace)
{
return (char*)str;
}
slen = strlen(search);
rlen = strlen(replace);
if (slen != rlen)
{
for (pos = (char*)str; (pos = strstr(pos, search)); pos += slen)
{
found = pos;
count++;
}
if (!count)
{
return (char*)str;
}
len = (found - str) + strlen(found) + count * (rlen - slen);
}
else
{
len = strlen(str);
}
found = strstr(str, search);
if (!found)
{
return (char*)str;
}
dst = res = malloc(len + 1);
pos = (char*)str;
do
{
len = found - pos;
memcpy(dst, pos, len);
dst += len;
memcpy(dst, replace, rlen);
dst += rlen;
pos = found + slen;
}
while ((found = strstr(pos, search)));
strcpy(dst, pos);
return res;
}
/**
* Described in header.
*/