Extract file extension: Difference between revisions

Add CLU
(Added solution for Action!)
(Add CLU)
Line 751:
(".gz" ".3DS" ".desktop" nil nil nil)
</pre>
 
=={{header|CLU}}==
CLU contains a built-in filename parser, which behaves slightly differently than
the task specification. It returns the <em>first</em>, rather than last dotted part,
and also accepts non-alphanumeric characters in the extension. Furthermore, it does
not include the dot itself in its output.
 
<lang clu>% Find the extension of a filename, according to the task specification
extension = proc (s: string) returns (string)
for i: int in int$from_to_by(string$size(s), 1, -1) do
c: char := s[i]
if c>='A' & c<='Z'
| c>='a' & c<='z'
| c>='0' & c<='9' then continue end
if c='.' then return(string$rest(s,i)) end
break
end
return("")
end extension
% For each test case, show both the extension according to the task,
% and the extension that the built-in function returns.
start_up = proc ()
po: stream := stream$primary_output()
tests: sequence[string] := sequence[string]$[
"http://example.com/download.tar.gz",
"CharacterModel.3DS",
".desktop",
"document",
"document.txt_backup",
"/etc/pam.d/login"
]
stream$putleft(po, "Input", 36)
stream$putleft(po, "Output", 10)
stream$putl(po, "Built-in")
stream$putl(po, "---------------------------------------------------------")
for test: string in sequence[string]$elements(tests) do
stream$putleft(po, test, 36)
stream$putleft(po, extension(test), 10)
% Using the built-in filename parser
stream$putl(po, file_name$parse(test).suffix)
except when bad_format:
stream$putl(po, "[bad_format signaled]")
end
end
end start_up</lang>
{{out}}
<pre>Input Output Built-in
---------------------------------------------------------
http://example.com/download.tar.gz .gz tar
CharacterModel.3DS .3DS 3DS
.desktop .desktop desktop
document
document.txt_backup txt_backup
/etc/pam.d/login</pre>
 
=={{header|Common Lisp}}==
2,115

edits