From 423ca671306259789afb99486f087e6eb25f4b2d Mon Sep 17 00:00:00 2001 From: yugui Date: Wed, 17 Dec 2008 06:16:47 +0000 Subject: merges r20765 from trunk into ruby_1_9_1. * ruby.c (process_options): revive global sub, gsub, chop, chomp only when auto looping options (-p/-n) is specified. [ruby-core:20570] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_1@20820 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 +++ ruby.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 119 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index 81f3bf73ad..2651fa309b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Mon Dec 15 23:29:49 2008 Yukihiro Matsumoto + + * ruby.c (process_options): revive global sub, gsub, chop, chomp + only when auto looping options (-p/-n) is specified. + [ruby-core:20570] + Mon Dec 15 21:01:46 2008 Tanaka Akira * ext/pty/pty.c (chfunc): make it static. diff --git a/ruby.c b/ruby.c index 31ef74cb9d..6a4a9afd0c 100644 --- a/ruby.c +++ b/ruby.c @@ -1071,6 +1071,115 @@ opt_enc_index(VALUE enc_name) #define rb_progname (GET_VM()->progname) VALUE rb_argv0; +static VALUE +false_value(void) +{ + return Qfalse; +} + +static VALUE +true_value(void) +{ + return Qtrue; +} + +#define rb_define_readonly_boolean(name, val) \ + rb_define_virtual_variable((name), (val) ? true_value : false_value, 0) + +static VALUE +uscore_get() +{ + VALUE line; + + line = rb_lastline_get(); + if (TYPE(line) != T_STRING) { + rb_raise(rb_eTypeError, "$_ value need to be String (%s given)", + NIL_P(line) ? "nil" : rb_obj_classname(line)); + } + return line; +} + +/* + * call-seq: + * sub(pattern, replacement) => $_ + * sub(pattern) { block } => $_ + * + * Equivalent to $_.sub(args), except that + * $_ will be updated if substitution occurs. + * Available only when -p/-n command line option specified. + */ + +static VALUE +rb_f_sub(argc, argv) + int argc; + VALUE *argv; +{ + VALUE str = rb_funcall3(uscore_get(), rb_intern("sub"), argc, argv); + rb_lastline_set(str); + return str; +} + +/* + * call-seq: + * gsub(pattern, replacement) => string + * gsub(pattern) {|...| block } => string + * + * Equivalent to $_.gsub..., except that $_ + * receives the modified result. + * Available only when -p/-n command line option specified. + * + */ + +static VALUE +rb_f_gsub(argc, argv) + int argc; + VALUE *argv; +{ + VALUE str = rb_funcall3(uscore_get(), rb_intern("gsub"), argc, argv); + rb_lastline_set(str); + return str; +} + +/* + * call-seq: + * chop => string + * + * Equivalent to ($_.dup).chop!, except nil + * is never returned. See String#chop!. + * Available only when -p/-n command line option specified. + * + */ + +static VALUE +rb_f_chop() +{ + VALUE str = rb_funcall3(uscore_get(), rb_intern("chop"), 0, 0); + rb_lastline_set(str); + return str; +} + + +/* + * call-seq: + * chomp => $_ + * chomp(string) => $_ + * + * Equivalent to $_ = $_.chomp(string). See + * String#chomp. + * Available only when -p/-n command line option specified. + * + */ + +static VALUE +rb_f_chomp(argc, argv) + int argc; + VALUE *argv; +{ + VALUE str = rb_funcall3(uscore_get(), rb_intern("chomp"), argc, argv); + rb_lastline_set(str); + return str; +} + static VALUE process_options(VALUE arg) { @@ -1242,6 +1351,10 @@ process_options(VALUE arg) } if (opt->do_loop) { tree = rb_parser_while_loop(parser, tree, opt->do_line, opt->do_split); + rb_define_global_function("sub", rb_f_sub, -1); + rb_define_global_function("gsub", rb_f_gsub, -1); + rb_define_global_function("chop", rb_f_chop, 0); + rb_define_global_function("chomp", rb_f_chomp, -1); } iseq = rb_iseq_new_top(tree, rb_str_new2("
"), @@ -1634,21 +1747,6 @@ ruby_set_argv(int argc, char **argv) } } -static VALUE -false_value(void) -{ - return Qfalse; -} - -static VALUE -true_value(void) -{ - return Qtrue; -} - -#define rb_define_readonly_boolean(name, val) \ - rb_define_virtual_variable((name), (val) ? true_value : false_value, 0) - void * ruby_process_options(int argc, char **argv) { -- cgit v1.2.3