Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String handling fixes and simplifications #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions ext/gsl_native/block_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,24 +138,20 @@ static VALUE FUNCTION(rb_gsl_block,fscanf)(VALUE obj, VALUE io)
static VALUE FUNCTION(rb_gsl_block,to_s)(VALUE obj)
{
GSL_TYPE(gsl_block) *v = NULL;
char buf[32];
size_t i, n;
VALUE str;
Data_Get_Struct(obj, GSL_TYPE(gsl_block), v);
str = rb_str_new2("[ ");
n = v->size;
if (rb_obj_is_kind_of(obj, cgsl_block_complex)) n *= 2;
for (i = 0; i < n; i++) {
sprintf(buf, PRINTF_FORMAT, (TYPE2) v->data[i]);
rb_str_cat(str, buf, strlen(buf));
rb_str_catf(str, PRINTF_FORMAT, (TYPE2) v->data[i]);
if (i == SHOW_ELM && i != v->size-1) {
strcpy(buf, "... ");
rb_str_cat(str, buf, strlen(buf));
rb_str_cat2(str, "... ");
break;
}
}
sprintf(buf, "]");
rb_str_cat(str, buf, strlen(buf));
rb_str_cat2(str, "]");
return str;
}
#undef SHOW_ELM
Expand All @@ -164,9 +160,7 @@ static VALUE FUNCTION(rb_gsl_block,to_s)(VALUE obj)
static VALUE FUNCTION(rb_gsl_block,inspect)(VALUE obj)
{
VALUE str;
char buf[64];
sprintf(buf, "%s\n", rb_class2name(CLASS_OF(obj)));
str = rb_str_new2(buf);
str = rb_sprintf("%s\n", rb_class2name(CLASS_OF(obj)));
return rb_str_concat(str, FUNCTION(rb_gsl_block,to_s)(obj));
}

Expand Down
10 changes: 2 additions & 8 deletions ext/gsl_native/complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ static VALUE rb_gsl_complex_printf(VALUE obj, VALUE s)
return obj;
}

