Bug #15244
closedMethod #extname return empty string if filename is dot ('.')
Description
Files
Updated by shevegen (Robert A. Heiler) about 6 years ago
Is this a real problem?
How many people have had entries with more leading '.'?
File.extname('foobar......jpg') # => ".jpg"
File.extname('......jpg') # => ""
It's not that I am not inclined to agree about it; I just think
it's a bit strange to want to change that behaviour. Do people
use lots of '.' for names to files or directories or symlinks?
Updated by ahorek (Pavel Rosický) about 6 years ago
IMO
File.extname('..jpg')
should be
=> ".jpg"
proposed
=> "."
or the current behaviour
=> ""
makes no sense
Updated by TiSer (Sergey Ti) about 6 years ago
ahorek (Pavel Rosický) wrote:
IMO
File.extname('..jpg')
should be
=> ".jpg"
proposed
=> "."
or the current behaviour
=> ""
makes no sense
Yes, sure. I'm sorry, I oversleep correct cases.
Updated.
Updated by nobu (Nobuyoshi Nakada) about 6 years ago
If File.extname("..jpg")
returns ".jpg"
, File.basename("..jpg", ".*")
should return "."
without that part, to restore the original name from from these results.
It is different from basename(1).
$ basename ..jpg '.*'
..jpg
Updated by TiSer (Sergey Ti) about 6 years ago
This is still present.
Updated by TiSer (Sergey Ti) almost 6 years ago
Still present.
Updated by TiSer (Sergey Ti) almost 6 years ago
Still present.
Updated by rushsteve1 (Steven vanZyl) over 5 years ago
- File dot-names.diff added
The issue seemed to be in how .dotfiles were handled, where all leading periods were trimmed when only the first should have been.
I've created a patch that seems to fix the issue and added a spect test for it. It's only a 2 line change.
This is my first patch submission so any and all feedback would be greatly appreciated.
Updated by rushsteve1 (Steven vanZyl) over 5 years ago
- ruby -v changed from 2.5.0p0 to 2.7.0dev
Updated by rushsteve1 (Steven vanZyl) over 5 years ago
- File dot-names.diff dot-names.diff added
Updated the diff to also account for the if (*p) case so that it will stop if p == 0.
Updated by rushsteve1 (Steven vanZyl) over 5 years ago
- File deleted (
dot-names.diff)
Updated by jeremyevans0 (Jeremy Evans) over 5 years ago
- Related to Bug #15267: File.basename + File.extname does not restore the original name added
Updated by jeremyevans0 (Jeremy Evans) over 5 years ago
- File multiple-leading-dot-basename-extname-15244.patch multiple-leading-dot-basename-extname-15244.patch added
I agree that this is a bug. The comparison that nobu made with basename(1)
is relevant, but unlike File.basename
, basename(1)
does not handle .*
specially. You can see that basename(1)
does actually strip the extension even for multiple leading periods:
basename '..*' '.*'
.
Attached is a patch that fixes the issue, both for File.basename
and File.extname
. With the patch:
File.basename('..jpg', '.*')
# => "."
File.extname('..jpg')
# => ".jpg"
Updated by matz (Yukihiro Matsumoto) about 5 years ago
- Status changed from Open to Feedback
.git
is not an extension but a filename. .a.jpg
has .jpg
extension. I wonder how we can parse ..jpg
. It's undefined behavior for me.
Any reasoning?
Matz.
Updated by znz (Kazuhiro NISHIYAMA) about 5 years ago
In some other languages:
% python
Python 2.7.16 (default, Mar 4 2019, 09:01:38)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path.splitext('..jpg')
('..jpg', '')
% python3
Python 3.7.4 (default, Jul 9 2019, 18:13:23)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path.splitext('..jpg')
('..jpg', '')
% php --version
PHP 7.1.23 (cli) (built: Feb 22 2019 22:19:32) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
% php -r 'var_export(pathinfo("..jpg", PATHINFO_EXTENSION));'
'jpg'
ref https://hydrocul.github.io/wiki/programming_languages_diff/io/extname.html
Updated by nobu (Nobuyoshi Nakada) about 5 years ago
znz (Kazuhiro NISHIYAMA) wrote:
% php --version PHP 7.1.23 (cli) (built: Feb 22 2019 22:19:32) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies % php -r 'var_export(pathinfo("..jpg", PATHINFO_EXTENSION));' 'jpg'
Seems not for us:
var_export(pathinfo(".profile", PATHINFO_EXTENSION)); //=> 'profile'
Updated by TiSer (Sergey Ti) about 5 years ago
Example from .NET:
(https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getextension)
class Program
{
static void Main(string[] args)
{
string ext = Path.GetExtension("..jpg");
Console.WriteLine(ext);
Console.ReadLine();
}
}
=> '.jpg'
Updated by jeremyevans0 (Jeremy Evans) over 4 years ago
- Status changed from Feedback to Closed