summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-18 11:16:56 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-05-18 11:16:56 +0000
commit05c0a22854312d528965ef5866cf5d840b42bdad (patch)
tree78b8218aab42e94c9f516c92df605e4bac8b9935 /string.c
parentbfec5ad41f808b37d5bc248fdcbc595c367be9e6 (diff)
* string.c (str_scrub_bang): add String#scrub!. [Feature #8414]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40810 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/string.c b/string.c
index 59ed1b2dc4..b8f1ab4f9d 100644
--- a/string.c
+++ b/string.c
@@ -8014,6 +8014,28 @@ rb_str_scrub(int argc, VALUE *argv, VALUE str)
}
}
+/*
+ * call-seq:
+ * str.scrub! -> str
+ * str.scrub!(repl) -> str
+ * str.scrub!{|bytes|} -> str
+ *
+ * If the string is invalid byte sequence then replace invalid bytes with given replacement
+ * character, else returns self.
+ * If block is given, replace invalid bytes with returned value of the block.
+ *
+ * "abc\u3042\x81".scrub! #=> "abc\u3042\uFFFD"
+ * "abc\u3042\x81".scrub!("*") #=> "abc\u3042*"
+ * "abc\u3042\xE3\x80".scrub!{|bytes| '<'+bytes.unpack('H*')[0]+'>' } #=> "abc\u3042<e380>"
+ */
+VALUE
+rb_str_scrub_bang(int argc, VALUE *argv, VALUE str)
+{
+ VALUE new = rb_str_scrub(argc, argv, str);
+ rb_str_replace(str, new);
+ return str;
+}
+
/**********************************************************************
* Document-class: Symbol
*
@@ -8500,6 +8522,7 @@ Init_String(void)
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!", rb_str_scrub_bang, -1);
rb_define_method(rb_cString, "to_i", rb_str_to_i, -1);
rb_define_method(rb_cString, "to_f", rb_str_to_f, 0);