diff --git a/m-string.h b/m-string.h index 8ea382da..8aad69d8 100644 --- a/m-string.h +++ b/m-string.h @@ -891,17 +891,18 @@ string_in_str(string_t v, FILE *f) } static inline bool -string_parse_str(string_t v, const char **str) +string_parse_str(string_t v, const char str[], const char **endptr) { STRINGI_CONTRACT(v); M_ASSUME (str != NULL); - int c = *((*str)++); - if (c != '"') return false; + bool success = false; + int c = *str++; + if (c != '"') goto exit; string_clean(v); - c = *((*str)++); + c = *str++; while (c != '"' && c != 0) { if (M_UNLIKELY (c == '\\')) { - c = *((*str)++); + c = *str++; switch (c) { case 'n': case 't': @@ -914,24 +915,27 @@ string_parse_str(string_t v, const char **str) break; default: if (!(c >= '0' && c <= '7')) - return false; + goto exit; int d1 = c - '0'; - c = *((*str)++); + c = *str++; if (!(c >= '0' && c <= '7')) - return false; + goto exit; int d2 = c - '0'; - c = *((*str)++); + c = *str++; if (!(c >= '0' && c <= '7')) - return false; + goto exit; int d3 = c - '0'; c = (d1 << 6) + (d2 << 3) + d3; break; } } string_push_back (v, c); - c = *((*str)++); + c = *str++; } - return c == '"'; + success = (c == '"'); + exit: + if (endptr != NULL) *endptr = str; + return success; } /* UTF8 Handling */ diff --git a/tests/test-mstring.c b/tests/test-mstring.c index 2a7337a4..6f0e0a70 100644 --- a/tests/test-mstring.c +++ b/tests/test-mstring.c @@ -289,11 +289,11 @@ static void test0(void) assert (string_cmp_str (s2, "\"Hello world\"\"Hello \\\"world\\\"\"") == 0); const char *sp = string_get_cstr(s2); - b = string_parse_str(s1, &sp); + b = string_parse_str(s1, sp, &sp); assert (b == true); assert (strcmp(sp, "\"Hello \\\"world\\\"\"") == 0); assert (string_cmp_str(s1, "Hello world") == 0); - b = string_parse_str(s1, &sp); + b = string_parse_str(s1, sp, &sp); assert (b == true); assert (strcmp(sp, "") == 0); assert (string_cmp_str(s1, "Hello \"world\"") == 0);