From 48b62e66cdbca267080274d7d76947073d7f58d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Tue, 6 Mar 2018 20:11:24 +0000 Subject: [PATCH] Fix File.extname to handle path with dot in directory name --- spec/std/file_spec.cr | 1 + src/file.cr | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/spec/std/file_spec.cr b/spec/std/file_spec.cr index fe6c1f520edc..cd143e8a77d6 100644 --- a/spec/std/file_spec.cr +++ b/spec/std/file_spec.cr @@ -291,6 +291,7 @@ describe "File" do File.extname("/foo/bar/.profile.sh").should eq(".sh") File.extname("/foo/bar/foo.").should eq("") File.extname("test").should eq("") + File.extname("/foo.bar/baz").should eq("") end it "constructs a path from parts" do diff --git a/src/file.cr b/src/file.cr index 67766919d514..96d816b6d9c1 100644 --- a/src/file.cr +++ b/src/file.cr @@ -294,12 +294,12 @@ class File < IO::FileDescriptor # File.extname("foo.cr") # => ".cr" # ``` def self.extname(filename) : String - filename.check_no_null_byte + filename = basename(filename) dot_index = filename.rindex('.') - if dot_index && dot_index != filename.size - 1 && filename[dot_index - 1] != SEPARATOR - filename[dot_index, filename.size - dot_index] + if dot_index && dot_index != 0 && dot_index != filename.size - 1 + filename[dot_index..-1] else "" end