diff --git a/src/common/header/shared.h b/src/common/header/shared.h index a15a2340..f0755955 100644 --- a/src/common/header/shared.h +++ b/src/common/header/shared.h @@ -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(). */ diff --git a/src/common/shared/shared.c b/src/common/shared/shared.c index 33a1e2e1..792ffde8 100644 --- a/src/common/shared/shared.c +++ b/src/common/shared/shared.c @@ -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. */