From f64dd7e2278f35b1cb4437d2ddb3185919977fe2 Mon Sep 17 00:00:00 2001 From: Cody Byrnes Date: Thu, 8 Mar 2018 07:09:41 -0800 Subject: [PATCH 1/4] Fixed File.extname edge case for . in path, with no extension --- spec/std/file_spec.cr | 1 + src/file.cr | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/std/file_spec.cr b/spec/std/file_spec.cr index fe6c1f520edc..7a05a8e53b31 100644 --- a/spec/std/file_spec.cr +++ b/spec/std/file_spec.cr @@ -290,6 +290,7 @@ 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").should eq("") end diff --git a/src/file.cr b/src/file.cr index 67766919d514..382207cdc8e3 100644 --- a/src/file.cr +++ b/src/file.cr @@ -297,8 +297,9 @@ class File < IO::FileDescriptor filename.check_no_null_byte dot_index = filename.rindex('.') + separator_index = filename.rindex(SEPARATOR) - if dot_index && dot_index != filename.size - 1 && filename[dot_index - 1] != SEPARATOR + if dot_index && dot_index > separator_index && dot_index != filename.size - 1 && filename[dot_index - 1] != SEPARATOR filename[dot_index, filename.size - dot_index] else "" From e2a07c424447e4dcda88a28a3386739603cfd671 Mon Sep 17 00:00:00 2001 From: Cody Byrnes Date: Thu, 8 Mar 2018 07:33:18 -0800 Subject: [PATCH 2/4] Addressed nil value in comparison --- src/file.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/file.cr b/src/file.cr index 382207cdc8e3..c005cb423b48 100644 --- a/src/file.cr +++ b/src/file.cr @@ -297,7 +297,7 @@ class File < IO::FileDescriptor filename.check_no_null_byte dot_index = filename.rindex('.') - separator_index = filename.rindex(SEPARATOR) + separator_index = filename.rindex(SEPARATOR) || 0 if dot_index && dot_index > separator_index && dot_index != filename.size - 1 && filename[dot_index - 1] != SEPARATOR filename[dot_index, filename.size - dot_index] From 793ac0649dbb348a0f7e6ebb09be0db19609b428 Mon Sep 17 00:00:00 2001 From: Cody Byrnes Date: Thu, 8 Mar 2018 12:14:46 -0800 Subject: [PATCH 3/4] Consolidated logic around . location --- spec/std/file_spec.cr | 5 +++++ src/file.cr | 8 +++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/spec/std/file_spec.cr b/spec/std/file_spec.cr index 7a05a8e53b31..19e4f4f527a0 100644 --- a/spec/std/file_spec.cr +++ b/spec/std/file_spec.cr @@ -291,6 +291,11 @@ describe "File" do 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 c005cb423b48..fa716cf45cdd 100644 --- a/src/file.cr +++ b/src/file.cr @@ -297,13 +297,11 @@ class File < IO::FileDescriptor filename.check_no_null_byte dot_index = filename.rindex('.') - separator_index = filename.rindex(SEPARATOR) || 0 - if dot_index && dot_index > separator_index && dot_index != filename.size - 1 && filename[dot_index - 1] != SEPARATOR - filename[dot_index, filename.size - dot_index] - else - "" + if dot_index && dot_index != filename.size - 1 && dot_index - 1 > (filename.rindex(SEPARATOR) || 0) + return filename[dot_index, filename.size - dot_index] end + "" end # Converts *path* to an absolute path. Relative paths are From c0461ce5b008539a7271958675d0dd922ce2c1f6 Mon Sep 17 00:00:00 2001 From: Cody Byrnes Date: Thu, 8 Mar 2018 14:27:32 -0800 Subject: [PATCH 4/4] removed extraneous return --- src/file.cr | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/file.cr b/src/file.cr index fa716cf45cdd..53e8f35ceaf1 100644 --- a/src/file.cr +++ b/src/file.cr @@ -299,9 +299,10 @@ class File < IO::FileDescriptor dot_index = filename.rindex('.') if dot_index && dot_index != filename.size - 1 && dot_index - 1 > (filename.rindex(SEPARATOR) || 0) - return filename[dot_index, filename.size - dot_index] + filename[dot_index, filename.size - dot_index] + else + "" end - "" end # Converts *path* to an absolute path. Relative paths are