1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 09:47:17 +00:00

bigint pretty printing

This commit is contained in:
J.-F. Cap 2019-03-13 01:28:26 +01:00 committed by Calvin Rose
parent 9bc899ccf2
commit 42a0af3b1b
4 changed files with 131 additions and 85 deletions

View File

@ -143,6 +143,13 @@ static Janet make_bn_uint64(Janet x) {
int janet_is_bigint(Janet x, JanetBigintType type) {
return janet_checktype(x, JANET_ABSTRACT) &&
(((type == JANET_BIGINT_TYPE_int64) && (janet_abstract_type(janet_unwrap_abstract(x)) == &bn_int64_type)) ||
((type == JANET_BIGINT_TYPE_uint64) && (janet_abstract_type(janet_unwrap_abstract(x)) == &bn_uint64_type)));
}
static Janet cfun_bn_int64_new(int32_t argc, Janet *argv) {

View File

@ -80,6 +80,22 @@ static void integer_to_string_b(JanetBuffer *buffer, int32_t x) {
buffer->count += len + neg;
}
#ifdef JANET_BIGINT
static void uint64_to_string_b(JanetBuffer *buffer, uint64_t x) {
janet_buffer_ensure(buffer, buffer->count + BUFSIZE, 2);
int count = snprintf((char *) buffer->data + buffer->count, BUFSIZE, "%lu", x);
buffer->count += count;
}
static void int64_to_string_b(JanetBuffer *buffer, int64_t x) {
janet_buffer_ensure(buffer, buffer->count + BUFSIZE, 2);
int count = snprintf((char *) buffer->data + buffer->count, BUFSIZE, "%li", x);
buffer->count += count;
}
#endif
#define HEX(i) (((uint8_t *) janet_base64)[(i)])
/* Returns a string description for a pointer. Truncates
@ -201,6 +217,16 @@ void janet_description_b(JanetBuffer *buffer, Janet x) {
janet_escape_buffer_b(buffer, janet_unwrap_buffer(x));
return;
case JANET_ABSTRACT: {
#ifdef JANET_BIGINT
if (janet_is_bigint(x, JANET_BIGINT_TYPE_int64)) {
int64_to_string_b(buffer,*(int64_t *)janet_unwrap_abstract(x));
return;
}
if (janet_is_bigint(x, JANET_BIGINT_TYPE_uint64)) {
uint64_to_string_b(buffer,*(uint64_t*)janet_unwrap_abstract(x));
return;
}
#endif
const char *n = janet_abstract_type(janet_unwrap_abstract(x))->name;
string_description_b(buffer, n, janet_unwrap_abstract(x));
return;

View File

@ -1321,6 +1321,19 @@ JANET_API JanetTArrayView *janet_gettarray_view(const Janet *argv, int32_t n, Ja
#endif
#ifdef JANET_BIGINT
typedef enum {
JANET_BIGINT_TYPE_int64,
JANET_BIGINT_TYPE_uint64,
} JanetBigintType;
JANET_API int janet_is_bigint(Janet x, JanetBigintType type);
#endif
/***** END SECTION MAIN *****/
#ifdef __cplusplus