Allow 0x/0o/0b integer prefixed in accordance with https://github.com/toml-lang/toml#integer

This commit is contained in:
Jesper Peterson 2019-05-10 17:17:29 +10:00
parent bd76f1276e
commit 386dccc84f
1 changed files with 11 additions and 4 deletions

11
toml.c
View File

@ -1788,6 +1788,7 @@ int toml_rtoi(const char* src, int64_t* ret_)
char* p = buf; char* p = buf;
char* q = p + sizeof(buf); char* q = p + sizeof(buf);
const char* s = src; const char* s = src;
int base = 0;
int64_t dummy; int64_t dummy;
int64_t* ret = ret_ ? ret_ : &dummy; int64_t* ret = ret_ ? ret_ : &dummy;
@ -1798,9 +1799,15 @@ int toml_rtoi(const char* src, int64_t* ret_)
/* if 0 ... */ /* if 0 ... */
if ('0' == s[0]) { if ('0' == s[0]) {
switch (s[1]) {
case 'x': base = 16; s += 2; break;
case 'o': base = 8; s += 2; break;
case 'b': base = 2; s += 2; break;
case '\0': return *ret = 0, 0;
default:
/* ensure no other digits after it */ /* ensure no other digits after it */
if (s[1]) return -1; if (s[1]) return -1;
return *ret = 0, 0; }
} }
/* just strip underscores and pass to strtoll */ /* just strip underscores and pass to strtoll */
@ -1816,7 +1823,7 @@ int toml_rtoi(const char* src, int64_t* ret_)
/* Run strtoll on buf to get the integer */ /* Run strtoll on buf to get the integer */
char* endp; char* endp;
errno = 0; errno = 0;
*ret = strtoll(buf, &endp, 0); *ret = strtoll(buf, &endp, base);
return (errno || *endp) ? -1 : 0; return (errno || *endp) ? -1 : 0;
} }