static VALUE rb_gsl_complex_return_double(double (*func)(gsl_complex), VALUE obj);
static VALUE rb_gsl_complex_return_double(double (*func)(gsl_complex), VALUE obj)
{
gsl_complex *c = NULL;
Expand Down Expand Up @@ -862,19 +861,14 @@ static VALUE rb_gsl_complex_zero(VALUE obj)

static VALUE rb_gsl_complex_to_s(VALUE obj)
{
char buf[256];
gsl_complex *z;
Data_Get_Struct(obj, gsl_complex, z);
sprintf(buf, "[ %4.3e %4.3e ]", GSL_REAL(*z), GSL_IMAG(*z));
return rb_str_new2(buf);
return rb_sprintf("[ %4.3e %4.3e ]", GSL_REAL(*z), GSL_IMAG(*z));
}

static VALUE rb_gsl_complex_inspect(VALUE obj)
{
char buf[256];
VALUE str;
sprintf(buf, "%s\n", rb_class2name(CLASS_OF(obj)));
str = rb_str_new2(buf);
VALUE str = rb_sprintf("%s\n", rb_class2name(CLASS_OF(obj)));
return rb_str_concat(str, rb_gsl_complex_to_s(obj));
}

Expand Down
18 changes: 8 additions & 10 deletions ext/gsl_native/gsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ static void rb_gsl_define_methods(VALUE module);

static VALUE rb_gsl_object_inspect(VALUE obj)
{
char buf[256];
sprintf(buf, "%s", rb_class2name(CLASS_OF(obj)));
return rb_str_new2(buf);
return rb_sprintf("%s", rb_class2name(CLASS_OF(obj)));
}

static VALUE rb_gsl_call_rescue(VALUE obj)
Expand All @@ -44,15 +42,15 @@ static VALUE rb_gsl_call_size(VALUE obj)

static VALUE rb_gsl_object_info(VALUE obj)
{
char buf[256];
VALUE s;
sprintf(buf, "Class: %s\n", rb_class2name(CLASS_OF(obj)));
sprintf(buf, "%sSuperClass: %s\n", buf, rb_class2name(RCLASS_SUPER(CLASS_OF(obj))));
VALUE buf, s;
buf = rb_sprintf("Class: %s\n", rb_class2name(CLASS_OF(obj)));
rb_str_catf(buf, "SuperClass: %s\n", rb_class2name(RCLASS_SUPER(CLASS_OF(obj))));
s = rb_rescue(rb_gsl_call_name, obj, rb_gsl_call_rescue, obj);
if (s) sprintf(buf, "%sType: %s\n", buf, STR2CSTR(s));
if (s) rb_str_catf(buf, "Type: %s\n", STR2CSTR(s));
RB_GC_GUARD(s);
s = rb_rescue(rb_gsl_call_size, obj, rb_gsl_call_rescue, obj);
if (s) sprintf(buf, "%sSize: %d\n", buf, (int) FIX2INT(s));
return rb_str_new2(buf);
if (s) rb_str_catf(buf, "Size: %d\n", (int) FIX2INT(s));
return buf;
}

static VALUE rb_gsl_not_implemeted(VALUE obj)
Expand Down
5 changes: 3 additions & 2 deletions ext/gsl_native/histogram.c
Original file line number Diff line number Diff line change
Expand Up @@ -1548,10 +1548,10 @@ static VALUE rb_gsl_histogram_fit_xexponential(int argc, VALUE *argv, VALUE obj)

static VALUE rb_gsl_histogram_fit(int argc, VALUE *argv, VALUE obj)
{
char fittype[32];
char *fittype;
if (argc < 1) rb_raise(rb_eArgError, "too few arguments");
Check_Type(argv[0], T_STRING);
strcpy(fittype, STR2CSTR(argv[0]));
fittype = STR2CSTR(argv[0]);
if (str_head_grep(fittype, "exp") == 0) {
return rb_gsl_histogram_fit_exponential(argc-1, argv+1, obj);
} else if (str_head_grep(fittype, "power") == 0) {
Expand All @@ -1566,6 +1566,7 @@ static VALUE rb_gsl_histogram_fit(int argc, VALUE *argv, VALUE obj)
rb_raise(rb_eRuntimeError,
"unknown fitting type %s (exp, power, gaus expected)", fittype);
}
RB_GC_GUARD(argv[0]);
return Qnil;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/gsl_native/include/rb_gsl_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ int str_tail_grep(const char *s0, const char *s1);
int str_head_grep(const char *s0, const char *s1);

#ifndef STR2CSTR
#define STR2CSTR StringValuePtr
#define STR2CSTR StringValueCStr
#endif

#ifndef RCLASS_SUPER
Expand Down
21 changes: 11 additions & 10 deletions ext/gsl_native/interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ static VALUE rb_gsl_interp_eval_integ_e(VALUE obj, VALUE xxa, VALUE yya,
const gsl_interp_type* get_interp_type(VALUE t)
{
int type;
char name[32];
char *name;
switch (TYPE(t)) {
case T_FIXNUM:
type = FIX2INT(t);
Expand All @@ -415,7 +415,7 @@ const gsl_interp_type* get_interp_type(VALUE t)
}
break;
case T_STRING:
strcpy(name, STR2CSTR(t));
name = STR2CSTR(t);
if (str_tail_grep(name, "linear") == 0) {
return gsl_interp_linear;
} else if (str_tail_grep(name, "polynomial") == 0) {
Expand All @@ -436,20 +436,21 @@ const gsl_interp_type* get_interp_type(VALUE t)
rb_raise(rb_eTypeError, "Unknown type");
break;
}
RB_GC_GUARD(t);
}

static VALUE rb_gsl_interp_info(VALUE obj)
{
rb_gsl_interp *p;
char buf[256];
VALUE buf;
Data_Get_Struct(obj, rb_gsl_interp, p);
sprintf(buf, "Class: %s\n", rb_class2name(CLASS_OF(obj)));
sprintf(buf, "%sSuperClass: %s\n", buf, rb_class2name(RCLASS_SUPER(CLASS_OF(obj))));
sprintf(buf, "%sType: %s\n", buf, gsl_interp_name(p->p));
sprintf(buf, "%sxmin: %f\n", buf, p->p->xmin);
sprintf(buf, "%sxmax: %f\n", buf, p->p->xmax);
sprintf(buf, "%sSize: %d\n", buf, (int) p->p->size);
return rb_str_new2(buf);
buf = rb_sprintf("Class: %s\n", rb_class2name(CLASS_OF(obj)));
rb_str_catf(buf, "SuperClass: %s\n", rb_class2name(RCLASS_SUPER(CLASS_OF(obj))));
rb_str_catf(buf, "Type: %s\n", gsl_interp_name(p->p));
rb_str_catf(buf, "xmin: %f\n", p->p->xmin);
rb_str_catf(buf, "xmax: %f\n", p->p->xmax);
rb_str_catf(buf, "Size: %d\n", (int) p->p->size);
return buf;
}

static void rb_gsl_interp_define_const(VALUE klass)
Expand Down
49 changes: 25 additions & 24 deletions ext/gsl_native/interp2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ VALUE cgsl_interp2d_accel; /* this is used also in spline2d.c */
EXTERN VALUE cgsl_vector, cgsl_matrix;

static VALUE rb_gsl_interp2d_alloc(int argc, VALUE *argv, VALUE self)
{
{
rb_gsl_interp2d *sp = NULL;
const gsl_interp2d_type *T = NULL;
double *xptr = NULL, *yptr = NULL, *zptr = NULL;
Expand Down Expand Up @@ -63,8 +63,8 @@ static VALUE rb_gsl_interp2d_init(VALUE self, VALUE xarr, VALUE yarr, VALUE zarr
}

static VALUE rb_gsl_interp_evaluate(
VALUE self, VALUE xarr, VALUE yarr, VALUE zarr, VALUE xx, VALUE yy,
double (*eval)(const gsl_interp2d * interp,
VALUE self, VALUE xarr, VALUE yarr, VALUE zarr, VALUE xx, VALUE yy,
double (*eval)(const gsl_interp2d * interp,
const double xa[], const double ya[], const double za[],
const double x, const double y, gsl_interp_accel * xacc,
gsl_interp_accel * yacc))
Expand All @@ -77,7 +77,7 @@ static VALUE rb_gsl_interp_evaluate(
xx = yy;
yy = temp;
}

rb_gsl_interp2d *rgi = NULL;
double *xptr = NULL, *yptr = NULL, *zptr = NULL;
gsl_vector *vx = NULL, *vy = NULL, *vnew = NULL;
Expand All @@ -89,19 +89,19 @@ static VALUE rb_gsl_interp_evaluate(
Data_Get_Struct(self, rb_gsl_interp2d, rgi);
xptr = get_vector_ptr(xarr, &stridex, &xsize);
if (xsize != rgi->p->xsize ) {
rb_raise(rb_eTypeError, "size mismatch (xa:%d != %d)", (int) xsize,
rb_raise(rb_eTypeError, "size mismatch (xa:%d != %d)", (int) xsize,
(int) rgi->p->xsize);
}

yptr = get_vector_ptr(yarr, &stridey, &ysize);
if (ysize != rgi->p->ysize ) {
rb_raise(rb_eTypeError, "size mismatch (ya:%d != %d)", (int) ysize,
rb_raise(rb_eTypeError, "size mismatch (ya:%d != %d)", (int) ysize,
(int) rgi->p->ysize);
}

zptr = get_vector_ptr(zarr, &stridez, &zsize);
if (zsize != xsize*ysize ) {
rb_raise(rb_eTypeError, "size mismatch (za:%d != %d)", (int) zsize,
rb_raise(rb_eTypeError, "size mismatch (za:%d != %d)", (int) zsize,
(int) xsize*ysize);
}

Expand Down Expand Up @@ -145,7 +145,7 @@ static VALUE rb_gsl_interp_evaluate(
vnew = gsl_vector_alloc(vx->size);

for (i = 0; i < vx->size; i++) {
val = (*eval)(rgi->p, xptr, yptr, zptr, gsl_vector_get(vx, i),
val = (*eval)(rgi->p, xptr, yptr, zptr, gsl_vector_get(vx, i),
gsl_vector_get(vy, i), rgi->xacc, rgi->yacc);
gsl_vector_set(vnew, i, val);
}
Expand Down Expand Up @@ -199,9 +199,9 @@ static void rb_gsl_interp2d_define_const(VALUE self)
}

const gsl_interp2d_type* get_interp2d_type(VALUE t)
{
{
int type;
char name[32];
char *name;

switch(TYPE(t)) {
case T_FIXNUM:
Expand All @@ -212,9 +212,9 @@ const gsl_interp2d_type* get_interp2d_type(VALUE t)
case 1: return gsl_interp2d_bilinear; break;
default:
rb_raise(rb_eRuntimeError, "Cannot recognize type %d.\n", type);
}
}
case T_STRING:
strcpy(name, STR2CSTR(t));
name = STR2CSTR(t);

if (str_tail_grep("bicubic", name) == 0) {
return gsl_interp2d_bicubic;
Expand All @@ -228,24 +228,25 @@ const gsl_interp2d_type* get_interp2d_type(VALUE t)
default:
rb_raise(rb_eRuntimeError, "Unknown type.");
}
RB_GC_GUARD(t);
}

static VALUE rb_gsl_interp2d_info(VALUE self)
{
rb_gsl_interp2d *p;
char buf[256];
VALUE buf;
Data_Get_Struct(self, rb_gsl_interp2d, p);
sprintf(buf, "Class: %s\n", rb_class2name(CLASS_OF(self)));
sprintf(buf, "%sSuperClass: %s\n", buf, rb_class2name(RCLASS_SUPER(CLASS_OF(self))));
sprintf(buf, "%sType: %s\n", buf, gsl_interp2d_name(p->p));
sprintf(buf, "%sxmin: %f\n", buf, p->p->xmin);
sprintf(buf, "%sxmax: %f\n", buf, p->p->xmax);
sprintf(buf, "%symin: %f\n", buf, p->p->ymin);
sprintf(buf, "%symax: %f\n", buf, p->p->ymax);
sprintf(buf, "%sxsize: %d\n", buf, (int) p->p->xsize);
sprintf(buf, "%sysize: %d\n", buf, (int) p->p->ysize);

return rb_str_new2(buf);
buf = rb_sprintf("Class: %s\n", rb_class2name(CLASS_OF(self)));
rb_str_catf(buf, "SuperClass: %s\n", rb_class2name(RCLASS_SUPER(CLASS_OF(self))));
rb_str_catf(buf, "Type: %s\n", gsl_interp2d_name(p->p));
rb_str_catf(buf, "xmin: %f\n", p->p->xmin);
rb_str_catf(buf, "xmax: %f\n", p->p->xmax);
rb_str_catf(buf, "ymin: %f\n", p->p->ymin);
rb_str_catf(buf, "ymax: %f\n", p->p->ymax);
rb_str_catf(buf, "xsize: %d\n", (int) p->p->xsize);
rb_str_catf(buf, "ysize: %d\n", (int) p->p->ysize);

return buf;
}

void Init_gsl_interp2d(VALUE module)
Expand Down
11 changes: 4 additions & 7 deletions ext/gsl_native/matrix_complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,6 @@ static VALUE rb_gsl_matrix_complex_print(VALUE obj)
static VALUE rb_gsl_matrix_complex_to_s(int argc, VALUE *argv, VALUE obj)
{
gsl_matrix_complex *m = NULL;
char buf[64];
size_t i, j;
VALUE str;
gsl_complex z;
Expand All @@ -624,9 +623,8 @@ static VALUE rb_gsl_matrix_complex_to_s(int argc, VALUE *argv, VALUE obj)
}
for (j = 0; j < m->size2; j++) {
z = gsl_matrix_complex_get(m, i, j);
sprintf(buf,
"%s[ %4.3e %4.3e ]", (j==0) ? "" : " ", GSL_REAL(z), GSL_IMAG(z));
rb_str_cat(str, buf, strlen(buf));
rb_str_catf(str,
"%s[ %4.3e %4.3e ]", (j==0) ? "" : " ", GSL_REAL(z), GSL_IMAG(z));
// if too many cols
if ((int) j >= max_cols-1 && j != m->size2-1) {
rb_str_cat(str, " ...", 4);
Expand All @@ -646,12 +644,11 @@ static VALUE rb_gsl_matrix_complex_to_s(int argc, VALUE *argv, VALUE obj)
static VALUE rb_gsl_matrix_complex_inspect(int argc, VALUE *argv, VALUE obj)
{
VALUE str;
char buf[128];
gsl_matrix_complex *m;

Data_Get_Struct(obj, gsl_matrix_complex, m);
sprintf(buf, "#<%s[%lu,%lu]:%#lx>\n", rb_class2name(CLASS_OF(obj)), m->size1, m->size2, NUM2ULONG(rb_obj_id(obj)));
str = rb_str_new2(buf);
str = rb_sprintf("#<%s[%lu,%lu]:%#lx>\n", rb_class2name(CLASS_OF(obj)),
m->size1, m->size2, NUM2ULONG(rb_obj_id(obj)));
return rb_str_concat(str, rb_gsl_matrix_complex_to_s(argc, argv, obj));
}

Expand Down
Loading