aboutsummaryrefslogtreecommitdiffstats
path: root/string.c
diff options
context:
space:
mode:
authorko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-12-07 15:10:00 +0000
committerko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-12-07 15:10:00 +0000
commit6e3f5ffa778a5d2c36ffcc422b1c9742fa5eb16b (patch)
tree025393fd97224c56104624276b716058c695cddd /string.c
parent7f9827e4b3e76f215fa9fd36f24fd8e1fdef9866 (diff)
downloadruby-6e3f5ffa778a5d2c36ffcc422b1c9742fa5eb16b.tar.gz
* string.c: introduce String#+@ and String#-@ to control
String mutability. [Feature #11782] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52917 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/string.c b/string.c
index 6d283dd145..a9fb005367 100644
--- a/string.c
+++ b/string.c
@@ -2227,6 +2227,46 @@ rb_str_freeze(VALUE str)
return rb_obj_freeze(str);
}
+
+/*
+ * call-seq:
+ * +str -> str (mutable)
+ *
+ * If the string is frozen, then return duplicated mutable string.
+ *
+ * If the string is not frozen, then return the string itself.
+ */
+static VALUE
+str_uplus(VALUE str)
+{
+ if (OBJ_FROZEN(str)) {
+ return rb_str_dup(str);
+ }
+ else {
+ return str;
+ }
+}
+
+/*
+ * call-seq:
+ * -str -> str (frozen)
+ *
+ * If the string is frozen, then return the string itself.
+ *
+ * If the string is not frozen, then duplicate the string
+ * freeze it and return it.
+ */
+static VALUE
+str_uminus(VALUE str)
+{
+ if (OBJ_FROZEN(str)) {
+ return str;
+ }
+ else {
+ return rb_str_freeze(rb_str_dup(str));
+ }
+}
+
RUBY_ALIAS_FUNCTION(rb_str_dup_frozen(VALUE str), rb_str_new_frozen, (str))
#define rb_str_dup_frozen rb_str_new_frozen
@@ -9295,6 +9335,8 @@ Init_String(void)
rb_define_method(rb_cString, "scrub", str_scrub, -1);
rb_define_method(rb_cString, "scrub!", str_scrub_bang, -1);
rb_define_method(rb_cString, "freeze", rb_str_freeze, 0);
+ rb_define_method(rb_cString, "+@", str_uplus, 0);
+ rb_define_method(rb_cString, "-@", str_uminus, 0);
rb_define_method(rb_cString, "to_i", rb_str_to_i, -1);
rb_define_method(rb_cString, "to_f", rb_str_to_f, 0);