Skip to content

Commit d2c8638

Browse files
committed
[GR-18163] Return new String instances from File.basename
PullRequest: truffleruby/2631
2 parents f2e97df + a342767 commit d2c8638

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Compatibility:
2323
* Configure `mandir` value in `RbConfig::CONFIG` and `RbConfig::MAKEFILE_CONFIG` (#2315).
2424
* TruffleRuby now supports the Truffle polyglot Hash interop API.
2525
* Implement `Fiber#raise` (#2338).
26+
* Update `File.basename` to return new `String` instances (#2343).
2627

2728
Performance:
2829

spec/ruby/core/file/basename_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,20 @@
164164
basename.encoding.should == Encoding::Windows_1250
165165
end
166166

167+
it "returns a new unfrozen String" do
168+
exts = [nil, '.rb', '.*', '.txt']
169+
['foo.rb','//', '/test/', 'test'].each do |example|
170+
exts.each do |ext|
171+
original = example.freeze
172+
result = if ext
173+
File.basename(original, ext)
174+
else
175+
File.basename(original)
176+
end
177+
result.should_not equal(original)
178+
result.frozen?.should == false
179+
end
180+
end
181+
end
167182

168183
end

src/main/ruby/truffleruby/core/file.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,20 +183,20 @@ def self.basename(path, ext=undefined)
183183
end
184184

185185
# edge case, it's all /'s, return "/"
186-
return slash unless found
186+
return slash.dup unless found
187187

188188
# Now that we've trimmed the /'s at the end, search again
189189
pos = Primitive.find_string_reverse(path, slash, path.bytesize)
190190
if ext_not_present and !pos
191191
# No /'s found and ext not present, return path.
192-
return path
192+
return path.dup
193193
end
194194
end
195195

196196
path = path.byteslice(pos + 1, path.bytesize - pos) if pos
197197
end
198198

199-
return path if ext_not_present
199+
return path.dup if ext_not_present
200200

201201
# special case. if ext is ".*", remove any extension
202202

@@ -213,7 +213,7 @@ def self.basename(path, ext=undefined)
213213
end
214214
end
215215

216-
path
216+
path.dup
217217
end
218218

219219
def self.query_stat_mode(path)

0 commit comments

Comments
 (0)