From 5a189cbe66a5ef632d8a5e1c8ba5df6cbcc4caa9 Mon Sep 17 00:00:00 2001 From: Cody Byrnes Date: Fri, 9 Mar 2018 06:09:41 -0800 Subject: [PATCH] Fix: File.extname edge case for dot in path with no extension (#5790) --- spec/std/file_spec.cr | 6 ++++++ src/file.cr | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/spec/std/file_spec.cr b/spec/std/file_spec.cr index fe6c1f520edc..19e4f4f527a0 100644 --- a/spec/std/file_spec.cr +++ b/spec/std/file_spec.cr @@ -290,6 +290,12 @@ describe "File" do File.extname("/foo/bar/.profile").should eq("") File.extname("/foo/bar/.profile.sh").should eq(".sh") File.extname("/foo/bar/foo.").should eq("") + File.extname("/foo.bar/baz").should eq("") + File.extname("test.cr").should eq(".cr") + File.extname("test.cr.cz").should eq(".cz") + File.extname(".test").should eq("") + File.extname(".test.cr").should eq(".cr") + File.extname(".test.cr.cz").should eq(".cz") File.extname("test").should eq("") end diff --git a/src/file.cr b/src/file.cr index 67766919d514..53e8f35ceaf1 100644 --- a/src/file.cr +++ b/src/file.cr @@ -298,7 +298,7 @@ class File < IO::FileDescriptor dot_index = filename.rindex('.') - if dot_index && dot_index != filename.size - 1 && filename[dot_index - 1] != SEPARATOR + if dot_index && dot_index != filename.size - 1 && dot_index - 1 > (filename.rindex(SEPARATOR) || 0) filename[dot_index, filename.size - dot_index] else ""