Merge pull request #16 from obloquy/master

Allow 0x/0o/0b integer prefixes
This commit is contained in:
CK Tan 2019-05-30 18:24:12 -07:00 committed by GitHub
commit e93b40c3ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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;
} }