Esempio n. 1
0
static VALUE
string_to_c_internal(VALUE self)
{
    VALUE s;

    s = self;

    if (RSTRING_LEN(s) == 0)
	return rb_assoc_new(Qnil, self);

    {
	VALUE m, sr, si, re, r, i;
	int po;

	m = f_match(comp_pat0, s);
	if (!NIL_P(m)) {
	    sr = rb_reg_nth_match(1, m);
	    si = rb_reg_nth_match(2, m);
	    re = rb_reg_match_post(m);
	    po = 1;
	}
	if (NIL_P(m)) {
	    m = f_match(comp_pat1, s);
	    if (!NIL_P(m)) {
		sr = Qnil;
		si = rb_reg_nth_match(1, m);
		if (NIL_P(si))
		    si = rb_usascii_str_new2("");
		{
		    VALUE t;

		    t = rb_reg_nth_match(2, m);
		    if (NIL_P(t))
			t = rb_usascii_str_new2("1");
		    rb_str_concat(si, t);
		}
		re = rb_reg_match_post(m);
		po = 0;
	    }
	}
	if (NIL_P(m)) {
	    m = f_match(comp_pat2, s);
	    if (NIL_P(m))
		return rb_assoc_new(Qnil, self);
	    sr = rb_reg_nth_match(1, m);
	    if (NIL_P(rb_reg_nth_match(2, m)))
		si = Qnil;
	    else {
		VALUE t;

		si = rb_reg_nth_match(3, m);
		t = rb_reg_nth_match(4, m);
		if (NIL_P(t))
		    t = rb_usascii_str_new2("1");
		rb_str_concat(si, t);
	    }
	    re = rb_reg_match_post(m);
	    po = 0;
	}
	r = INT2FIX(0);
	i = INT2FIX(0);
	if (!NIL_P(sr)) {
	    if (strchr(RSTRING_PTR(sr), '/'))
		r = f_to_r(sr);
	    else if (strpbrk(RSTRING_PTR(sr), ".eE"))
		r = f_to_f(sr);
	    else
		r = f_to_i(sr);
	}
	if (!NIL_P(si)) {
	    if (strchr(RSTRING_PTR(si), '/'))
		i = f_to_r(si);
	    else if (strpbrk(RSTRING_PTR(si), ".eE"))
		i = f_to_f(si);
	    else
		i = f_to_i(si);
	}
	if (po)
	    return rb_assoc_new(rb_complex_polar(r, i), re);
	else
	    return rb_assoc_new(rb_complex_new2(r, i), re);
    }
}
Esempio n. 2
0
static int
read_comp(const char **s, int strict,
	  VALUE *ret, char **b)
{
    char *bb;
    int sign;
    VALUE num, num2;

    bb = *b;

    sign = read_sign(s, b);

    if (isimagunit(**s)) {
	(*s)++;
	num = INT2FIX((sign == '-') ? -1 : + 1);
	*ret = rb_complex_new2(ZERO, num);
	return 1; /* e.g. "i" */
    }

    if (!read_rat_nos(s, strict, b)) {
	**b = '\0';
	num = str2num(bb);
	*ret = rb_complex_new2(num, ZERO);
	return 0; /* e.g. "-" */
    }
    **b = '\0';
    num = str2num(bb);

    if (isimagunit(**s)) {
	(*s)++;
	*ret = rb_complex_new2(ZERO, num);
	return 1; /* e.g. "3i" */
    }

    if (**s == '@') {
	int st;

	(*s)++;
	bb = *b;
	st = read_rat(s, strict, b);
	**b = '\0';
	if (strlen(bb) < 1 ||
	    !isdecimal(*(bb + strlen(bb) - 1))) {
	    *ret = rb_complex_new2(num, ZERO);
	    return 0; /* e.g. "1@-" */
	}
	num2 = str2num(bb);
	*ret = rb_complex_polar(num, num2);
	if (!st)
	    return 0; /* e.g. "1@2." */
	else
	    return 1; /* e.g. "1@2" */
    }

    if (issign(**s)) {
	bb = *b;
	sign = read_sign(s, b);
	if (isimagunit(**s))
	    num2 = INT2FIX((sign == '-') ? -1 : + 1);
	else {
	    if (!read_rat_nos(s, strict, b)) {
		*ret = rb_complex_new2(num, ZERO);
		return 0; /* e.g. "1+xi" */
	    }
	    **b = '\0';
	    num2 = str2num(bb);
	}
	if (!isimagunit(**s)) {
	    *ret = rb_complex_new2(num, ZERO);
	    return 0; /* e.g. "1+3x" */
	}
	(*s)++;
	*ret = rb_complex_new2(num, num2);
	return 1; /* e.g. "1+2i" */
    }
    /* !(@, - or +) */
    {
	*ret = rb_complex_new2(num, ZERO);
	return 1; /* e.g. "3" */
    }
}
Esempio n. 3
0
static VALUE
string_to_c_internal(VALUE self)
{
    VALUE s;

    s = self;

    if (RSTRING_LEN(s) == 0)
	return rb_assoc_new(Qnil, self);

    {
	VALUE m, sr, si, re, r, i;
	int po;

	m = f_match(comp_pat0, s);
	if (!NIL_P(m)) {
	  sr = f_aref(m, INT2FIX(1));
	  si = f_aref(m, INT2FIX(2));
	  re = f_post_match(m);
	  po = 1;
	}
	if (NIL_P(m)) {
	    m = f_match(comp_pat1, s);
	    if (!NIL_P(m)) {
		sr = Qnil;
		si = f_aref(m, INT2FIX(1));
		if (NIL_P(si))
		    si = rb_usascii_str_new2("");
		{
		    VALUE t;

		    t = f_aref(m, INT2FIX(2));
		    if (NIL_P(t))
			t = rb_usascii_str_new2("1");
		    rb_str_concat(si, t);
		}
		re = f_post_match(m);
		po = 0;
	    }
	}
	if (NIL_P(m)) {
	    m = f_match(comp_pat2, s);
	    if (NIL_P(m))
		return rb_assoc_new(Qnil, self);
	    sr = f_aref(m, INT2FIX(1));
	    if (NIL_P(f_aref(m, INT2FIX(2))))
		si = Qnil;
	    else {
		VALUE t;

		si = f_aref(m, INT2FIX(3));
		t = f_aref(m, INT2FIX(4));
		if (NIL_P(t))
		    t = rb_usascii_str_new2("1");
		rb_str_concat(si, t);
	    }
	    re = f_post_match(m);
	    po = 0;
	}
	r = INT2FIX(0);
	i = INT2FIX(0);
	if (!NIL_P(sr)) {
	    if (f_include_p(sr, a_slash))
		r = f_to_r(sr);
	    else if (f_gt_p(f_count(sr, a_dot_and_an_e), INT2FIX(0)))
		r = f_to_f(sr);
	    else
		r = f_to_i(sr);
	}
	if (!NIL_P(si)) {
	    if (f_include_p(si, a_slash))
		i = f_to_r(si);
	    else if (f_gt_p(f_count(si, a_dot_and_an_e), INT2FIX(0)))
		i = f_to_f(si);
	    else
		i = f_to_i(si);
	}
	if (po)
	    return rb_assoc_new(rb_complex_polar(r, i), re);
	else
	    return rb_assoc_new(rb_complex_new2(r, i), re);
    }
}