Extract file extension: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: changed comments and whitespace.)
(→‎{{header|Perl 6}}: use the new test-cases, and compare output to the built-in .extension method)
Line 10: Line 10:




If your programming language (or standard library) has built-in functionality for extracting a filename extension, show how it would be used, and clearly state how its behavior differs from this specification.
If your programming language (or standard library) has built-in functionality for extracting a filename extension, show how it would be used and how exactly its behavior differs from this specification.


{{task heading|Specification}}
{{task heading|Specification}}
Line 31: Line 31:
|
|
|-
|-
| <code>characterModel.3D</code>
| <code>CharacterModel.3DS</code>
| <code>.3D</code>
| <code>.3DS</code>
|
|
|-
|-
Line 801: Line 801:
=={{header|Perl 6}}==
=={{header|Perl 6}}==


The built-in <code>.IO.extension</code> method can be used, but it...
The built-in <code>IO::Path</code> class has an <code>.extension</code> method:

<lang perl6>say $path.IO.extension;</lang>
Contrary to this task's specification, it
* doesn't include the dot in the output
* doesn't include the dot in the output
* doesn't restrict extensions to letters and numbers.
* doesn't restrict the extension to letters and numbers.


Here's a custom implementation which does satisfy those requirements:


Here's a custom implementation which does satisfy the task requirements:
<lang perl6>sub extension (Str $filename --> Str) {

$filename.match(/:i ['.' <+alpha-[_]>+]? $ /).Str
<lang perl6>sub extension (Str $path --> Str) {
$path.match(/:i ['.' <+alpha-[_]>+]? $ /).Str
}</lang>
}</lang>


Testing:
Testing:


<lang perl6>say "$_ -> {(extension $_).perl}" for
<lang perl6>printf "%-35s %-11s %-12s\n", $_, extension($_).perl, $_.IO.extension.perl
for <
'mywebsite.com/picture/image.png',
'http://mywebsite.com/picture/image.png',
http://example.com/download.tar.gz
CharacterModel.3DS
'myuniquefile.longextension',
.desktop
'IAmAFileWithoutExtension',
document
'/path/to.my/file',
'file.odd_one',
document.txt_backup
/etc/pam.d/login
;</lang>
>;</lang>


{{out}}
{{out}}
<pre>
<pre>
http://example.com/download.tar.gz ".gz" "gz"
mywebsite.com/picture/image.png -> ".png"
CharacterModel.3DS ".3DS" "3DS"
http://mywebsite.com/picture/image.png -> ".png"
.desktop ".desktop" "desktop"
myuniquefile.longextension -> ".longextension"
document "" ""
IAmAFileWithoutExtension -> ""
document.txt_backup "" "txt_backup"
/path/to.my/file -> ""
/etc/pam.d/login "" ""
file.odd_one -> ""
</pre>
</pre>