diff options
Diffstat (limited to 'source/blender/blenlib/intern/string.c')
-rw-r--r-- | source/blender/blenlib/intern/string.c | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index 8501db7c8b8..ff589764287 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -41,6 +41,8 @@ #include "BLI_dynstr.h" #include "BLI_string.h" +#include "BLI_utildefines.h" + char *BLI_strdupn(const char *str, const size_t len) { char *n = MEM_mallocN(len + 1, "strdup"); @@ -71,6 +73,7 @@ char *BLI_strncpy(char *dst, const char *src, const size_t maxncpy) { size_t srclen = strlen(src); size_t cpylen = (srclen > (maxncpy - 1)) ? (maxncpy - 1) : srclen; + BLI_assert(maxncpy != 0); memcpy(dst, src, cpylen); dst[cpylen] = '\0'; @@ -130,10 +133,13 @@ char *BLI_sprintfN(const char *format, ...) * TODO: support more fancy string escaping. current code is primitive * this basically is an ascii version of PyUnicode_EncodeUnicodeEscape() * which is a useful reference. */ -size_t BLI_strescape(char *dst, const char *src, const size_t maxlen) +size_t BLI_strescape(char *dst, const char *src, const size_t maxncpy) { size_t len = 0; - while (len < maxlen) { + + BLI_assert(maxncpy != 0); + + while (len < maxncpy) { switch (*src) { case '\0': goto escape_finish; @@ -144,7 +150,7 @@ size_t BLI_strescape(char *dst, const char *src, const size_t maxlen) case '\t': case '\n': case '\r': - if (len + 1 < maxlen) { + if (len + 1 < maxncpy) { *dst++ = '\\'; len++; } @@ -296,11 +302,12 @@ char *BLI_strcasestr(const char *s, const char *find) int BLI_strcasecmp(const char *s1, const char *s2) { - int i; + register int i; + register char c1, c2; for (i = 0;; i++) { - char c1 = tolower(s1[i]); - char c2 = tolower(s2[i]); + c1 = tolower(s1[i]); + c2 = tolower(s2[i]); if (c1 < c2) { return -1; @@ -318,11 +325,12 @@ int BLI_strcasecmp(const char *s1, const char *s2) int BLI_strncasecmp(const char *s1, const char *s2, size_t len) { - int i; + register size_t i; + register char c1, c2; for (i = 0; i < len; i++) { - char c1 = tolower(s1[i]); - char c2 = tolower(s2[i]); + c1 = tolower(s1[i]); + c2 = tolower(s2[i]); if (c1 < c2) { return -1; @@ -341,15 +349,16 @@ int BLI_strncasecmp(const char *s1, const char *s2, size_t len) /* natural string compare, keeping numbers in order */ int BLI_natstrcmp(const char *s1, const char *s2) { - int d1 = 0, d2 = 0; + register int d1 = 0, d2 = 0; + register char c1, c2; /* if both chars are numeric, to a strtol(). * then increase string deltas as long they are * numeric, else do a tolower and char compare */ while (1) { - char c1 = tolower(s1[d1]); - char c2 = tolower(s2[d2]); + c1 = tolower(s1[d1]); + c2 = tolower(s2[d2]); if (isdigit(c1) && isdigit(c2) ) { int val1, val2; @@ -374,7 +383,7 @@ int BLI_natstrcmp(const char *s1, const char *s2) c2 = tolower(s2[d2]); } - /* first check for '.' so "foo.bar" comes before "foo 1.bar" */ + /* first check for '.' so "foo.bar" comes before "foo 1.bar" */ if (c1 == '.' && c2 != '.') return -1; if (c1 != '.' && c2 == '.') @@ -413,27 +422,26 @@ void BLI_timestr(double _time, char *str) } /* determine the length of a fixed-size string */ -size_t BLI_strnlen(const char *str, size_t maxlen) +size_t BLI_strnlen(const char *str, const size_t maxlen) { const char *end = memchr(str, '\0', maxlen); return end ? (size_t) (end - str) : maxlen; } -void BLI_ascii_strtolower(char *str, int len) +void BLI_ascii_strtolower(char *str, const size_t len) { - int i; + size_t i; for (i = 0; i < len; i++) if (str[i] >= 'A' && str[i] <= 'Z') str[i] += 'a' - 'A'; } -void BLI_ascii_strtoupper(char *str, int len) +void BLI_ascii_strtoupper(char *str, const size_t len) { - int i; + size_t i; for (i = 0; i < len; i++) if (str[i] >= 'a' && str[i] <= 'z') str[i] -= 'a' - 'A'; } - |