From cf9d48a8653cb2dfb2826eca1cc4423de01849e9 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 27 Sep 2014 19:40:10 +0900 Subject: [PATCH] add pic_size_value --- blob.c | 2 +- dict.c | 2 +- include/picrin/value.h | 12 ++++++++++++ pair.c | 2 +- string.c | 2 +- vector.c | 2 +- 6 files changed, 17 insertions(+), 5 deletions(-) diff --git a/blob.c b/blob.c index 509b295a..ae0f2fb7 100644 --- a/blob.c +++ b/blob.c @@ -83,7 +83,7 @@ pic_blob_bytevector_length(pic_state *pic) pic_get_args(pic, "b", &bv); - return pic_int_value((int)bv->len); + return pic_size_value(bv->len); } static pic_value diff --git a/dict.c b/dict.c index 2a8fcd41..c5387706 100644 --- a/dict.c +++ b/dict.c @@ -213,7 +213,7 @@ pic_dict_dictionary_size(pic_state *pic) pic_get_args(pic, "d", &dict); - return pic_int_value((int)pic_dict_size(pic, dict)); + return pic_size_value(pic_dict_size(pic, dict)); } static pic_value diff --git a/include/picrin/value.h b/include/picrin/value.h index 37dd58c0..d0c1dbe3 100644 --- a/include/picrin/value.h +++ b/include/picrin/value.h @@ -189,6 +189,7 @@ static inline pic_value pic_undef_value(); static inline pic_value pic_obj_value(void *); static inline pic_value pic_float_value(double); static inline pic_value pic_int_value(int); +static inline pic_value pic_size_value(size_t); static inline pic_value pic_sym_value(pic_sym); static inline pic_value pic_char_value(char c); static inline pic_value pic_none_value(); @@ -323,6 +324,17 @@ pic_bool_value(bool b) return v; } +static inline pic_value +pic_size_value(size_t s) +{ + if (sizeof(unsigned) < sizeof(size_t)) { + if (s > (size_t)INT_MAX) { + return pic_float_value(s); + } + } + return pic_int_value((int)s); +} + #if PIC_NAN_BOXING static inline pic_value diff --git a/pair.c b/pair.c index 763458f9..184182cc 100644 --- a/pair.c +++ b/pair.c @@ -559,7 +559,7 @@ pic_pair_length(pic_state *pic) pic_get_args(pic, "o", &list); - return pic_int_value(pic_length(pic, list)); + return pic_size_value(pic_length(pic, list)); } static pic_value diff --git a/string.c b/string.c index 6ef7e745..cd4b3660 100644 --- a/string.c +++ b/string.c @@ -271,7 +271,7 @@ pic_str_string_length(pic_state *pic) pic_get_args(pic, "s", &str); - return pic_int_value(pic_strlen(str)); + return pic_size_value(pic_strlen(str)); } static pic_value diff --git a/vector.c b/vector.c index 86cd3c9b..a7dbd81d 100644 --- a/vector.c +++ b/vector.c @@ -92,7 +92,7 @@ pic_vec_vector_length(pic_state *pic) pic_get_args(pic, "v", &v); - return pic_int_value((int)v->len); + return pic_size_value(v->len); } static pic_value