string.c: adjust to rb_str_upto_each
* range.c (range_each_func): adjust the signature of the callback function to rb_str_upto_each, and exit the loop if the callback returned non-zero. * string.c (rb_str_upto_endless_each): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63290 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d025f64a9f
commit
703a5dd3e0
@ -2012,7 +2012,8 @@ VALUE rb_gcd_gmp(VALUE x, VALUE y);
|
|||||||
/* internal use */
|
/* internal use */
|
||||||
VALUE rb_setup_fake_str(struct RString *fake_str, const char *name, long len, rb_encoding *enc);
|
VALUE rb_setup_fake_str(struct RString *fake_str, const char *name, long len, rb_encoding *enc);
|
||||||
#endif
|
#endif
|
||||||
VALUE rb_str_upto_endless_each(VALUE, VALUE (*each)(VALUE, VALUE), VALUE);
|
VALUE rb_str_upto_each(VALUE, VALUE, int, int (*each)(VALUE, VALUE), VALUE);
|
||||||
|
VALUE rb_str_upto_endless_each(VALUE, int (*each)(VALUE, VALUE), VALUE);
|
||||||
|
|
||||||
/* thread.c (export) */
|
/* thread.c (export) */
|
||||||
int ruby_thread_has_gvl_p(void); /* for ext/fiddle/closure.c */
|
int ruby_thread_has_gvl_p(void); /* for ext/fiddle/closure.c */
|
||||||
|
62
range.c
62
range.c
@ -236,7 +236,7 @@ range_hash(VALUE range)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
range_each_func(VALUE range, rb_block_call_func *func, VALUE arg)
|
range_each_func(VALUE range, int (*func)(VALUE, VALUE), VALUE arg)
|
||||||
{
|
{
|
||||||
int c;
|
int c;
|
||||||
VALUE b = RANGE_BEG(range);
|
VALUE b = RANGE_BEG(range);
|
||||||
@ -245,21 +245,21 @@ range_each_func(VALUE range, rb_block_call_func *func, VALUE arg)
|
|||||||
|
|
||||||
if (EXCL(range)) {
|
if (EXCL(range)) {
|
||||||
while (r_less(v, e) < 0) {
|
while (r_less(v, e) < 0) {
|
||||||
(*func) (v, arg, 0, 0, 0);
|
if ((*func)(v, arg)) break;
|
||||||
v = rb_funcallv(v, id_succ, 0, 0);
|
v = rb_funcallv(v, id_succ, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
while ((c = r_less(v, e)) <= 0) {
|
while ((c = r_less(v, e)) <= 0) {
|
||||||
(*func) (v, arg, 0, 0, 0);
|
if ((*func)(v, arg)) break;;
|
||||||
if (!c) break;
|
if (!c) break;
|
||||||
v = rb_funcallv(v, id_succ, 0, 0);
|
v = rb_funcallv(v, id_succ, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static int
|
||||||
sym_step_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, arg))
|
sym_step_i(VALUE i, VALUE arg)
|
||||||
{
|
{
|
||||||
VALUE *iter = (VALUE *)arg;
|
VALUE *iter = (VALUE *)arg;
|
||||||
|
|
||||||
@ -273,11 +273,11 @@ sym_step_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, arg))
|
|||||||
rb_yield(rb_str_intern(i));
|
rb_yield(rb_str_intern(i));
|
||||||
iter[0] = iter[1];
|
iter[0] = iter[1];
|
||||||
}
|
}
|
||||||
return Qnil;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static int
|
||||||
step_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, arg))
|
step_i(VALUE i, VALUE arg)
|
||||||
{
|
{
|
||||||
VALUE *iter = (VALUE *)arg;
|
VALUE *iter = (VALUE *)arg;
|
||||||
|
|
||||||
@ -291,7 +291,7 @@ step_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, arg))
|
|||||||
rb_yield(i);
|
rb_yield(i);
|
||||||
iter[0] = iter[1];
|
iter[0] = iter[1];
|
||||||
}
|
}
|
||||||
return Qnil;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@ -426,17 +426,16 @@ range_step(int argc, VALUE *argv, VALUE range)
|
|||||||
|
|
||||||
}
|
}
|
||||||
else if (SYMBOL_P(b) && (NIL_P(e) || SYMBOL_P(e))) { /* symbols are special */
|
else if (SYMBOL_P(b) && (NIL_P(e) || SYMBOL_P(e))) { /* symbols are special */
|
||||||
VALUE args[2], iter[2];
|
VALUE iter[2];
|
||||||
iter[0] = INT2FIX(1);
|
iter[0] = INT2FIX(1);
|
||||||
iter[1] = step;
|
iter[1] = step;
|
||||||
|
|
||||||
|
b = rb_sym2str(b);
|
||||||
if (NIL_P(e)) {
|
if (NIL_P(e)) {
|
||||||
rb_str_upto_endless_each(rb_sym2str(b), (VALUE (*)(VALUE, VALUE))sym_step_i, (VALUE)iter);
|
rb_str_upto_endless_each(b, sym_step_i, (VALUE)iter);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
args[0] = rb_sym2str(e);
|
rb_str_upto_each(b, rb_sym2str(e), EXCL(range), sym_step_i, (VALUE)iter);
|
||||||
args[1] = EXCL(range) ? Qtrue : Qfalse;
|
|
||||||
rb_block_call(rb_sym2str(b), rb_intern("upto"), 2, args, sym_step_i, (VALUE)iter);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (ruby_float_step(b, e, step, EXCL(range), TRUE)) {
|
else if (ruby_float_step(b, e, step, EXCL(range), TRUE)) {
|
||||||
@ -459,19 +458,17 @@ range_step(int argc, VALUE *argv, VALUE range)
|
|||||||
tmp = rb_check_string_type(b);
|
tmp = rb_check_string_type(b);
|
||||||
|
|
||||||
if (!NIL_P(tmp)) {
|
if (!NIL_P(tmp)) {
|
||||||
VALUE args[2], iter[2];
|
VALUE iter[2];
|
||||||
|
|
||||||
b = tmp;
|
b = tmp;
|
||||||
iter[0] = INT2FIX(1);
|
iter[0] = INT2FIX(1);
|
||||||
iter[1] = step;
|
iter[1] = step;
|
||||||
|
|
||||||
if (NIL_P(e)) {
|
if (NIL_P(e)) {
|
||||||
rb_str_upto_endless_each(b, (VALUE (*)(VALUE, VALUE))step_i, (VALUE)iter);
|
rb_str_upto_endless_each(b, step_i, (VALUE)iter);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
args[0] = e;
|
rb_str_upto_each(b, e, EXCL(range), step_i, (VALUE)iter);
|
||||||
args[1] = EXCL(range) ? Qtrue : Qfalse;
|
|
||||||
rb_block_call(b, rb_intern("upto"), 2, args, step_i, (VALUE)iter);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -722,18 +719,18 @@ range_bsearch(VALUE range)
|
|||||||
return range;
|
return range;
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static int
|
||||||
each_i(RB_BLOCK_CALL_FUNC_ARGLIST(v, arg))
|
each_i(VALUE v, VALUE arg)
|
||||||
{
|
{
|
||||||
rb_yield(v);
|
rb_yield(v);
|
||||||
return Qnil;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static int
|
||||||
sym_each_i(RB_BLOCK_CALL_FUNC_ARGLIST(v, arg))
|
sym_each_i(VALUE v, VALUE arg)
|
||||||
{
|
{
|
||||||
rb_yield(rb_str_intern(v));
|
rb_yield(rb_str_intern(v));
|
||||||
return Qnil;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -817,25 +814,18 @@ range_each(VALUE range)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (SYMBOL_P(beg) && SYMBOL_P(end)) { /* symbols are special */
|
else if (SYMBOL_P(beg) && SYMBOL_P(end)) { /* symbols are special */
|
||||||
VALUE args[2];
|
beg = rb_sym2str(beg);
|
||||||
|
rb_str_upto_each(beg, rb_sym2str(end), EXCL(range), sym_each_i, 0);
|
||||||
args[0] = rb_sym2str(end);
|
|
||||||
args[1] = EXCL(range) ? Qtrue : Qfalse;
|
|
||||||
rb_block_call(rb_sym2str(beg), rb_intern("upto"), 2, args, sym_each_i, 0);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
VALUE tmp = rb_check_string_type(beg);
|
VALUE tmp = rb_check_string_type(beg);
|
||||||
|
|
||||||
if (!NIL_P(tmp)) {
|
if (!NIL_P(tmp)) {
|
||||||
if (!NIL_P(end)) {
|
if (!NIL_P(end)) {
|
||||||
VALUE args[2];
|
rb_str_upto_each(tmp, end, EXCL(range), each_i, 0);
|
||||||
|
|
||||||
args[0] = end;
|
|
||||||
args[1] = EXCL(range) ? Qtrue : Qfalse;
|
|
||||||
rb_block_call(tmp, rb_intern("upto"), 2, args, each_i, 0);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
rb_str_upto_endless_each(tmp, (VALUE (*)(VALUE, VALUE))each_i, 0);
|
rb_str_upto_endless_each(tmp, each_i, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
18
string.c
18
string.c
@ -4192,8 +4192,6 @@ all_digits_p(const char *s, long len)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALUE);
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
str_upto_i(VALUE str, VALUE arg)
|
str_upto_i(VALUE str, VALUE arg)
|
||||||
{
|
{
|
||||||
@ -4240,11 +4238,11 @@ rb_str_upto(int argc, VALUE *argv, VALUE beg)
|
|||||||
|
|
||||||
rb_scan_args(argc, argv, "11", &end, &exclusive);
|
rb_scan_args(argc, argv, "11", &end, &exclusive);
|
||||||
RETURN_ENUMERATOR(beg, argc, argv);
|
RETURN_ENUMERATOR(beg, argc, argv);
|
||||||
return str_upto_each(beg, end, RTEST(exclusive), str_upto_i, Qnil);
|
return rb_str_upto_each(beg, end, RTEST(exclusive), str_upto_i, Qnil);
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
VALUE
|
||||||
str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALUE arg)
|
rb_str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALUE arg)
|
||||||
{
|
{
|
||||||
VALUE current, after_end;
|
VALUE current, after_end;
|
||||||
ID succ;
|
ID succ;
|
||||||
@ -4326,7 +4324,7 @@ str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALUE a
|
|||||||
}
|
}
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
rb_str_upto_endless_each(VALUE beg, VALUE (*each)(VALUE, VALUE), VALUE arg)
|
rb_str_upto_endless_each(VALUE beg, int (*each)(VALUE, VALUE), VALUE arg)
|
||||||
{
|
{
|
||||||
VALUE current;
|
VALUE current;
|
||||||
ID succ;
|
ID succ;
|
||||||
@ -4343,7 +4341,7 @@ rb_str_upto_endless_each(VALUE beg, VALUE (*each)(VALUE, VALUE), VALUE arg)
|
|||||||
rb_encoding *usascii = rb_usascii_encoding();
|
rb_encoding *usascii = rb_usascii_encoding();
|
||||||
|
|
||||||
while (FIXABLE(bi)) {
|
while (FIXABLE(bi)) {
|
||||||
(*each)(rb_enc_sprintf(usascii, "%.*ld", width, bi), arg);
|
if ((*each)(rb_enc_sprintf(usascii, "%.*ld", width, bi), arg)) break;
|
||||||
bi++;
|
bi++;
|
||||||
}
|
}
|
||||||
b = LONG2NUM(bi);
|
b = LONG2NUM(bi);
|
||||||
@ -4351,7 +4349,7 @@ rb_str_upto_endless_each(VALUE beg, VALUE (*each)(VALUE, VALUE), VALUE arg)
|
|||||||
args[0] = INT2FIX(width);
|
args[0] = INT2FIX(width);
|
||||||
while (1) {
|
while (1) {
|
||||||
args[1] = b;
|
args[1] = b;
|
||||||
(*each)(rb_str_format(numberof(args), args, fmt), arg);
|
if ((*each)(rb_str_format(numberof(args), args, fmt), arg)) break;
|
||||||
b = rb_funcallv(b, succ, 0, 0);
|
b = rb_funcallv(b, succ, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -4359,7 +4357,7 @@ rb_str_upto_endless_each(VALUE beg, VALUE (*each)(VALUE, VALUE), VALUE arg)
|
|||||||
current = rb_str_dup(beg);
|
current = rb_str_dup(beg);
|
||||||
while (1) {
|
while (1) {
|
||||||
VALUE next = rb_funcallv(current, succ, 0, 0);
|
VALUE next = rb_funcallv(current, succ, 0, 0);
|
||||||
(*each)(current, arg);
|
if ((*each)(current, arg)) break;
|
||||||
current = next;
|
current = next;
|
||||||
StringValue(current);
|
StringValue(current);
|
||||||
if (RSTRING_LEN(current) == 0)
|
if (RSTRING_LEN(current) == 0)
|
||||||
@ -4417,7 +4415,7 @@ rb_str_include_range_p(VALUE beg, VALUE end, VALUE val, VALUE exclusive)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
str_upto_each(beg, end, RTEST(exclusive), include_range_i, (VALUE)&val);
|
rb_str_upto_each(beg, end, RTEST(exclusive), include_range_i, (VALUE)&val);
|
||||||
|
|
||||||
return NIL_P(val) ? Qtrue : Qfalse;
|
return NIL_P(val) ? Qtrue : Qfalse;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user