RFR: 8057113: (fs) Path should have a method to obtain the filename extension
Remi Forax
forax at univ-mlv.fr
Fri Jan 29 22:10:47 UTC 2021
----- Mail original -----
> De: "Brian Burkhalter" <bpb at openjdk.java.net>
> À: "nio-dev" <nio-dev at openjdk.java.net>
> Envoyé: Vendredi 29 Janvier 2021 22:32:52
> Objet: RFR: 8057113: (fs) Path should have a method to obtain the filename extension
> Please review this proposed change to add a method
> `java.nio.file.Path.getExtension()`. This was initially discussed in the thread
> http://mail.openjdk.java.net/pipermail/nio-dev/2018-February/004716.html. This
> method would return the filename extension of the file name of the `Path`. The
> extension is defined to be the portion of the file name after the last dot
> `(‘.’)`.
>
> The definitions of file extension for about fifteen platforms and languages were
> surveyed to try to find a reasonable compromise for the definition of
> extension. The most common definition was the last segment of the name
> including and after the last dot. The second definition omitted the last dot
> from the extension. Java-related platforms all exclude the last dot. (One
> divergent definition in the internal Java NIO method
> `AbstractFileTypeDetector.getExtension(String)` defines the extension as the
> part after the *first* dot.)
>
> All examined cases define the extension to be an empty string if it cannot be
> determined. None of these cases used `null` to represent an indeterminate
> extension.
>
> Little in the way of specifying behavior for special cases (consisting mainly of
> file names with one or more leading dots) was found. Most definitions concern
> themselves only with the last dot and what comes after it and ignore leading
> dots altogether. A few definitions ignore a leading dot at the zeroth
> character. The current proposal ignores a dot at character zero.
>
> The behavior of the proposed method for some example cases is as:
>
> . ->
> .. ->
> .a.b -> b
> ...... ->
> .....a -> a
> ....a.b -> b
> ..foo -> foo
> test.rb -> rb
> a/b/d/test.rb -> rb
> .a/b/d/test.rb -> rb
> foo. ->
> test ->
> .profile ->
> .profile.sh -> sh
> ..foo -> foo
> .....foo -> foo
> .vimrc ->
> test. ->
> test.. ->
> test... ->
> image.jpg -> jpg
> music.mp3 -> mp3
> video.mp4 -> mp4
> document.txt -> txt
>
> If the specification can be agreed upon, then a test will be added to this PR
> and a corresponding CSR will be filed.
Thanks for doing this !
Usually in JDK codes, the return value is returned early instead of being stored in a local variable and then returned.
So a code like below is more usual.
default String getExtension() {
String name = getFileName().toString();
int length = name.length();
// Indeterminate if name is too short or equal to "..".
if (length > 1 && !name.equals("..")) {
int lastDotIndex = name.lastIndexOf('.');
// Indeterminate if no dot or found at last or only the first index
if (lastDotIndex > 0 && lastDotIndex < length - 1) {
return name.substring(lastDotIndex + 1);
}
}
return "";
}
regards,
Rémi
>
> -------------
>
> Commit messages:
> - 8057113: (fs) Path should have a method to obtain the filename extension
>
> Changes: https://git.openjdk.java.net/jdk/pull/2319/files
> Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=2319&range=00
> Issue: https://bugs.openjdk.java.net/browse/JDK-8057113
> Stats: 37 lines in 1 file changed: 36 ins; 0 del; 1 mod
> Patch: https://git.openjdk.java.net/jdk/pull/2319.diff
> Fetch: git fetch https://git.openjdk.java.net/jdk pull/2319/head:pull/2319
>
> PR: https://git.openjdk.java.net/jdk/pull/2319
More information about the nio-dev
mailing list