diff --git a/pkgs/ruby/ext/default/ntoa.c b/pkgs/ruby/ext/default/ntoa.c index 668ec95..7eebe3e 100644 --- a/pkgs/ruby/ext/default/ntoa.c +++ b/pkgs/ruby/ext/default/ntoa.c @@ -4,10 +4,10 @@ #define MAX_PREFIX_LEN 100 #ifdef HAVE_KERNAUX_UTOA -static VALUE rb_KernAux_utoa(VALUE self, VALUE number, VALUE base, VALUE prefix); +static VALUE rb_KernAux_utoa(int argc, const VALUE *argv, VALUE self); #endif #ifdef HAVE_KERNAUX_ITOA -static VALUE rb_KernAux_itoa(VALUE self, VALUE number, VALUE base, VALUE prefix); +static VALUE rb_KernAux_itoa(int argc, const VALUE *argv, VALUE self); #endif #ifdef HAVE_KERNAUX_UTOA10 static VALUE rb_KernAux_utoa10(VALUE self, VALUE number); @@ -70,10 +70,10 @@ void init_ntoa() rb_KernAux_Error)); #ifdef HAVE_KERNAUX_UTOA - rb_define_singleton_method(rb_KernAux, "utoa", rb_KernAux_utoa, 3); + rb_define_singleton_method(rb_KernAux, "utoa", rb_KernAux_utoa, -1); #endif #ifdef HAVE_KERNAUX_ITOA - rb_define_singleton_method(rb_KernAux, "itoa", rb_KernAux_itoa, 3); + rb_define_singleton_method(rb_KernAux, "itoa", rb_KernAux_itoa, -1); #endif #ifdef HAVE_KERNAUX_UTOA10 rb_define_singleton_method(rb_KernAux, "utoa10", rb_KernAux_utoa10, 1); @@ -90,12 +90,20 @@ void init_ntoa() } #ifdef HAVE_KERNAUX_UTOA -VALUE rb_KernAux_utoa( - const VALUE self_rb __attribute__((unused)), - const VALUE number_rb, - const VALUE base_rb, - VALUE prefix_rb -) { +VALUE rb_KernAux_utoa(const int argc, const VALUE *argv, const VALUE self) +{ + if (argc < 2 || argc > 3) { + rb_raise( + rb_eArgError, + "wrong number of arguments (given %d, expected 2..3)", + argc + ); + } + + VALUE number_rb = argv[0]; + VALUE base_rb = argv[1]; + VALUE prefix_rb = argc == 3 ? argv[2] : Qnil; + RB_INTEGER_TYPE_P(number_rb); if (rb_funcall(number_rb, rb_intern_LESS, 1, INT2FIX(0))) { rb_raise(rb_eRangeError, "can't convert negative number to uint64_t"); @@ -123,12 +131,20 @@ VALUE rb_KernAux_utoa( #endif #ifdef HAVE_KERNAUX_ITOA -VALUE rb_KernAux_itoa( - const VALUE self_rb __attribute__((unused)), - const VALUE number_rb, - const VALUE base_rb, - VALUE prefix_rb -) { +VALUE rb_KernAux_itoa(const int argc, const VALUE *argv, const VALUE self) +{ + if (argc < 2 || argc > 3) { + rb_raise( + rb_eArgError, + "wrong number of arguments (given %d, expected 2..3)", + argc + ); + } + + VALUE number_rb = argv[0]; + VALUE base_rb = argv[1]; + VALUE prefix_rb = argc == 3 ? argv[2] : Qnil; + RB_INTEGER_TYPE_P(number_rb); const char *prefix = NULL; diff --git a/pkgs/ruby/spec/lib/kernaux/itoa_spec.rb b/pkgs/ruby/spec/lib/kernaux/itoa_spec.rb index 2c3f618..d5c891f 100644 --- a/pkgs/ruby/spec/lib/kernaux/itoa_spec.rb +++ b/pkgs/ruby/spec/lib/kernaux/itoa_spec.rb @@ -169,6 +169,14 @@ RSpec.describe KernAux, '.itoa' do end end + context 'when no prefix is given' do + subject(:itoa) { described_class.itoa number, base } + + it { is_expected.to be_instance_of String } + it { is_expected.to be_frozen } + it { is_expected.to eq number.to_s base } + end + context 'when prefix is nil' do let(:prefix) { nil } diff --git a/pkgs/ruby/spec/lib/kernaux/utoa_spec.rb b/pkgs/ruby/spec/lib/kernaux/utoa_spec.rb index ed46369..82eedbf 100644 --- a/pkgs/ruby/spec/lib/kernaux/utoa_spec.rb +++ b/pkgs/ruby/spec/lib/kernaux/utoa_spec.rb @@ -145,6 +145,14 @@ RSpec.describe KernAux, '.utoa' do end end + context 'when no prefix is given' do + subject(:utoa) { described_class.utoa number, base } + + it { is_expected.to be_instance_of String } + it { is_expected.to be_frozen } + it { is_expected.to eq number.to_s base } + end + context 'when prefix is nil' do let(:prefix) { nil }