From adbdd97d28336b1be05d64d4aeda78ef1812a859 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 1 Nov 2013 07:55:56 +0000 Subject: string.c: export rb_str_scrub * string.c (rb_str_scrub): export with fixed length arguments, and allow nil as replacement string instead of omitting. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43500 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ include/ruby/intern.h | 1 + string.c | 20 ++++++++++---------- transcode.c | 11 +++-------- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index b9ec91c300..d22415931a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Fri Nov 1 16:55:52 2013 Nobuyoshi Nakada + + * string.c (rb_str_scrub): export with fixed length arguments, and + allow nil as replacement string instead of omitting. + Fri Nov 1 06:20:44 2013 KOSAKI Motohiro * thread.c (rb_mutex_struct): reduce rb_mutex_t size by 8 bytes diff --git a/include/ruby/intern.h b/include/ruby/intern.h index b294f2d0fa..030ba7b55b 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -774,6 +774,7 @@ VALUE rb_str_length(VALUE); long rb_str_offset(VALUE, long); size_t rb_str_capacity(VALUE); VALUE rb_str_ellipsize(VALUE, long); +VALUE rb_str_scrub(VALUE, VALUE); #if defined(__GNUC__) && !defined(__PCC__) #define rb_str_new_cstr(str) __extension__ ( \ { \ diff --git a/string.c b/string.c index 3c689bda40..191f846d36 100644 --- a/string.c +++ b/string.c @@ -7973,20 +7973,18 @@ str_compat_and_valid(VALUE str, rb_encoding *enc) * @param repl the replacement character * @return If given string is invalid, returns a new string. Otherwise, returns Qnil. */ -static VALUE -str_scrub0(int argc, VALUE *argv, VALUE str) +VALUE +rb_str_scrub(VALUE str, VALUE repl) { int cr = ENC_CODERANGE(str); rb_encoding *enc; int encidx; - VALUE repl; if (cr == ENC_CODERANGE_7BIT || cr == ENC_CODERANGE_VALID) return Qnil; enc = STR_ENC_GET(str); - rb_scan_args(argc, argv, "01", &repl); - if (argc != 0) { + if (!NIL_P(repl)) { repl = str_compat_and_valid(repl, enc); } @@ -8225,10 +8223,11 @@ str_scrub0(int argc, VALUE *argv, VALUE str) * "abc\u3042\x81".scrub("*") #=> "abc\u3042*" * "abc\u3042\xE3\x80".scrub{|bytes| '<'+bytes.unpack('H*')[0]+'>' } #=> "abc\u3042" */ -VALUE -rb_str_scrub(int argc, VALUE *argv, VALUE str) +static VALUE +str_scrub(int argc, VALUE *argv, VALUE str) { - VALUE new = str_scrub0(argc, argv, str); + VALUE repl = argc ? (rb_check_arity(argc, 0, 1), argv[0]) : Qnil; + VALUE new = rb_str_scrub(str, repl); return NIL_P(new) ? rb_str_dup(str): new; } @@ -8249,7 +8248,8 @@ rb_str_scrub(int argc, VALUE *argv, VALUE str) static VALUE str_scrub_bang(int argc, VALUE *argv, VALUE str) { - VALUE new = str_scrub0(argc, argv, str); + VALUE repl = argc ? (rb_check_arity(argc, 0, 1), argv[0]) : Qnil; + VALUE new = rb_str_scrub(str, repl); if (!NIL_P(new)) rb_str_replace(str, new); return str; } @@ -8743,7 +8743,7 @@ Init_String(void) rb_define_method(rb_cString, "getbyte", rb_str_getbyte, 1); rb_define_method(rb_cString, "setbyte", rb_str_setbyte, 2); rb_define_method(rb_cString, "byteslice", rb_str_byteslice, -1); - rb_define_method(rb_cString, "scrub", rb_str_scrub, -1); + rb_define_method(rb_cString, "scrub", str_scrub, -1); rb_define_method(rb_cString, "scrub!", str_scrub_bang, -1); rb_define_method(rb_cString, "to_i", rb_str_to_i, -1); diff --git a/transcode.c b/transcode.c index 4a86f84d1b..92fd806597 100644 --- a/transcode.c +++ b/transcode.c @@ -2660,8 +2660,6 @@ str_transcode_enc_args(VALUE str, volatile VALUE *arg1, volatile VALUE *arg2, return dencidx; } -VALUE rb_str_scrub(int argc, VALUE *argv, VALUE str); - static int str_transcode0(int argc, VALUE *argv, VALUE *self, int ecflags, VALUE ecopts) { @@ -2697,14 +2695,11 @@ str_transcode0(int argc, VALUE *argv, VALUE *self, int ecflags, VALUE ecopts) ECONV_XML_ATTR_QUOTE_DECORATOR)) == 0) { if (senc && senc == denc) { if (ecflags & ECONV_INVALID_MASK) { + VALUE rep = Qnil; if (!NIL_P(ecopts)) { - VALUE rep = rb_hash_aref(ecopts, sym_replace); - dest = rb_str_scrub(1, &rep, str); - } - else { - dest = rb_str_scrub(0, NULL, str); + rep = rb_hash_aref(ecopts, sym_replace); } - *self = dest; + *self = rb_str_scrub(str, rep); return dencidx; } return NIL_P(arg2) ? -1 : dencidx; -- cgit v1.2.3