Rewrite parse_str interface to be more like strol

This commit is contained in:
Patrick Pelissier committed 2018-05-03 12:59:10 +02:00
1 parent b1597f6f61
commit 2ad8ae3451
2 files changed
+18 -14

No files matched your search

+16 -12
View File
@@ -891,17 +891,18 @@ string_in_str(string_t v, FILE *f)
} }
static inline bool 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); STRINGI_CONTRACT(v);
M_ASSUME (str != NULL); M_ASSUME (str != NULL);
int c = *((*str)++); bool success = false;
if (c != '"') return false; int c = *str++;
if (c != '"') goto exit;
string_clean(v); string_clean(v);
c = *((*str)++); c = *str++;
while (c != '"' && c != 0) { while (c != '"' && c != 0) {
if (M_UNLIKELY (c == '\\')) { if (M_UNLIKELY (c == '\\')) {
c = *((*str)++); c = *str++;
switch (c) { switch (c) {
case 'n': case 'n':
case 't': case 't':
@@ -914,24 +915,27 @@ string_parse_str(string_t v, const char **str)
break; break;
default: default:
if (!(c >= '0' && c <= '7')) if (!(c >= '0' && c <= '7'))
return false; goto exit;
int d1 = c - '0'; int d1 = c - '0';
c = *((*str)++); c = *str++;
if (!(c >= '0' && c <= '7')) if (!(c >= '0' && c <= '7'))
return false; goto exit;
int d2 = c - '0'; int d2 = c - '0';
c = *((*str)++); c = *str++;
if (!(c >= '0' && c <= '7')) if (!(c >= '0' && c <= '7'))
return false; goto exit;
int d3 = c - '0'; int d3 = c - '0';
c = (d1 << 6) + (d2 << 3) + d3; c = (d1 << 6) + (d2 << 3) + d3;
break; break;
} }
} }
string_push_back (v, c); string_push_back (v, c);
c = *((*str)++); c = *str++;
} }
return c == '"'; success = (c == '"');
exit:
if (endptr != NULL) *endptr = str;
return success;
} }
/* UTF8 Handling */ /* UTF8 Handling */
+2 -2
View File
@@ -289,11 +289,11 @@ static void test0(void)
assert (string_cmp_str (s2, "\"Hello world\"\"Hello \\\"world\\\"\"") == 0); assert (string_cmp_str (s2, "\"Hello world\"\"Hello \\\"world\\\"\"") == 0);
const char *sp = string_get_cstr(s2); const char *sp = string_get_cstr(s2);
b = string_parse_str(s1, &sp); b = string_parse_str(s1, sp, &sp);
assert (b == true); assert (b == true);
assert (strcmp(sp, "\"Hello \\\"world\\\"\"") == 0); assert (strcmp(sp, "\"Hello \\\"world\\\"\"") == 0);
assert (string_cmp_str(s1, "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 (b == true);
assert (strcmp(sp, "") == 0); assert (strcmp(sp, "") == 0);
assert (string_cmp_str(s1, "Hello \"world\"") == 0); assert (string_cmp_str(s1, "Hello \"world\"") == 0);