refactor(util): update wcsreplace()
* POSSIBILITY OF SUCH DAMAGE.
*/
-
#include <stdlib.h>
#include <string.h>
#include <errno.h>
@@ -144,27 +143,29 @@ int wcstrim(wchar_t *outstr, const wchar_t *instr, const wchar_t *charlist)
STRTRIM_CODE(wchar_t, outstr, instr, charlist, L"\t\n\r ");
}
-int wcsreplace(wchar_t *str, size_t max_len,
- const wchar_t *substr, const wchar_t *newstr)
+size_t wcsreplace(wchar_t *str, size_t max_len, const wchar_t *substr,
+ const wchar_t *newstr)
{
size_t len, buf_len;
- wchar_t *buf, *p, *q;
- len = wcslen(newstr);
- p = wcsstr(str, substr);
- if (!p) {
+ wchar_t *buf, *pout, *pin;
+
+ pin = wcsstr(str, substr);
+ if (!pin) {
return 0;
}
- buf_len = wcslen(str) + len;
+ len = wcslen(newstr);
+ buf_len = wcslen(str) + len + 1;
buf = malloc(buf_len * sizeof(wchar_t));
wcscpy(buf, str);
- q = buf + (p - str);
- wcscpy(q, newstr);
- p += wcslen(substr);
- q += len;
- wcscpy(q, p);
+ pout = buf + (pin - str);
+ wcscpy(pout, newstr);
+ pin += wcslen(substr);
+ pout += len;
+ wcscpy(pout, pin);
+ buf[buf_len - 1] = 0;
wcsncpy(str, buf, max_len);
free(buf);
- return 1;
+ return min(buf_len, max_len);
}
void freestrs(char **strs)
@@ -375,7 +376,8 @@ static int strsdelone(char ***strlist, const char *str)
if (!*strlist) {
return 0;
}
- for (len = 0; (*strlist)[len]; ++len);
+ for (len = 0; (*strlist)[len]; ++len)
+ ;
for (pos = -1, i = 0; i < len; ++i) {
if (strcmp((*strlist)[i], str) == 0) {
pos = i;
@@ -435,7 +437,8 @@ int sortedstrsadd(char ***strlist, const char *str)
char **newlist, *newstr;
if (*strlist) {
- for (i = 0; (*strlist)[i]; ++i);
+ for (i = 0; (*strlist)[i]; ++i)
+ ;
n = i + 2;
} else {
n = 2;
