summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2014-06-07 22:28:42 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2014-06-07 22:28:42 +0900
commit6459a984489e3674d4cb108c3b252525b8fd7068 (patch)
tree4eb1ae910362cca02e48455538e00d2b2d6fa886
parent76012a885d40dd7a5451b747ca3f23ca39595faa (diff)
downloadmruby-6459a984489e3674d4cb108c3b252525b8fd7068.tar.gz
mruby-6459a984489e3674d4cb108c3b252525b8fd7068.zip
move String#clear to mruby-string-ext; ref #2370
-rw-r--r--mrbgems/mruby-string-ext/src/string.c32
-rw-r--r--mrbgems/mruby-string-ext/test/string.rb21
-rw-r--r--src/string.c32
-rw-r--r--test/t/string.rb21
4 files changed, 53 insertions, 53 deletions
diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c
index f04f12c4b..48146944d 100644
--- a/mrbgems/mruby-string-ext/src/string.c
+++ b/mrbgems/mruby-string-ext/src/string.c
@@ -239,6 +239,37 @@ mrb_str_lines(mrb_state *mrb, mrb_value self)
return result;
}
+/*
+ * call-seq:
+ * string.clear -> string
+ *
+ * Makes string empty.
+ *
+ * a = "abcde"
+ * a.clear #=> ""
+ */
+static mrb_value
+mrb_str_clear(mrb_state *mrb, mrb_value str)
+{
+ struct RString *s = mrb_str_ptr(str);
+
+ if (!RSTR_SHARED_P(s) && !RSTR_EMBED_P(s)) {
+ if (s->flags & MRB_STR_NOFREE) {
+ s->flags &= ~MRB_STR_NOFREE;
+ }
+ else {
+ mrb_free(mrb, s->as.heap.ptr);
+ }
+ s->as.heap.ptr = 0;
+ s->as.heap.len = 0;
+ }
+ RSTR_UNSET_SHARED_FLAG(s);
+ RSTR_SET_EMBED_FLAG(s);
+ RSTR_SET_EMBED_LEN(s, 0);
+ RSTRING_PTR(str)[0] = '\0';
+ return str;
+}
+
void
mrb_mruby_string_ext_gem_init(mrb_state* mrb)
{
@@ -256,6 +287,7 @@ mrb_mruby_string_ext_gem_init(mrb_state* mrb)
mrb_define_method(mrb, s, "oct", mrb_str_oct, MRB_ARGS_NONE());
mrb_define_method(mrb, s, "chr", mrb_str_chr, MRB_ARGS_NONE());
mrb_define_method(mrb, s, "lines", mrb_str_lines, MRB_ARGS_NONE());
+ mrb_define_method(mrb, s, "clear", mrb_str_clear, MRB_ARGS_NONE());
}
void
diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb
index 0ef92b35a..72c919b35 100644
--- a/mrbgems/mruby-string-ext/test/string.rb
+++ b/mrbgems/mruby-string-ext/test/string.rb
@@ -172,3 +172,24 @@ assert('String#lines') do
assert_equal ["\n", "\n", "\n"], "\n\n\n".lines
assert_equal [], "".lines
end
+
+assert('String#clear') do
+ # embed string
+ s = "foo"
+ assert_equal("", s.clear)
+ assert_equal("", s)
+
+ # not embed string and not shared string
+ s = "foo" * 100
+ a = s
+ assert_equal("", s.clear)
+ assert_equal("", s)
+ assert_equal("", a)
+
+ # shared string
+ s = "foo" * 100
+ a = s[10, 90] # create shared string
+ assert_equal("", s.clear) # clear
+ assert_equal("", s) # s is cleared
+ assert_not_equal("", a) # a should not be affected
+end
diff --git a/src/string.c b/src/string.c
index 8628274cb..0fe79e61d 100644
--- a/src/string.c
+++ b/src/string.c
@@ -2497,37 +2497,6 @@ mrb_str_bytes(mrb_state *mrb, mrb_value str)
return a;
}
-/*
- * call-seq:
- * string.clear -> string
- *
- * Makes string empty.
- *
- * a = "abcde"
- * a.clear #=> ""
- */
-static mrb_value
-mrb_str_clear(mrb_state *mrb, mrb_value str)
-{
- struct RString *s = mrb_str_ptr(str);
-
- if (!RSTR_SHARED_P(s) && !RSTR_EMBED_P(s)) {
- if (s->flags & MRB_STR_NOFREE) {
- s->flags &= ~MRB_STR_NOFREE;
- }
- else {
- mrb_free(mrb, s->as.heap.ptr);
- }
- s->as.heap.ptr = 0;
- s->as.heap.len = 0;
- }
- RSTR_UNSET_SHARED_FLAG(s);
- RSTR_SET_EMBED_FLAG(s);
- RSTR_SET_EMBED_LEN(s, 0);
- RSTRING_PTR(str)[0] = '\0';
- return str;
-}
-
/* ---------------------------*/
void
mrb_init_string(mrb_state *mrb)
@@ -2579,5 +2548,4 @@ mrb_init_string(mrb_state *mrb)
mrb_define_method(mrb, s, "upcase!", mrb_str_upcase_bang, MRB_ARGS_REQ(1)); /* 15.2.10.5.43 */
mrb_define_method(mrb, s, "inspect", mrb_str_inspect, MRB_ARGS_NONE()); /* 15.2.10.5.46(x) */
mrb_define_method(mrb, s, "bytes", mrb_str_bytes, MRB_ARGS_NONE());
- mrb_define_method(mrb, s, "clear", mrb_str_clear, MRB_ARGS_NONE());
}
diff --git a/test/t/string.rb b/test/t/string.rb
index 779a74791..5ecb51530 100644
--- a/test/t/string.rb
+++ b/test/t/string.rb
@@ -515,24 +515,3 @@ assert('String#each_byte') do
assert_equal bytes1, bytes2
end
-
-assert('String#clear') do
- # embed string
- s = "foo"
- assert_equal("", s.clear)
- assert_equal("", s)
-
- # not embed string and not shared string
- s = "foo" * 100
- a = s
- assert_equal("", s.clear)
- assert_equal("", s)
- assert_equal("", a)
-
- # shared string
- s = "foo" * 100
- a = s[10, 90] # create shared string
- assert_equal("", s.clear) # clear
- assert_equal("", s) # s is cleared
- assert_not_equal("", a) # a should not be affected
-end