string: Move string related utility functions to separate files

This commit is contained in:
Martin Willi
2015-04-16 14:49:19 +02:00
parent 03cf888277
commit bbfe7a80b1
6 changed files with 199 additions and 155 deletions
-74
View File
@@ -215,80 +215,6 @@ void *utils_memrchr(const void *s, int c, size_t n)
return NULL;
}
/**
* Described in header.
*/
char* translate(char *str, const char *from, const char *to)
{
char *pos = str;
if (strlen(from) != strlen(to))
{
return str;
}
while (pos && *pos)
{
char *match;
if ((match = strchr(from, *pos)) != NULL)
{
*pos = to[match - from];
}
pos++;
}
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;
}
#ifdef WIN32
/**