Skip to content

Commit 1823b3a

Browse files
committed
Fix File.extname to return unfrozen strings
1 parent 2990292 commit 1823b3a

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

spec/ruby/core/file/extname_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
File.extname(".app.conf").should == ".conf"
1313
end
1414

15+
it "returns unfrozen strings" do
16+
File.extname("foo.rb").frozen?.should == false
17+
File.extname("/foo/bar.rb").frozen?.should == false
18+
File.extname("/foo.rb/bar.c").frozen?.should == false
19+
File.extname("bar").frozen?.should == false
20+
File.extname(".bashrc").frozen?.should == false
21+
File.extname("/foo.bar/baz").frozen?.should == false
22+
File.extname(".app.conf").frozen?.should == false
23+
end
24+
1525
it "returns the extension for edge cases" do
1626
File.extname("").should == ""
1727
File.extname(".").should == ""

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,21 +484,21 @@ def self.extname(path)
484484
dot_idx = Primitive.find_string_reverse(path, '.', path_size)
485485

486486
# No dots at all
487-
return '' unless dot_idx
487+
return +'' unless dot_idx
488488

489489
slash_idx = Primitive.find_string_reverse(path, '/', path_size)
490490

491491
# pretend there is / just to the left of the start of the string
492492
slash_idx ||= -1
493493

494494
# no . in the last component of the path
495-
return '' if dot_idx < slash_idx
495+
return +'' if dot_idx < slash_idx
496496

497497
# last component starts with a .
498-
return '' if dot_idx == slash_idx + 1
498+
return +'' if dot_idx == slash_idx + 1
499499

500500
# last component ends with a .
501-
return '' if dot_idx == path_size - 1
501+
return +'' if dot_idx == path_size - 1
502502

503503
path.byteslice(dot_idx, path_size - dot_idx)
504504
end

0 commit comments

Comments
 (0)