mirror of
https://github.com/tailix/libkernaux.git
synced 2025-06-30 18:42:46 -04:00
Ruby: improve code
This commit is contained in:
parent
33eb48ddb0
commit
b3b669c896
1 changed files with 30 additions and 44 deletions
|
@ -37,6 +37,10 @@ static VALUE rb_KernAux = Qnil;
|
||||||
static VALUE rb_KernAux_Error = Qnil;
|
static VALUE rb_KernAux_Error = Qnil;
|
||||||
static VALUE rb_KernAux_InvalidNtoaBaseError = Qnil;
|
static VALUE rb_KernAux_InvalidNtoaBaseError = Qnil;
|
||||||
|
|
||||||
|
#if defined(HAVE_KERNAUX_UTOA) || defined(HAVE_KERNAUX_ITOA)
|
||||||
|
static int convert_base(VALUE base);
|
||||||
|
#endif
|
||||||
|
|
||||||
void init_ntoa()
|
void init_ntoa()
|
||||||
{
|
{
|
||||||
rb_gc_register_mark_object(ID2SYM(rb_intern_LESS = rb_intern("<")));
|
rb_gc_register_mark_object(ID2SYM(rb_intern_LESS = rb_intern("<")));
|
||||||
|
@ -89,29 +93,8 @@ VALUE rb_KernAux_utoa(
|
||||||
if (rb_funcall(number_rb, rb_intern_LESS, 1, INT2FIX(0))) {
|
if (rb_funcall(number_rb, rb_intern_LESS, 1, INT2FIX(0))) {
|
||||||
rb_raise(rb_eRangeError, "can't convert negative number to uint64_t");
|
rb_raise(rb_eRangeError, "can't convert negative number to uint64_t");
|
||||||
}
|
}
|
||||||
|
|
||||||
int base = 0;
|
|
||||||
if (TYPE(base_rb) == T_SYMBOL) {
|
|
||||||
const ID base_id = SYM2ID(base_rb);
|
|
||||||
if (base_id == rb_intern_b) base = 'b';
|
|
||||||
else if (base_id == rb_intern_B) base = 'B';
|
|
||||||
else if (base_id == rb_intern_h) base = 'h';
|
|
||||||
else if (base_id == rb_intern_H) base = 'H';
|
|
||||||
else if (base_id == rb_intern_o) base = 'o';
|
|
||||||
else if (base_id == rb_intern_O) base = 'O';
|
|
||||||
else if (base_id == rb_intern_d) base = 'd';
|
|
||||||
else if (base_id == rb_intern_D) base = 'D';
|
|
||||||
else if (base_id == rb_intern_x) base = 'x';
|
|
||||||
else if (base_id == rb_intern_X) base = 'X';
|
|
||||||
else {
|
|
||||||
rb_raise(rb_KernAux_InvalidNtoaBaseError, "invalid base");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
base = NUM2INT(base_rb);
|
|
||||||
}
|
|
||||||
|
|
||||||
char buffer[KERNAUX_UTOA_BUFFER_SIZE];
|
char buffer[KERNAUX_UTOA_BUFFER_SIZE];
|
||||||
kernaux_utoa(NUM2ULL(number_rb), buffer, base);
|
kernaux_utoa(NUM2ULL(number_rb), buffer, convert_base(base_rb));
|
||||||
return rb_funcall(rb_str_new2(buffer), rb_intern_freeze, 0);
|
return rb_funcall(rb_str_new2(buffer), rb_intern_freeze, 0);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -123,29 +106,8 @@ VALUE rb_KernAux_itoa(
|
||||||
const VALUE base_rb
|
const VALUE base_rb
|
||||||
) {
|
) {
|
||||||
RB_INTEGER_TYPE_P(number_rb);
|
RB_INTEGER_TYPE_P(number_rb);
|
||||||
|
|
||||||
int base = 0;
|
|
||||||
if (TYPE(base_rb) == T_SYMBOL) {
|
|
||||||
const ID base_id = SYM2ID(base_rb);
|
|
||||||
if (base_id == rb_intern_b) base = 'b';
|
|
||||||
else if (base_id == rb_intern_B) base = 'B';
|
|
||||||
else if (base_id == rb_intern_h) base = 'h';
|
|
||||||
else if (base_id == rb_intern_H) base = 'H';
|
|
||||||
else if (base_id == rb_intern_o) base = 'o';
|
|
||||||
else if (base_id == rb_intern_O) base = 'O';
|
|
||||||
else if (base_id == rb_intern_d) base = 'd';
|
|
||||||
else if (base_id == rb_intern_D) base = 'D';
|
|
||||||
else if (base_id == rb_intern_x) base = 'x';
|
|
||||||
else if (base_id == rb_intern_X) base = 'X';
|
|
||||||
else {
|
|
||||||
rb_raise(rb_KernAux_InvalidNtoaBaseError, "invalid base");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
base = NUM2INT(base_rb);
|
|
||||||
}
|
|
||||||
|
|
||||||
char buffer[KERNAUX_ITOA_BUFFER_SIZE];
|
char buffer[KERNAUX_ITOA_BUFFER_SIZE];
|
||||||
kernaux_itoa(NUM2LL(number_rb), buffer, base);
|
kernaux_itoa(NUM2LL(number_rb), buffer, convert_base(base_rb));
|
||||||
return rb_funcall(rb_str_new2(buffer), rb_intern_freeze, 0);
|
return rb_funcall(rb_str_new2(buffer), rb_intern_freeze, 0);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -203,3 +165,27 @@ VALUE rb_KernAux_itoa16(
|
||||||
return rb_funcall(rb_str_new2(buffer), rb_intern_freeze, 0);
|
return rb_funcall(rb_str_new2(buffer), rb_intern_freeze, 0);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(HAVE_KERNAUX_UTOA) || defined(HAVE_KERNAUX_ITOA)
|
||||||
|
int convert_base(const VALUE base_rb)
|
||||||
|
{
|
||||||
|
if (TYPE(base_rb) == T_SYMBOL) {
|
||||||
|
const ID base_id = SYM2ID(base_rb);
|
||||||
|
if (base_id == rb_intern_b) return 'b';
|
||||||
|
else if (base_id == rb_intern_B) return 'B';
|
||||||
|
else if (base_id == rb_intern_h) return 'h';
|
||||||
|
else if (base_id == rb_intern_H) return 'H';
|
||||||
|
else if (base_id == rb_intern_o) return 'o';
|
||||||
|
else if (base_id == rb_intern_O) return 'O';
|
||||||
|
else if (base_id == rb_intern_d) return 'd';
|
||||||
|
else if (base_id == rb_intern_D) return 'D';
|
||||||
|
else if (base_id == rb_intern_x) return 'x';
|
||||||
|
else if (base_id == rb_intern_X) return 'X';
|
||||||
|
else {
|
||||||
|
rb_raise(rb_KernAux_InvalidNtoaBaseError, "invalid base");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return NUM2INT(base_rb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue