Skip to content

Commit

Permalink
Merge pull request #297 from timcraft/string-uplus
Browse files Browse the repository at this point in the history
  • Loading branch information
seven1m authored Dec 15, 2021
2 parents fe8f9ae + 52cadbc commit 47e9b9a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/natalie/string_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class StringObject : public Object {
Value strip(Env *) const;
Value to_i(Env *, Value) const;
Value upcase(Env *);
Value uplus(Env *);

template <typename... Args>
static StringObject *format(Env *env, const char *fmt, Args... args) {
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 @@ -718,6 +718,7 @@ def generate_name

gen.binding('String', '*', 'StringObject', 'mul', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('String', '+', 'StringObject', 'add', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('String', '+@', 'StringObject', 'uplus', argc: 0, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('String', '<<', 'StringObject', 'ltlt', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('String', '<=>', 'StringObject', 'cmp', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('String', '==', 'StringObject', 'eq', argc: 1, pass_env: true, pass_block: false, return_type: :bool)
Expand Down
23 changes: 23 additions & 0 deletions spec/core/string/uplus_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require_relative '../../spec_helper'

describe 'String#+@' do
it 'returns an unfrozen copy of a frozen String' do
input = 'foo'.freeze
output = +input

output.should_not.frozen?
output.should == 'foo'
end

it 'returns self if the String is not frozen' do
input = 'foo'
output = +input

output.equal?(input).should == true
end

# NATFIXME: Implement fixture method and add freeze_magic_comment.rb fixture
xit 'returns mutable copy despite freeze-magic-comment in file' do
ruby_exe(fixture(__FILE__, "freeze_magic_comment.rb")).should == 'mutable'
end
end
8 changes: 8 additions & 0 deletions src/string_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,14 @@ Value StringObject::upcase(Env *env) {
return str;
}

Value StringObject::uplus(Env *env) {
if (this->is_frozen()) {
return this->dup(env);
} else {
return this;
}
}

Value StringObject::reverse(Env *env) {
if (length() == 0)
return new StringObject {};
Expand Down

0 comments on commit 47e9b9a

Please sign in to comment.