diff --git a/pkgs/mruby/src/assert.c b/pkgs/mruby/src/assert.c index 5dee060..2173637 100644 --- a/pkgs/mruby/src/assert.c +++ b/pkgs/mruby/src/assert.c @@ -41,12 +41,12 @@ void assert_cb(const char *const file, const int line, const char *const msg) if (mrb_nil_p(assert_cb_rb)) return; mrb_value file_rb = mrb_str_new_lit(mrb, ""); - file_rb = mrb_str_cat(mrb, file_rb, file, strlen(file)); + file_rb = mrb_str_cat_cstr(mrb, file_rb, file); mrb_value line_rb = mrb_fixnum_value(line); mrb_value msg_rb = mrb_str_new_lit(mrb, ""); - msg_rb = mrb_str_cat(mrb, msg_rb, msg, strlen(msg)); + msg_rb = mrb_str_cat_cstr(mrb, msg_rb, msg); mrb_funcall_id( mrb, assert_cb_rb, MRB_SYM(call), 3, file_rb, line_rb, msg_rb); diff --git a/pkgs/mruby/src/main.c b/pkgs/mruby/src/main.c index a05db33..444ba79 100644 --- a/pkgs/mruby/src/main.c +++ b/pkgs/mruby/src/main.c @@ -22,6 +22,7 @@ void mrb_mruby_kernaux_gem_init(mrb_state *const mrb) mrb_define_module_id(mrb, MRB_SYM(KernAux)); init_assert(mrb); + init_ntoa(mrb); } void current_mrb_start(mrb_state *mrb) diff --git a/pkgs/mruby/src/main.h b/pkgs/mruby/src/main.h index 489a1e7..1eb543f 100644 --- a/pkgs/mruby/src/main.h +++ b/pkgs/mruby/src/main.h @@ -8,5 +8,6 @@ void current_mrb_finish(mrb_state *mrb); mrb_state *current_mrb_get(); void init_assert(mrb_state *mrb); +void init_ntoa(mrb_state *mrb); #endif diff --git a/pkgs/mruby/src/ntoa.c b/pkgs/mruby/src/ntoa.c new file mode 100644 index 0000000..b42838b --- /dev/null +++ b/pkgs/mruby/src/ntoa.c @@ -0,0 +1,98 @@ +#include "main.h" + +#include + +#include + +#include +#include +#include + +static mrb_value rb_KernAux_utoa10(mrb_state *mrb, mrb_value self); +static mrb_value rb_KernAux_itoa10(mrb_state *mrb, mrb_value self); +static mrb_value rb_KernAux_utoa16(mrb_state *mrb, mrb_value self); +static mrb_value rb_KernAux_itoa16(mrb_state *mrb, mrb_value self); + +void init_ntoa(mrb_state *const mrb) +{ + struct RClass *const rb_KernAux = mrb_module_get_id(mrb, MRB_SYM(KernAux)); + + mrb_define_class_method(mrb, rb_KernAux, "utoa10", + rb_KernAux_utoa10, MRB_ARGS_REQ(1)); + mrb_define_class_method(mrb, rb_KernAux, "itoa10", + rb_KernAux_itoa10, MRB_ARGS_REQ(1)); + mrb_define_class_method(mrb, rb_KernAux, "utoa16", + rb_KernAux_utoa16, MRB_ARGS_REQ(1)); + mrb_define_class_method(mrb, rb_KernAux, "itoa16", + rb_KernAux_itoa16, MRB_ARGS_REQ(1)); +} + +mrb_value rb_KernAux_utoa10(mrb_state *mrb, mrb_value self) +{ + mrb_int value = 0; + mrb_get_args(mrb, "i", &value); + + if (value < 0) { + mrb_raise(mrb, E_RANGE_ERROR, + "can't convert negative number to uint64_t"); + } + + char buffer[KERNAUX_UTOA10_BUFFER_SIZE]; + current_mrb_start(mrb); + kernaux_utoa10(value, buffer); + current_mrb_finish(mrb); + + mrb_value result = mrb_str_new_lit(mrb, ""); + result = mrb_str_cat_cstr(mrb, result, buffer); + return result; +} + +mrb_value rb_KernAux_itoa10(mrb_state *mrb, mrb_value self) +{ + mrb_int value = 0; + mrb_get_args(mrb, "i", &value); + + char buffer[KERNAUX_ITOA10_BUFFER_SIZE]; + current_mrb_start(mrb); + kernaux_itoa10(value, buffer); + current_mrb_finish(mrb); + + mrb_value result = mrb_str_new_lit(mrb, ""); + result = mrb_str_cat_cstr(mrb, result, buffer); + return result; +} + +mrb_value rb_KernAux_utoa16(mrb_state *mrb, mrb_value self) +{ + mrb_int value = 0; + mrb_get_args(mrb, "i", &value); + + if (value < 0) { + mrb_raise(mrb, E_RANGE_ERROR, + "can't convert negative number to uint64_t"); + } + + char buffer[KERNAUX_UTOA16_BUFFER_SIZE]; + current_mrb_start(mrb); + kernaux_utoa16(value, buffer); + current_mrb_finish(mrb); + + mrb_value result = mrb_str_new_lit(mrb, ""); + result = mrb_str_cat_cstr(mrb, result, buffer); + return result; +} + +mrb_value rb_KernAux_itoa16(mrb_state *mrb, mrb_value self) +{ + mrb_int value = 0; + mrb_get_args(mrb, "i", &value); + + char buffer[KERNAUX_ITOA16_BUFFER_SIZE]; + current_mrb_start(mrb); + kernaux_itoa16(value, buffer); + current_mrb_finish(mrb); + + mrb_value result = mrb_str_new_lit(mrb, ""); + result = mrb_str_cat_cstr(mrb, result, buffer); + return result; +} diff --git a/pkgs/mruby/test/ntoa.rb b/pkgs/mruby/test/ntoa.rb new file mode 100644 index 0000000..8d8d352 --- /dev/null +++ b/pkgs/mruby/test/ntoa.rb @@ -0,0 +1,33 @@ +assert 'KernAux.utoa10' do + assert_equal '0', KernAux.utoa10(0) + assert_equal '1', KernAux.utoa10(1) + assert_equal '123', KernAux.utoa10(123) + assert_equal (2**32 - 1).to_s, KernAux.utoa10(2**32 - 1) +end + +assert 'KernAux.itoa10' do + assert_equal '0', KernAux.itoa10(0) + assert_equal '1', KernAux.itoa10(1) + assert_equal '-1', KernAux.itoa10(-1) + assert_equal '123', KernAux.itoa10(123) + assert_equal '-123', KernAux.itoa10(-123) + assert_equal (2**31 - 1).to_s, KernAux.itoa10(2**31 - 1) + assert_equal (-2**31).to_s, KernAux.itoa10(-2**31) +end + +assert 'KernAux.utoa16' do + assert_equal '0', KernAux.utoa16(0) + assert_equal '1', KernAux.utoa16(1) + assert_equal '123', KernAux.utoa16(0x123) + assert_equal (2**32 - 1).to_s(16), KernAux.utoa16(2**32 - 1) +end + +assert 'KernAux.itoa16' do + assert_equal '0', KernAux.itoa16(0) + assert_equal '1', KernAux.itoa16(1) + assert_equal '-1', KernAux.itoa16(-1) + assert_equal '123', KernAux.itoa16(0x123) + assert_equal '-123', KernAux.itoa16(-0x123) + assert_equal (2**31 - 1).to_s(16), KernAux.itoa16(2**31 - 1) + assert_equal (-2**31).to_s(16), KernAux.itoa16(-2**31) +end