From 0dc4998e38b58dd2f8e0467578783b6de20f135e Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Thu, 3 Mar 2022 21:42:41 +0000 Subject: [PATCH 1/3] Add String#clear spec --- spec/core/string/clear_spec.rb | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 spec/core/string/clear_spec.rb diff --git a/spec/core/string/clear_spec.rb b/spec/core/string/clear_spec.rb new file mode 100644 index 000000000..e1d68e03b --- /dev/null +++ b/spec/core/string/clear_spec.rb @@ -0,0 +1,37 @@ +require_relative '../../spec_helper' + +describe "String#clear" do + before :each do + @s = "Jolene" + end + + it "sets self equal to the empty String" do + @s.clear + @s.should == "" + end + + it "returns self after emptying it" do + cleared = @s.clear + cleared.should == "" + cleared.should equal @s + end + + it "preserves its encoding" do + @s.encode!(Encoding::SHIFT_JIS) + @s.encoding.should == Encoding::SHIFT_JIS + @s.clear.encoding.should == Encoding::SHIFT_JIS + @s.encoding.should == Encoding::SHIFT_JIS + end + + it "works with multibyte Strings" do + s = "\u{9765}\u{876}" + s.clear + s.should == "" + end + + it "raises a FrozenError if self is frozen" do + @s.freeze + -> { @s.clear }.should raise_error(FrozenError) + -> { "".freeze.clear }.should raise_error(FrozenError) + end +end From a562595a3f2a176ce9386a69adaf16c657eca567 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Thu, 3 Mar 2022 21:43:54 +0000 Subject: [PATCH 2/3] Add binding for String#clear --- include/natalie/string_object.hpp | 1 + lib/natalie/compiler/binding_gen.rb | 1 + src/string_object.cpp | 4 ++++ 3 files changed, 6 insertions(+) diff --git a/include/natalie/string_object.hpp b/include/natalie/string_object.hpp index becc5d743..80447afc3 100644 --- a/include/natalie/string_object.hpp +++ b/include/natalie/string_object.hpp @@ -158,6 +158,7 @@ class StringObject : public Object { Value bytes(Env *, Block *); Value center(Env *, Value, Value); Value chr(Env *); + Value clear(Env *); Value cmp(Env *, Value) const; Value concat(Env *env, size_t argc, Value *args); Value downcase(Env *); diff --git a/lib/natalie/compiler/binding_gen.rb b/lib/natalie/compiler/binding_gen.rb index a5e8712cf..fc55a0373 100644 --- a/lib/natalie/compiler/binding_gen.rb +++ b/lib/natalie/compiler/binding_gen.rb @@ -841,6 +841,7 @@ def generate_name gen.binding('String', 'center', 'StringObject', 'center', argc: 1..2, pass_env: true, pass_block: false, return_type: :Object) gen.binding('String', 'chars', 'StringObject', 'chars', argc: 0, pass_env: true, pass_block: false, return_type: :Object) gen.binding('String', 'chr', 'StringObject', 'chr', argc: 0, pass_env: true, pass_block: false, return_type: :Object) +gen.binding('String', 'clear', 'StringObject', 'clear', argc: 0, pass_env: true, pass_block: false, return_type: :Object) gen.binding('String', 'concat', 'StringObject', 'concat', argc: :any, pass_env: true, pass_block: false, return_type: :Object) gen.binding('String', 'downcase', 'StringObject', 'downcase', argc: 0, pass_env: true, pass_block: false, return_type: :Object) gen.binding('String', 'each_byte', 'StringObject', 'each_byte', argc: 0, pass_env: true, pass_block: true, return_type: :Object) diff --git a/src/string_object.cpp b/src/string_object.cpp index 0170a9cce..41e60eb2c 100644 --- a/src/string_object.cpp +++ b/src/string_object.cpp @@ -285,6 +285,10 @@ Value StringObject::mul(Env *env, Value arg) const { return new_string; } +Value StringObject::clear(Env *env) { + return this; +} + Value StringObject::cmp(Env *env, Value other) const { if (other->type() != Object::Type::String) return NilObject::the(); auto *str = c_str(); From 040e453d07e3fb4e8902190cf70818c9006857f5 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Thu, 3 Mar 2022 21:44:54 +0000 Subject: [PATCH 3/3] Make String#clear spec-compliant --- spec/core/string/clear_spec.rb | 3 ++- src/string_object.cpp | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/spec/core/string/clear_spec.rb b/spec/core/string/clear_spec.rb index e1d68e03b..414f13ead 100644 --- a/spec/core/string/clear_spec.rb +++ b/spec/core/string/clear_spec.rb @@ -16,7 +16,8 @@ cleared.should equal @s end - it "preserves its encoding" do + # NATFIXME: Add back once we have encoding. + xit "preserves its encoding" do @s.encode!(Encoding::SHIFT_JIS) @s.encoding.should == Encoding::SHIFT_JIS @s.clear.encoding.should == Encoding::SHIFT_JIS diff --git a/src/string_object.cpp b/src/string_object.cpp index 41e60eb2c..dc7d04892 100644 --- a/src/string_object.cpp +++ b/src/string_object.cpp @@ -286,6 +286,10 @@ Value StringObject::mul(Env *env, Value arg) const { } Value StringObject::clear(Env *env) { + assert_not_frozen(env); + + m_string.clear(); + return this; }