static mrb_value flo_to_s(mrb_state *mrb, mrb_value flt) { if (isnan(mrb_float(flt))) { return mrb_str_new_lit(mrb, "NaN"); } return mrb_float_to_str(mrb, flt, MRB_FLO_TO_STR_FMT); }
static size_t get_pool_block_size(mrb_state *mrb, mrb_irep *irep) { size_t size = 0; size_t pool_no; mrb_value str; char buf[32]; size += sizeof(uint32_t); /* plen */ size += irep->plen * (sizeof(uint8_t) + sizeof(uint16_t)); /* len(n) */ for (pool_no = 0; pool_no < irep->plen; pool_no++) { int ai = mrb_gc_arena_save(mrb); switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: str = mrb_fixnum_to_str(mrb, irep->pool[pool_no], 10); { mrb_int len = RSTRING_LEN(str); mrb_assert(len >= 0); mrb_assert((size_t)len <= SIZE_MAX); size += (size_t)len; } break; case MRB_TT_FLOAT: { int len; len = mrb_float_to_str(buf, mrb_float(irep->pool[pool_no])); mrb_assert(len >= 0); mrb_assert((size_t)len <= SIZE_MAX); size += (size_t)len; } break; case MRB_TT_STRING: { mrb_int len = RSTRING_LEN(irep->pool[pool_no]); mrb_assert(len >= 0); mrb_assert((size_t)len <= SIZE_MAX); size += (size_t)len; } break; default: break; } mrb_gc_arena_restore(mrb, ai); } return size; }
static ptrdiff_t write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) { size_t pool_no; uint8_t *cur = buf; uint16_t len; mrb_value str; const char *char_ptr; cur += uint32_to_bin(irep->plen, cur); /* number of pool */ for (pool_no = 0; pool_no < irep->plen; pool_no++) { int ai = mrb_gc_arena_save(mrb); switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: cur += uint8_to_bin(IREP_TT_FIXNUM, cur); /* data type */ str = mrb_fixnum_to_str(mrb, irep->pool[pool_no], 10); break; case MRB_TT_FLOAT: cur += uint8_to_bin(IREP_TT_FLOAT, cur); /* data type */ str = mrb_float_to_str(mrb, irep->pool[pool_no], MRB_FLOAT_FMT); break; case MRB_TT_STRING: cur += uint8_to_bin(IREP_TT_STRING, cur); /* data type */ str = irep->pool[pool_no]; break; default: continue; } char_ptr = RSTRING_PTR(str); { mrb_int tlen = RSTRING_LEN(str); mrb_assert_int_fit(mrb_int, tlen, uint16_t, UINT16_MAX); len = (uint16_t)tlen; } cur += uint16_to_bin(len, cur); /* data length */ memcpy(cur, char_ptr, (size_t)len); cur += len; mrb_gc_arena_restore(mrb, ai); } return cur - buf; }
mrb_value mrb_redis_zadd(mrb_state *mrb, mrb_value self) { mrb_value key, member; mrb_float score; redisContext *rc = DATA_PTR(self); mrb_get_args(mrb, "ofo", &key, &score, &member); mrb_value score_str = mrb_float_to_str(mrb, mrb_float_value(mrb, score), "%f"); const char *argv[] = {"ZADD", RSTRING_PTR(key), RSTRING_PTR(score_str), RSTRING_PTR(member)}; size_t lens[] = {4, RSTRING_LEN(key), RSTRING_LEN(score_str), RSTRING_LEN(member)}; redisReply *rs = redisCommandArgv(rc, 4, argv, lens); freeReplyObject(rs); return self; }
static int write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) { size_t pool_no; uint8_t *cur = buf; size_t len; mrb_value str; const char *char_ptr; char char_buf[30]; cur += uint32_to_bin(irep->plen, cur); /* number of pool */ for (pool_no = 0; pool_no < irep->plen; pool_no++) { int ai = mrb_gc_arena_save(mrb); cur += uint8_to_bin(mrb_type(irep->pool[pool_no]), cur); /* data type */ switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: str = mrb_fixnum_to_str(mrb, irep->pool[pool_no], 10); char_ptr = RSTRING_PTR(str); len = RSTRING_LEN(str); break; case MRB_TT_FLOAT: len = mrb_float_to_str(char_buf, mrb_float(irep->pool[pool_no])); char_ptr = &char_buf[0]; break; case MRB_TT_STRING: str = irep->pool[pool_no]; char_ptr = RSTRING_PTR(str); len = RSTRING_LEN(str); break; default: continue; } cur += uint16_to_bin(len, cur); /* data length */ memcpy(cur, char_ptr, len); cur += len; mrb_gc_arena_restore(mrb, ai); } return (int)(cur - buf); }
static uint32_t get_pool_block_size(mrb_state *mrb, mrb_irep *irep, int type) { uint32_t size = 0; int pool_no; mrb_value str; char buf[32]; size += MRB_DUMP_SIZE_OF_LONG; /* plen */ size += irep->plen; /* tt(n) */ size += irep->plen * MRB_DUMP_SIZE_OF_SHORT; /* len(n) */ size += MRB_DUMP_SIZE_OF_SHORT; /* crc */ size = DUMP_SIZE(size, type); for (pool_no = 0; pool_no < irep->plen; pool_no++) { uint16_t nlen =0; int len; switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: len = mrb_int_to_str( buf, mrb_fixnum(irep->pool[pool_no])); size += (uint32_t)len; break; case MRB_TT_FLOAT: len = mrb_float_to_str( buf, mrb_float(irep->pool[pool_no])); size += (uint32_t)len; break; case MRB_TT_STRING: str = mrb_string_value( mrb, &irep->pool[pool_no]); nlen = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); size += nlen; break; #ifdef ENABLE_REGEXP case MRB_TT_REGEX: str = mrb_reg_to_s(mrb, irep->pool[pool_no]); nlen = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); size += nlen; break; #endif default: break; } } return size; }
static size_t get_pool_block_size(mrb_state *mrb, mrb_irep *irep) { size_t size = 0; size_t pool_no; int len; mrb_value str; char buf[32]; size += sizeof(uint32_t); /* plen */ size += irep->plen * (sizeof(uint8_t) + sizeof(uint16_t)); /* len(n) */ for (pool_no = 0; pool_no < irep->plen; pool_no++) { int ai = mrb_gc_arena_save(mrb); switch (irep->pool[pool_no].type) { case IREP_TT_FIXNUM: str = mrb_fixnum_to_str(mrb, mrb_fixnum_value(irep->pool[pool_no].value.i), 10); size += RSTRING_LEN(str); break; case IREP_TT_FLOAT: len = mrb_float_to_str(buf, irep->pool[pool_no].value.f); size += len; break; case IREP_TT_STRING: size += irep->pool[pool_no].value.s->len; break; default: break; } mrb_gc_arena_restore(mrb, ai); } return size; }
static int write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) { int pool_no; mrb_value str; char *buf_top = buf; char *char_buf; uint16_t buf_size =0; uint16_t len =0; int result; buf_size = MRB_DUMP_DEFAULT_STR_LEN; char_buf = (char *)mrb_malloc(mrb, buf_size); if (char_buf == NULL) { result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } buf += uint32_dump((uint32_t)irep->plen, buf, type); /* number of pool */ for (pool_no = 0; pool_no < irep->plen; pool_no++) { buf += uint8_dump(mrb_type(irep->pool[pool_no]), buf, type); /* data type */ memset(char_buf, 0, buf_size); switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: str = mrb_fix2str(mrb, irep->pool[pool_no], 10); memcpy(char_buf, RSTRING_PTR(str), RSTRING_LEN(str)); len = RSTRING_LEN(str); break; case MRB_TT_FLOAT: len = mrb_float_to_str(char_buf, mrb_float(irep->pool[pool_no])); break; case MRB_TT_STRING: str = irep->pool[pool_no]; len = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); if (len > buf_size - 1) { buf_size = len + 1; char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); if (char_buf == NULL) { result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } memset(char_buf, 0, buf_size); } str_dump(RSTRING_PTR(str), char_buf, RSTRING_LEN(str), type); break; default: buf += uint16_dump(0, buf, type); /* data length = 0 */ continue; } buf += uint16_dump(len, buf, type); /* data length */ memcpy(buf, char_buf, len); buf += len; } result = (int)(buf - buf_top); error_exit: mrb_free(mrb, char_buf); return result; }
static int write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) { int result; size_t pool_no; uint8_t *cur = buf; size_t buf_size, len; mrb_value str; char *char_buf = NULL; buf_size = MRB_DUMP_DEFAULT_STR_LEN; char_buf = (char *)mrb_malloc(mrb, buf_size); if (char_buf == NULL) { result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } cur += uint32_to_bin(irep->plen, cur); /* number of pool */ for (pool_no = 0; pool_no < irep->plen; pool_no++) { int ai = mrb_gc_arena_save(mrb); cur += uint8_to_bin(mrb_type(irep->pool[pool_no]), cur); /* data type */ memset(char_buf, 0, buf_size); switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: str = mrb_fix2str(mrb, irep->pool[pool_no], 10); memcpy(char_buf, RSTRING_PTR(str), RSTRING_LEN(str)); len = RSTRING_LEN(str); break; case MRB_TT_FLOAT: len = mrb_float_to_str(char_buf, mrb_float(irep->pool[pool_no])); break; case MRB_TT_STRING: str = irep->pool[pool_no]; len = RSTRING_LEN(str); if (len > buf_size - 1) { buf_size = len + 1; char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); if (char_buf == NULL) { mrb_gc_arena_restore(mrb, ai); result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } memset(char_buf, 0, buf_size); } memcpy(char_buf, RSTRING_PTR(str), RSTRING_LEN(str)); break; default: len = 0; continue; } cur += uint16_to_bin(len, cur); /* data length */ memcpy(cur, char_buf, len); cur += len; mrb_gc_arena_restore(mrb, ai); } result = (int)(cur - buf); error_exit: mrb_free(mrb, char_buf); return result; }