14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'ext/number/number.c', line 14
VALUE rb_String_to_number (VALUE str)
{
VALUE complex_regex = rb_reg_new_str(rb_enc_str_new("^(.*[^\\s]+)\\s*\\+\\s*(.*)i$", strlen("^(.*[^\\s]+)\\s*\\+\\s*(.*)i$"), rb_utf8_encoding()), 0);
VALUE complex_match = rb_funcall(complex_regex, rb_intern("match"), 1, str);
if (RTEST(complex_match))
{
VALUE complex_captures = rb_funcall(complex_match, rb_intern("captures"), 0);
return rb_funcall(Qnil, rb_intern("Number"), 2, RARRAY_PTR(complex_captures)[0], RARRAY_PTR(complex_captures)[1]);
}
else
{
VALUE interval_regex = rb_reg_new_str(rb_enc_str_new("^\\[(.*[^\\s]+)\\s*([<≤])\\s*\\((.*[^\\s]+)\\)\\s*([<≤])\\s*(.*[^\\s]+)\\]$", strlen("^\\[(.*[^\\s]+)\\s*([<≤])\\s*\\((.*[^\\s]+)\\)\\s*([<≤])\\s*(.*[^\\s]+)\\]$"), rb_utf8_encoding()), 0);
VALUE interval_match = rb_funcall(interval_regex, rb_intern("match"), 1, str);
if (RTEST(interval_match))
{
VALUE interval_captures = rb_funcall(interval_match, rb_intern("captures"), 0);
return rb_funcall(Qnil, rb_intern("Number"), 5, RARRAY_PTR(interval_captures)[0], RARRAY_PTR(interval_captures)[2], RARRAY_PTR(interval_captures)[4], *RSTRING_PTR(RARRAY_PTR(interval_captures)[2]) == '<' ? Qtrue : Qfalse, *RSTRING_PTR(RARRAY_PTR(interval_captures)[4]) == '<' ? Qtrue : Qfalse);
}
else
{
VALUE real_regex = rb_reg_new_str(rb_enc_str_new("^([+-]?[0-9]+):([+-]?[0-9]+)$", strlen("^([+-]?[0-9]+):([+-]?[0-9]+)$"), rb_utf8_encoding()), 0);
VALUE real_match = rb_funcall(real_regex, rb_intern("match"), 1, str);
if (RTEST(real_match))
{
VALUE real_captures = rb_funcall(real_match, rb_intern("captures"), 0);
VALUE result = rb_obj_alloc(rb_cNumber);
VALUE integer = rb_funcall(RARRAY_PTR(real_captures)[1], rb_intern("to_i"), 0);
Real* real = real_from_str((char*)RSTRING_PTR(RARRAY_PTR(real_captures)[0]), NUM2LONG(integer));
DATA_PTR(result) = complex_from_real(real);
real_free(real);
return result;
}
else if (RSTRING_PTR(str)[0] == -61)
{
VALUE result = rb_obj_alloc(rb_cNumber);
DATA_PTR(result) = complex_nan();
return result;
}
else if (RSTRING_PTR(str)[0] == -30)
{
VALUE result = rb_obj_alloc(rb_cNumber);
DATA_PTR(result) = complex_pos_inf();
return result;
}
else if ((RSTRING_PTR(str)[0] == 43 || RSTRING_PTR(str)[0] == 45) && RSTRING_PTR(str)[1] == -30)
{
VALUE result = rb_obj_alloc(rb_cNumber);
DATA_PTR(result) = (RSTRING_PTR(str)[0] == 43) ? complex_pos_inf() : complex_neg_inf();
return result;
}
else
{
VALUE integer_regex = rb_reg_new_str(rb_enc_str_new("^([+-]?[0-9]+)$", strlen("^([+-]?[0-9]+)$"), rb_utf8_encoding()), 0);
VALUE integer_match = rb_funcall(integer_regex, rb_intern("match"), 1, str);
if (RTEST(integer_match))
{
return rb_funcall(rb_funcall(str, rb_intern("to_i"), 0), rb_intern("to_number"), 0);
}
else
{
return rb_funcall(rb_funcall(str, rb_intern("to_f"), 0), rb_intern("to_number"), 0);
}
}
}
}
/* not reached */
return Qnil;
}
|