1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* string.c (rb_str_upto): add optional argument to specify

exclusiveness.

* range.c (range_step): use String#upto with optional argument.

* range.c (range_each): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12586 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2007-06-22 16:23:16 +00:00
parent f11a6f2d65
commit 115410ecde
5 changed files with 53 additions and 59 deletions

View file

@ -1,3 +1,12 @@
Fri Jun 22 23:55:59 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* string.c (rb_str_upto): add optional argument to specify
exclusiveness.
* range.c (range_step): use String#upto with optional argument.
* range.c (range_each): ditto.
Fri Jun 22 19:55:51 2007 Tanaka Akira <akr@fsij.org> Fri Jun 22 19:55:51 2007 Tanaka Akira <akr@fsij.org>
* proc.c (proc_to_s): revert the change from %p to %lx at YARV * proc.c (proc_to_s): revert the change from %p to %lx at YARV

View file

@ -518,7 +518,6 @@ VALUE rb_str_concat(VALUE, VALUE);
int rb_memhash(const void *ptr, long len); int rb_memhash(const void *ptr, long len);
int rb_str_hash(VALUE); int rb_str_hash(VALUE);
int rb_str_cmp(VALUE, VALUE); int rb_str_cmp(VALUE, VALUE);
VALUE rb_str_upto(VALUE, VALUE, int);
void rb_str_update(VALUE, long, long, VALUE); void rb_str_update(VALUE, long, long, VALUE);
VALUE rb_str_inspect(VALUE); VALUE rb_str_inspect(VALUE);
VALUE rb_str_dump(VALUE); VALUE rb_str_dump(VALUE);

29
range.c
View file

