extension: Print empty entry if there is no extension

Because we now count the extension including the ".", we print an
empty entry.

This makes e.g.

```fish
set -l base (path change-extension '' $somefile)
set -l ext (path extension $somefile)
echo $base$ext
```

reconstruct the filename, and makes it easier to deal with files with
no extension.
This commit is contained in:
Fabian Homborg 2022-04-07 15:16:05 +02:00
parent 5cce6d01ad
commit 640bd7b183
2 changed files with 14 additions and 1 deletions

View file

@ -593,7 +593,12 @@ static int path_extension(parser_t &parser, io_streams_t &streams, int argc, con
while (const wcstring *arg = aiter.nextstr()) {
auto pos = find_extension(*arg);
if (!pos) continue;
if (!pos) {
// If there is no extension the extension is empty.
// This is unambiguous because we include the ".".
path_out(streams, opts, L"");
continue;
}
wcstring ext = arg->substr(*pos);
if (opts.quiet && !ext.empty()) {

View file

@ -4,20 +4,24 @@
# Extension - for figuring out the file extension of a given path.
path extension /
or echo None
# CHECK:
# CHECK: None
# No extension
path extension /.
or echo Filename is just a dot, no extension
# CHECK:
# CHECK: Filename is just a dot, no extension
# No extension - ".foo" is the filename
path extension /.foo
or echo None again
# CHECK:
# CHECK: None again
path extension /foo
or echo None once more
# CHECK:
# CHECK: None once more
path extension /foo.txt
and echo Success
@ -25,17 +29,21 @@ and echo Success
# CHECK: Success
path extension /foo.txt/bar
or echo Not even here
# CHECK:
# CHECK: Not even here
path extension . ..
or echo No extension
# CHECK:
# CHECK: No extension
path extension ./foo.mp4
# CHECK: .mp4
path extension ../banana
# CHECK:
# nothing, status 1
echo $status
# CHECK: 1
path extension ~/.config
# CHECK:
# nothing, status 1
echo $status
# CHECK: 1