Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make String#clear spec-compliant #522

Merged
merged 3 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/natalie/string_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down
1 change: 1 addition & 0 deletions lib/natalie/compiler/binding_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions spec/core/string/clear_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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

# 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
@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
8 changes: 8 additions & 0 deletions src/string_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ Value StringObject::mul(Env *env, Value arg) const {
return new_string;
}

Value StringObject::clear(Env *env) {
assert_not_frozen(env);

m_string.clear();

return this;
}

Value StringObject::cmp(Env *env, Value other) const {
if (other->type() != Object::Type::String) return NilObject::the();
auto *str = c_str();
Expand Down