File extension is in extensions list: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Perl 6}}: disallow slash too)
(→‎{{header|REXX}}: added support for blanks in the filename and/or file extension.)
Line 112: Line 112:
:::* handle cases of the filename ending in a period
:::* handle cases of the filename ending in a period
:::* handle cases of the filename ending in multiple periods
:::* handle cases of the filename ending in multiple periods
:::* handle cases of blanks in the filename and/or extension
<lang rexx>/*REXX program displays if a filename has a known extension (per a list)*/
<lang rexx>/*REXX program displays if a filename has a known extension (per a list)*/
/*╔════════════════════════════════════════════════════════════════════╗
extensions='.bat .cmd .com .dat .dll .exe .ini .jpg .jpeg .log .sys .txt'
║ The list of extenions below have blanks encoded as 'ff'x. ║
║ The extension pointed to has a 'ff'x after the period──────────┐ ║
║ (which is used to indicate a true blank, due to │ ║
║ REXX's use of blanks in lists as delimitors). │ ║
╚══════════════════════════════════════════════════════════════════↓═╝*/
extensions='.bat .cmd .com .dat .dll .exe .ini .jpg .jpeg .log .txt . ys'
/* [↑] above would be some EXTs.*/
/* [↑] above would be some EXTs.*/
parse arg fn /*get the filename from the C.L. */
parse arg fn /*get the filename from the C.L. */
fn=strip(fn) /*remove any superfluous blanks. */
if fn=='' then exit /*No fn specified? Then exit.*/
if fn=='' then exit /*No fn specified? Then exit.*/
afn=translate(fn,'/',"\") /*handle both versions of pathSep*/
afn=translate( fn, '/', "\") /*handle both versions of pathSep*/
afn=substr(fn,lastpos('/',afn)+1) /*pick off the filename from path*/
afn=translate(afn, 'ff'x, " ") /*··· and also handle true blanks*/
afn=substr(afn,lastpos('/',afn)+1) /*pick off the filename from path*/
p=lastpos('.',afn) /*find the last position of a dot*/
p=lastpos('.',afn) /*find the last position of a dot*/
if p==0 | p==length(afn) then do /*no dot or dot is at end of name*/
if p==0 | p==length(afn) then do /*no dot or dot is at end of name*/
Line 126: Line 133:
end
end
ft=substr(afn,p) /*pickoff the fileType (fileExt).*/
ft=substr(afn,p) /*pickoff the fileType (fileExt).*/
say 'ft=' ft
upper ft extensions /*uppercase a couple of REXX vars*/
upper ft extensions /*uppercase a couple of REXX vars*/
if wordpos(ft,extensions)==0 then _='an unknown'
if wordpos(ft,extensions)==0 then _='an unknown'

Revision as of 20:47, 12 August 2014

File extension is in extensions list is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Given a file name and a list of extensions (including the dot), tell whether the file's extension is in the extensions list. The check should be case insensitive. The file might not have an extension.

Haskell

<lang Haskell> import Data.List import qualified Data.Char as Ch

toLower :: String -> String toLower = map Ch.toLower

isExt :: String -> [String] -> Bool isExt filename extensions = any (`elem` (tails . toLower $ filename)) $ map toLower extensions </lang>

The code defining isExt could be done point free: <lang Haskell> isExt filename = any (`elem` (tails . toLower $ filename)) . map toLower </lang>

Overcoming the one-liner urge on behalf of being more comprehensible would give: <lang Haskell> isExt filename extensions = any (`elem` allTails) lowerExtensions

                           where allTails = tails . toLower $ filename

lowerExtensions = map toLower extensions </lang>


J

Solution: <lang j>getExt=: }.~ i:&'.' isExt=: e.~&:(tolower&.>) getExt&.>@boxopen</lang> Usage: <lang j> Exts=: <;._1 ' .o .ijs .ij .p'

  TestFiles=: <;._1 ' foo.o blab.iJ dram. lat.P win.dll foo'
  Exts isExt TestFiles

1 1 0 1 0 0</lang>

Java

Works with: Java version 1.5+

<lang java5>import java.util.Arrays; import java.util.Comparator;

public class FileExt{ public static void main(String[] args){ String[] tests = {"text.txt", "text.TXT", "test.tar.gz", "test/test2.exe", "test\\test2.exe", "test", "a/b/c\\d/foo"}; String[] exts = {".txt",".gz","",".bat"};

System.out.println("Extensions: " + Arrays.toString(exts) + "\n");

for(String test:tests){ System.out.println(test +": " + extIsIn(test, exts)); } }

public static boolean extIsIn(String test, String... exts){ int lastSlash = Math.max(test.lastIndexOf('/'), test.lastIndexOf('\\')); //whichever one they decide to use today String filename = test.substring(lastSlash + 1);//+1 to get rid of the slash or move to index 0 if there's no slash

//end of the name if no dot, last dot index otherwise int lastDot = filename.lastIndexOf('.') == -1 ? filename.length() : filename.lastIndexOf('.'); String ext = filename.substring(lastDot);//everything at the last dot and after is the extension

Arrays.sort(exts);//sort for the binary search

return Arrays.binarySearch(exts, ext, new Comparator<String>() { //just use the built-in binary search method @Override //it will let us specify a Comparator and it's fast enough public int compare(String o1, String o2) { return o1.compareToIgnoreCase(o2); } }) >= 0;//binarySearch returns negative numbers when it's not found } } </lang>

Output:
Extensions: [.txt, .gz, , .bat]

text.txt: true
text.TXT: true
test.tar.gz: true
test/test2.exe: false
test\test2.exe: false
test: true
a/b/c\d/foo: true

Perl 6

<lang perl6>my @ext = < .c .o >;

for < foo.C foo.zkl foo foo. > {

   when / :i @ext $ /      { say "$_\t$/ is in the list" }
   when / '.' <-[/.]>* $ / { say "$_\t$/ is not in the list" }
   default                 { say "$_\t (has no extension)" }

}</lang>

Output:
foo.C	.C is in the list
foo.zkl	.zkl is not in the list
foo	 (has no extension)
foo.	. is not in the list

Python

<lang Python> import os

def isExt(filename, extensions):

   return os.path.splitext(filename.lower())[-1] in [e.lower() for e in extensions]

</lang>

REXX

Programming note:   extra code was added to:

  • display some error/warning messages
  • handle the case of the filename having a path
  • handle the case of different types of path separators
  • handle the case of the filename having no file extension
  • handle cases of the filename ending in a period
  • handle cases of the filename ending in multiple periods
  • handle cases of blanks in the filename and/or extension

<lang rexx>/*REXX program displays if a filename has a known extension (per a list)*/ /*╔════════════════════════════════════════════════════════════════════╗

 ║ The list of extenions below have blanks encoded as  'ff'x.         ║
 ║ The extension pointed to has a  'ff'x  after the period──────────┐ ║
 ║ (which is used to indicate a true blank,  due to                 │ ║
 ║ REXX's use of blanks in lists as delimitors).                    │ ║
 ╚══════════════════════════════════════════════════════════════════↓═╝*/

extensions='.bat .cmd .com .dat .dll .exe .ini .jpg .jpeg .log .txt . ys'

                                      /* [↑]  above would be some EXTs.*/

parse arg fn /*get the filename from the C.L. */ if fn== then exit /*No fn specified? Then exit.*/ afn=translate( fn, '/', "\") /*handle both versions of pathSep*/ afn=translate(afn, 'ff'x, " ") /*··· and also handle true blanks*/ afn=substr(afn,lastpos('/',afn)+1) /*pick off the filename from path*/ p=lastpos('.',afn) /*find the last position of a dot*/ if p==0 | p==length(afn) then do /*no dot or dot is at end of name*/

                              say 'Filename ' fn " has no extension."
                              exit
                              end

ft=substr(afn,p) /*pickoff the fileType (fileExt).*/ say 'ft=' ft upper ft extensions /*uppercase a couple of REXX vars*/ if wordpos(ft,extensions)==0 then _='an unknown'

                             else _= 'a known'

say 'Filename ' fn "has" _ 'extension.'</lang>

Ruby

<lang ruby>extensions = [".c",".o",""] ["foo.C","foo.zkl","foo","foo."].each{|f| p extensions.include?( File.extname(f).downcase )} </lang>

Output:
true
false
true
true

Scala

<lang Scala> def isExt(fileName: String, extensions: List[String]): Boolean = {

   extensions.map { _.toLowerCase }.exists { fileName.toLowerCase endsWith _ }

} </lang>

zkl

<lang zkl>var exts=T(".c",".o",""); fcn hasExtension(fname){ exts.holds(File.splitFileName(fname)[3].toLower()) } T("foo.C","foo.zkl","foo","foo.").apply(hasExtension).println();</lang>

Output:
L(True,False,True,True)