restructure and clean up toml_rtos

This commit is contained in:
CK Tan 2019-12-29 11:55:17 -08:00
parent 416be8de63
commit 4078d23a33
1 changed files with 27 additions and 41 deletions

50
toml.c
View File

@ -2093,22 +2093,26 @@ int toml_rtos(const char* src, char** ret)
const char* sp; const char* sp;
const char* sq; const char* sq;
if (!src) return -1;
if (*src != '\'' && *src != '"') return -1;
*ret = 0; *ret = 0;
if (!src) return -1;
int qchar = src[0];
int srclen = strlen(src); int srclen = strlen(src);
if (*src == '\'') { if (! (qchar == '\'' || qchar == '"')) {
if (0 == strncmp(src, "'''", 3)) { return -1;
}
// triple quotes?
if (qchar == src[1] && qchar == src[2]) {
multiline = 1; multiline = 1;
sp = src + 3; sp = src + 3;
sq = src + srclen - 3; sq = src + srclen - 3;
/* last 3 chars in src must be ''' */ /* last 3 chars in src must be qchar */
if (! (sp <= sq && 0 == strcmp(sq, "'''"))) if (! (sp <= sq && sq[0] == qchar && sq[1] == qchar && sq[2] == qchar))
return -1; return -1;
/* skip first new line right after ''' */ /* skip new line immediate after qchar */
if (*sp == '\n') if (sp[0] == '\n')
sp++; sp++;
else if (sp[0] == '\r' && sp[1] == '\n') else if (sp[0] == '\r' && sp[1] == '\n')
sp += 2; sp += 2;
@ -2116,38 +2120,20 @@ int toml_rtos(const char* src, char** ret)
} else { } else {
sp = src + 1; sp = src + 1;
sq = src + srclen - 1; sq = src + srclen - 1;
/* last char in src must be ' */ /* last char in src must be qchar */
if (! (sp <= sq && *sq == '\'')) if (! (sp <= sq && *sq == qchar))
return -1; return -1;
} }
if (qchar == '\'') {
*ret = norm_lit_str(sp, sq - sp, *ret = norm_lit_str(sp, sq - sp,
multiline, multiline,
0, 0); 0, 0);
return *ret ? 0 : -1;
}
if (0 == strncmp(src, "\"\"\"", 3)) {
multiline = 1;
sp = src + 3;
sq = src + srclen - 3;
if (! (sp <= sq && 0 == strcmp(sq, "\"\"\"")))
return -1;
/* skip first new line right after """ */
if (*sp == '\n')
sp++;
else if (sp[0] == '\r' && sp[1] == '\n')
sp += 2;
} else { } else {
sp = src + 1;
sq = src + srclen - 1;
if (! (sp <= sq && *sq == '"'))
return -1;
}
*ret = norm_basic_str(sp, sq - sp, *ret = norm_basic_str(sp, sq - sp,
multiline, multiline,
0, 0); 0, 0);
}
return *ret ? 0 : -1; return *ret ? 0 : -1;
} }