Extract file extension: Difference between revisions

Content deleted Content added
add "needs updating" template to 20 of the language entries
Line 55:
 
=={{header|ALGOL 68}}==
 
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<lang algol68># extracts a file-extension from the end of a pathname. The file extension is #
Line 115 ⟶ 116:
 
=={{header|ALGOL W}}==
 
<lang algolw>begin
% extracts a file-extension from the end of a pathname. %
Line 203 ⟶ 205:
 
=={{header|AWK}}==
 
{{update|AWK|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
<lang AWK>
# syntax: GAWK -f EXTRACT_FILE_EXTENSION.AWK
Line 240 ⟶ 245:
file.odd_one ''
</pre>
 
=={{header|C}}==
 
{{update|C|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
<lang C>
#include <assert.h>
Line 292 ⟶ 301:
 
=={{header|C++}}==
 
{{update|C++|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
<lang cpp>#include <string>
#include <algorithm>
Line 333 ⟶ 345:
 
=={{header|C sharp|C#}}==
 
<lang [[C sharp|C#]]>public static string FindExtension(string filename) {
int indexOfDot = filename.Length;
Line 355 ⟶ 368:
 
=={{header|Emacs Lisp}}==
 
<lang Lisp>(file-name-extension "foo.txt")
=>
Line 370 ⟶ 384:
 
=={{header|Forth}}==
 
{{update|Forth|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
<lang forth>: invalid? ( c -- f )
toupper dup [char] A [char] Z 1+ within
Line 410 ⟶ 427:
 
=={{header|Fortran}}==
 
{{update|Fortran|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
The plan is to scan backwards from the end of the text until a non-extensionish character is encountered. If it is a period, then a valid file extension has been spanned. Otherwise, no extension. Yet again the "no specification" on the possibility of shortcut evaluation of compound logical expressions prevents the structured use of a DO WHILE(L1 > 0 & etc) loop because the possible evaluation of both parts of the expression means that the second part may attempt to access character zero of a text. So, the compound expression has to be broken into two separate parts.
 
Line 493 ⟶ 513:
 
=={{header|Go}}==
 
{{update|Go|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
<lang go>package main
 
Line 500 ⟶ 523:
)
 
// An exact copy of `path.Ext` from Go  1.4.2 for reference:
func Ext(path string) string {
for i := len(path) - 1; i >= 0 && path[i] != '/'; i-- {
Line 573 ⟶ 596:
 
=={{header|Haskell}}==
 
{{update|Haskell|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
<lang Haskell>module FileExtension
where
Line 591 ⟶ 617:
 
=={{header|J}}==
 
{{update|J|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
'''Implementation:'''
Line 620 ⟶ 648:
 
=={{header|Java}}==
 
{{update|Java|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
<lang java>public class Test {
 
Line 649 ⟶ 680:
 
=={{header|jq}}==
 
{{update|jq|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
Pending resolution of the inconsistency in the task description as of this writing, the following
Line 699 ⟶ 732:
 
=={{header|Lua}}==
 
{{update|Lua|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
<lang Lua>-- Lua pattern docs at http://www.lua.org/manual/5.1/manual.html#5.4.1
function fileExt (filename) return filename:match("(%.%w+)$") or "" end
Line 722 ⟶ 758:
 
=={{header|Oforth}}==
 
{{update|Oforth|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
If extension is not valid, returns null, not "".
Easy to change if "" is required.
Line 732 ⟶ 771:
s extract(i, s size)
} </lang>
 
 
 
{{out}}
Line 814 ⟶ 851:
 
=={{header|Phix}}==
 
{{update|Phix|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
<lang Phix>function getExtension(string filename)
for i=length(filename) to 1 by -1 do
Line 843 ⟶ 883:
 
=={{header|PowerShell}}==
 
{{update|PowerShell|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
<lang PowerShell>
function extension($file){
Line 869 ⟶ 912:
 
=={{header|Python}}==
 
{{update|Python|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
Uses [https://docs.python.org/3/library/os.path.html#os.path.splitext os.path.splitext] and the extended tests from the Go example above.
 
Line 903 ⟶ 949:
 
=={{header|Racket}}==
 
{{update|Racket|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
<lang Racket>#lang racket
 
Line 969 ⟶ 1,018:
 
=={{header|REXX}}==
 
Using this paraphrased Rosetta Code task's definition that:
 
Line 1,004 ⟶ 1,054:
 
=={{header|sed}}==
 
{{update|sed|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
<lang sed>s:.*\.:.:
s:\(^[^.]\|.*[/_]\).*::</lang> or <lang bash>sed -re 's:.*\.:.:' -e 's:(^[^.]|.*[/_]).*::'</lang>
Line 1,017 ⟶ 1,070:
 
=={{header|Sidef}}==
 
{{update|Sidef|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
<lang ruby>func extension (filename) {
given(filename.split('.').last) {
Line 1,043 ⟶ 1,099:
 
=={{header|Tcl}}==
 
{{update|Tcl|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
Tcl's built in [http://wiki.tcl.tk/10072 file extension] command already almost knows how to do this, except it accepts any character after the dot. Just for fun, we'll enhance the builtin with a new subcommand with the limitation specified for this problem.
Line 1,078 ⟶ 1,136:
 
=={{header|VBScript}}==
 
{{update|VBScript|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}}
 
<lang vb>
Function GetExtension(s)
Line 1,118 ⟶ 1,179:
 
=={{header|zkl}}==
 
The File object has a method splitFileName that does just that, returning a list of the parts. The method knows about the OS it was compiled on (Unix, Windows).
<lang zkl>valid:=Walker.chain(".",["a".."z"],["A".."Z"],["0".."9")).sink(String).walk();