Skip to content

Commit da8455e

Browse files
committed
Extract arith_seq_take
1 parent 0828dff commit da8455e

File tree

1 file changed

+48
-35
lines changed

1 file changed

+48
-35
lines changed

enumerator.c

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3952,53 +3952,24 @@ rb_arithmetic_sequence_beg_len_step(VALUE obj, long *begp, long *lenp, long *ste
39523952
return Qnil;
39533953
}
39543954

3955-
/*
3956-
* call-seq:
3957-
* aseq.first -> num or nil
3958-
* aseq.first(n) -> an_array
3959-
*
3960-
* Returns the first number in this arithmetic sequence,
3961-
* or an array of the first +n+ elements.
3962-
*/
39633955
static VALUE
3964-
arith_seq_first(int argc, VALUE *argv, VALUE self)
3956+
arith_seq_take(VALUE self, VALUE num)
39653957
{
39663958
VALUE b, e, s, ary;
39673959
long n;
39683960
int x;
39693961

3970-
rb_check_arity(argc, 0, 1);
3971-
3972-
b = arith_seq_begin(self);
3973-
e = arith_seq_end(self);
3974-
s = arith_seq_step(self);
3975-
if (argc == 0) {
3976-
if (NIL_P(b)) {
3977-
return Qnil;
3978-
}
3979-
if (!NIL_P(e)) {
3980-
VALUE zero = INT2FIX(0);
3981-
int r = rb_cmpint(rb_num_coerce_cmp(s, zero, idCmp), s, zero);
3982-
if (r > 0 && RTEST(rb_funcall(b, '>', 1, e))) {
3983-
return Qnil;
3984-
}
3985-
if (r < 0 && RTEST(rb_funcall(b, '<', 1, e))) {
3986-
return Qnil;
3987-
}
3988-
}
3989-
return b;
3990-
}
3991-
3992-
// TODO: the following code should be extracted as arith_seq_take
3993-
3994-
n = NUM2LONG(argv[0]);
3962+
n = NUM2LONG(num);
39953963
if (n < 0) {
39963964
rb_raise(rb_eArgError, "attempt to take negative size");
39973965
}
39983966
if (n == 0) {
39993967
return rb_ary_new_capa(0);
40003968
}
40013969

3970+
b = arith_seq_begin(self);
3971+
e = arith_seq_end(self);
3972+
s = arith_seq_step(self);
40023973
x = arith_seq_exclude_end_p(self);
40033974

40043975
if (FIXNUM_P(b) && NIL_P(e) && FIXNUM_P(s)) {
@@ -4093,7 +4064,49 @@ arith_seq_first(int argc, VALUE *argv, VALUE self)
40934064
return ary;
40944065
}
40954066

4096-
return rb_call_super(argc, argv);
4067+
{
4068+
VALUE argv[1];
4069+
argv[0] = num;
4070+
return rb_call_super(1, argv);
4071+
}
4072+
}
4073+
4074+
/*
4075+
* call-seq:
4076+
* aseq.first -> num or nil
4077+
* aseq.first(n) -> an_array
4078+
*
4079+
* Returns the first number in this arithmetic sequence,
4080+
* or an array of the first +n+ elements.
4081+
*/
4082+
static VALUE
4083+
arith_seq_first(int argc, VALUE *argv, VALUE self)
4084+
{
4085+
VALUE b, e, s;
4086+
4087+
rb_check_arity(argc, 0, 1);
4088+
4089+
b = arith_seq_begin(self);
4090+
e = arith_seq_end(self);
4091+
s = arith_seq_step(self);
4092+
if (argc == 0) {
4093+
if (NIL_P(b)) {
4094+
return Qnil;
4095+
}
4096+
if (!NIL_P(e)) {
4097+
VALUE zero = INT2FIX(0);
4098+
int r = rb_cmpint(rb_num_coerce_cmp(s, zero, idCmp), s, zero);
4099+
if (r > 0 && RTEST(rb_funcall(b, '>', 1, e))) {
4100+
return Qnil;
4101+
}
4102+
if (r < 0 && RTEST(rb_funcall(b, '<', 1, e))) {
4103+
return Qnil;
4104+
}
4105+
}
4106+
return b;
4107+
}
4108+
4109+
return arith_seq_take(self, argv[0]);
40974110
}
40984111

40994112
static inline VALUE

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy