Q_strdel and Q_strins utility functions

This commit is contained in:
BjossiAlfreds 2024-07-31 21:40:11 +00:00
parent 4b3545697e
commit 29eb6a601f
2 changed files with 53 additions and 0 deletions

View file

@ -329,6 +329,13 @@ char *Q_strlwr(char *s);
int Q_strlcpy(char *dst, const char *src, int size);
int Q_strlcat(char *dst, const char *src, int size);
/* Delete n characters from s starting at index i */
void Q_strdel(char *s, size_t i, size_t n);
/* Insert src into dest starting at index i, total, n is the total size of the buffer */
/* Returns length of src on success, 0 if there is not enough space in dest for src */
size_t Q_strins(char *dest, const char *src, size_t i, size_t n);
/* ============================================= */
/* Unicode wrappers that also make sure it's a regular file around fopen(). */

View file

@ -1144,6 +1144,52 @@ Q_strlcat(char *dst, const char *src, int size)
return (d - dst) + Q_strlcpy(d, src, size);
}
void
Q_strdel(char *s, size_t i, size_t n)
{
size_t len;
if (!n)
{
return;
}
len = strlen(s);
if (i >= len || n > (len - i))
{
return;
}
memmove(s + i, s + i + n, len - i);
s[len - n] = '\0';
}
size_t
Q_strins(char *dest, const char *src, size_t i, size_t n)
{
size_t dlen;
size_t slen;
if (!src || *src == '\0')
{
return 0;
}
slen = strlen(src);
dlen = strlen(dest);
if (i > dlen || (dlen + slen + 1) > n)
{
return 0;
}
memmove(dest + i + slen, dest + i, dlen - i + 1);
memcpy(dest + i, src, slen);
return slen;
}
/*
* An unicode compatible fopen() Wrapper for Windows.
*/