@ -218,14 +218,6 @@ range_hash(VALUE range)
return LONG2FIX(hash); return LONG2FIX(hash);
} }
static VALUE
str_step(VALUE arg)
{
VALUE *args = (VALUE *)arg;
return rb_str_upto(args[0], args[1], EXCL(args[2]));
}
static void static void
range_each_func(VALUE range, VALUE (*func) (VALUE, void *), VALUE v, VALUE e, range_each_func(VALUE range, VALUE (*func) (VALUE, void *), VALUE v, VALUE e,
void *arg) void *arg)
@ -323,16 +315,15 @@ range_step(int argc, VALUE *argv, VALUE range)
VALUE tmp = rb_check_string_type(b); VALUE tmp = rb_check_string_type(b);
if (!NIL_P(tmp)) { if (!NIL_P(tmp)) {
VALUE args[5]; VALUE args[2];
long iter[2]; long iter[2];
b = tmp; b = tmp;
args[0] = b; args[0] = e;
args[1] = e; args[1] = EXCL(range) ? Qtrue : Qfalse;
args[2] = range;
iter[0] = 1; iter[0] = 1;
iter[1] = unit; iter[1] = unit;
rb_iterate(str_step, (VALUE)args, step_i, (VALUE)iter); rb_block_call(b, rb_intern("upto"), 2, args, step_i, (VALUE)iter);
} }
else if (rb_obj_is_kind_of(b, rb_cNumeric)) { else if (rb_obj_is_kind_of(b, rb_cNumeric)) {
ID c = rb_intern(EXCL(range) ? "<" : "<="); ID c = rb_intern(EXCL(range) ? "<" : "<=");
@ -409,15 +400,11 @@ range_each(VALUE range)
} }
} }
else if (TYPE(beg) == T_STRING) { else if (TYPE(beg) == T_STRING) {
VALUE args[5]; VALUE args[2];
long iter[2];
args[0] = beg; args[0] = end;
args[1] = end; args[1] = EXCL(range) ? Qtrue : Qfalse;
args[2] = range; rb_block_call(beg, rb_intern("upto"), 2, args, rb_yield, 0);
iter[0] = 1;
iter[1] = 1;
rb_iterate(str_step, (VALUE)args, step_i, (VALUE)iter);
} }
else { else {
range_each_func(range, each_i, beg, end, NULL); range_each_func(range, each_i, beg, end, NULL);

View file

@ -1549,13 +1549,39 @@ rb_str_succ_bang(VALUE str)
return str; return str;
} }
VALUE
rb_str_upto(VALUE beg, VALUE end, int excl)
{
VALUE current, after_end;
ID succ = rb_intern("succ");
int n;
/*
* call-seq:
* str.upto(other_str, exclusive=false) {|s| block } => str
*
* Iterates through successive values, starting at <i>str</i> and
* ending at <i>other_str</i> inclusive, passing each value in turn to
* the block. The <code>String#succ</code> method is used to generate
* each value. If optional second arguent excle is omitted or is <code>false</code>,
* the last value will be included; otherwise it will be excluded.
*
* "a8".upto("b6") {|s| print s, ' ' }
* for s in "a8".."b6"
* print s, ' '
* end
*
* <em>produces:</em>
*
* a8 a9 b0 b1 b2 b3 b4 b5 b6
* a8 a9 b0 b1 b2 b3 b4 b5 b6
*/
static VALUE
rb_str_upto(int argc, VALUE *argv, VALUE beg)
{
VALUE end, exclusive;
VALUE current, after_end;
ID succ;
int n, excl;
rb_scan_args(argc, argv, "11", &end, &exclusive);
excl = RTEST(exclusive);
succ = rb_intern("succ");
StringValue(end); StringValue(end);
n = rb_str_cmp(beg, end); n = rb_str_cmp(beg, end);
if (n > 0 || (excl && n == 0)) return beg; if (n > 0 || (excl && n == 0)) return beg;
@ -1575,33 +1601,6 @@ rb_str_upto(VALUE beg, VALUE end, int excl)
return beg; return beg;
} }
/*
* call-seq:
* str.upto(other_str) {|s| block } => str
*
* Iterates through successive values, starting at <i>str</i> and
* ending at <i>other_str</i> inclusive, passing each value in turn to
* the block. The <code>String#succ</code> method is used to generate
* each value.
*
* "a8".upto("b6") {|s| print s, ' ' }
* for s in "a8".."b6"
* print s, ' '
* end
*
* <em>produces:</em>
*
* a8 a9 b0 b1 b2 b3 b4 b5 b6
* a8 a9 b0 b1 b2 b3 b4 b5 b6
*/
static VALUE
rb_str_upto_m(VALUE beg, VALUE end)
{
return rb_str_upto(beg, end, Qfalse);
}
static VALUE static VALUE
rb_str_subpat(VALUE str, VALUE re, int nth) rb_str_subpat(VALUE str, VALUE re, int nth)
{ {
@ -4911,7 +4910,7 @@ Init_String(void)
rb_define_method(rb_cString, "succ!", rb_str_succ_bang, 0); rb_define_method(rb_cString, "succ!", rb_str_succ_bang, 0);
rb_define_method(rb_cString, "next", rb_str_succ, 0); rb_define_method(rb_cString, "next", rb_str_succ, 0);
rb_define_method(rb_cString, "next!", rb_str_succ_bang, 0); rb_define_method(rb_cString, "next!", rb_str_succ_bang, 0);
rb_define_method(rb_cString, "upto", rb_str_upto_m, 1); rb_define_method(rb_cString, "upto", rb_str_upto, -1);
rb_define_method(rb_cString, "index", rb_str_index_m, -1); rb_define_method(rb_cString, "index", rb_str_index_m, -1);
rb_define_method(rb_cString, "rindex", rb_str_rindex_m, -1); rb_define_method(rb_cString, "rindex", rb_str_rindex_m, -1);
rb_define_method(rb_cString, "replace", rb_str_replace, 1); rb_define_method(rb_cString, "replace", rb_str_replace, 1);

View file

@ -1,7 +1,7 @@
#define RUBY_VERSION "1.9.0" #define RUBY_VERSION "1.9.0"
#define RUBY_RELEASE_DATE "2007-06-21" #define RUBY_RELEASE_DATE "2007-06-23"
#define RUBY_VERSION_CODE 190 #define RUBY_VERSION_CODE 190
#define RUBY_RELEASE_CODE 20070621 #define RUBY_RELEASE_CODE 20070623
#define RUBY_PATCHLEVEL 0 #define RUBY_PATCHLEVEL 0
#define RUBY_VERSION_MAJOR 1 #define RUBY_VERSION_MAJOR 1
@ -9,7 +9,7 @@
#define RUBY_VERSION_TEENY 0 #define RUBY_VERSION_TEENY 0
#define RUBY_RELEASE_YEAR 2007 #define RUBY_RELEASE_YEAR 2007
#define RUBY_RELEASE_MONTH 6 #define RUBY_RELEASE_MONTH 6
#define RUBY_RELEASE_DAY 21 #define RUBY_RELEASE_DAY 23
#ifdef RUBY_EXTERN #ifdef RUBY_EXTERN
RUBY_EXTERN const char ruby_version[]; RUBY_EXTERN const char ruby_version[];