1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2024-11-13 11:04:27 -05:00

mruby: add methods KernAux.utoa10, .itoa10, .utoa16, .itoa16

This commit is contained in:
Alex Kotov 2022-02-01 10:19:54 +05:00
parent f39833b1b8
commit d414441521
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
5 changed files with 135 additions and 2 deletions

View file

@ -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);

View file

@ -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)

View file

@ -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

98
pkgs/mruby/src/ntoa.c Normal file
View file

@ -0,0 +1,98 @@
#include "main.h"
#include <stdint.h>
#include <kernaux.h>
#include <mruby.h>
#include <mruby/presym.h>
#include <mruby/string.h>
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;
}

33
pkgs/mruby/test/ntoa.rb Normal file
View file

@ -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