1
0
mirror of https://github.com/janet-lang/janet synced 2025-01-13 00:50:26 +00:00

Add optional base to scan-number.

This commit is contained in:
Calvin Rose 2021-10-10 09:07:56 -05:00
parent 3e5bd460a5
commit 684f3ac172
2 changed files with 11 additions and 9 deletions

View File

@ -398,15 +398,21 @@ JANET_CORE_FN(janet_core_is_abstract,
}
JANET_CORE_FN(janet_core_scannumber,
"(scan-number str)",
"(scan-number str &opt base)",
"Parse a number from a byte sequence an return that number, either and integer "
"or a real. The number "
"must be in the same format as numbers in janet source code. Will return nil "
"on an invalid number.") {
"on an invalid number. Optionally provide a base - if a base is provided, no "
"radix specifier is expected at the beginning of the number.") {
double number;
janet_fixarity(argc, 1);
janet_arity(argc, 1, 2);
JanetByteView view = janet_getbytes(argv, 0);
if (janet_scan_number(view.bytes, view.len, &number))
int32_t base = janet_optinteger(argv, argc, 1, 0);
int valid = base == 0 || (base >= 2 && base <= 36);
if (!valid) {
janet_panicf("expected base between 2 and 36, got %d", base);
}
if (janet_scan_number_base(view.bytes, view.len, base, &number))
return janet_wrap_nil();
return janet_wrap_number(number);
}

View File

@ -81,11 +81,7 @@ int janet_dobytes(JanetTable *env, const uint8_t *bytes, int32_t len, const char
ret = janet_cstringv(e);
int32_t line = parser.line;
int32_t col = parser.column;
if (line > 0 && col > 0) {
janet_eprintf("%s:%d:%d: parse error: %s\n", sourcePath, line, col, e);
} else {
janet_eprintf("%s: parse error: %s\n", sourcePath, e);
}
janet_eprintf("%s:%d:%d: parse error: %s\n", sourcePath, line, col, e);
done = 1;
break;
}