diff --git a/src/inc/tutil.h b/src/inc/tutil.h index 901810c44ea7eb43e4f1ef95c8082f692e714366..a9de168fae8a300d3c03c98367b69512276a7071 100644 --- a/src/inc/tutil.h +++ b/src/inc/tutil.h @@ -184,7 +184,7 @@ char *strnchr(char *haystack, char needle, int32_t len, bool skipquote); char **strsplit(char *src, const char *delim, int32_t *num); -void strtolower(char *dst, const char *src); +char* strtolower(char *dst, const char *src); int64_t strnatoi(char *num, int32_t len); diff --git a/src/util/src/tutil.c b/src/util/src/tutil.c index f9bd6aace475f8ec290259c9f1b024795a49549d..d2865ec31dfe6e2b5720b6dbb7066c4b013b1ef9 100644 --- a/src/util/src/tutil.c +++ b/src/util/src/tutil.c @@ -140,28 +140,31 @@ char *strnchr(char *haystack, char needle, int32_t len, bool skipquote) { return NULL; } -void strtolower(char *dst, const char *z) { - int quote = 0; - char *str = z; - if (dst == NULL) { - return; - } - - while (*str) { - if (*str == '\'' || *str == '"') { - quote = quote ^ 1; - } - - if ((!quote) && (*str >= 'A' && *str <= 'Z')) { - *dst++ = *str | 0x20; - } else { - *dst++ = *str; +char* strtolower(char *dst, const char *src) { + int esc = 0; + char quote = 0, *p = dst, c; + + assert(dst != NULL); + + for (c = *src++; c; c = *src++) { + if (esc) { + esc = 0; + } else if (quote) { + if (c == '\\') { + esc = 1; + } else if (c == quote) { + quote = 0; + } + } else if (c >= 'A' && c <= 'Z') { + c -= 'A' - 'a'; + } else if (c == '\'' || c == '"') { + quote = c; } - - str++; + *p++ = c; } - *dst = 0; + *p = 0; + return dst; } char *paGetToken(char *string, char **token, int32_t *tokenLen) {