File extension is in extensions list: Difference between revisions

Added Easylang
No edit summary
(Added Easylang)
 
(10 intermediate revisions by 10 users not shown)
Line 72:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F is_ext(file_name, extensions)
R any(extensions.map(e -> @file_name.lowercase().ends_with(‘.’e.lowercase())))
 
Line 80:
 
test([‘MyData.a##’, ‘MyData.tar.Gz’, ‘MyData.gzip’, ‘MyData.7z.backup’, ‘MyData...’, ‘MyData’], [‘zip’, ‘rar’, ‘7z’, ‘gz’, ‘archive’, ‘A##’, ‘tar.bz2’])
test([‘MyData_v1.0.tar.bz2’, ‘MyData_v1.0.bz2’], [‘tar.bz2’])</langsyntaxhighlight>
 
{{out}}
Line 95:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">DEFINE PTR="CARD"
 
CHAR FUNC ToLower(CHAR c)
Line 164:
FI
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/File_extension_is_in_extensions_list.png Screenshot from Atari 8-bit computer]
Line 179:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Fixed.Equal_Case_Insensitive; use Ada.Strings.Fixed;
with Ada.Strings.Bounded;
Line 220:
Put_Line (": " & Valid_Extension'Image);
end loop;
end Main;</langsyntaxhighlight>
{{out}}
<pre>
Line 234:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<langsyntaxhighlight lang="algol68"># returns the length of str #
OP LENGTH = ( STRING str )INT: ( UPB str - LWB str ) + 1;
# returns TRUE if str ends with ending FALSE otherwise #
Line 311:
# test the file names in the extra credit #
test has extension in list( "MyData_v1.0.tar.bz2", ec extensions, TRUE );
test has extension in list( "MyData_v1.0.bz2", ec extensions, FALSE )</langsyntaxhighlight>
{{out}}
<pre>
Line 322:
MyData_v1.0.tar.bz2 has extension "tar.bz2"
MyData_v1.0.bz2 does not have an extension in the list</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">ext←{
ex←⎕C'.',¨⍺
ex∨.≡(-≢¨ex)↑¨⊂⎕C⍵
}
 
test←{
e←'zip' 'rar' '7z' 'gz' 'archive' 'A##' 'tar.bz2'
f←'MyData.a##' 'MyData.tar.Gz' 'MyData.gzip' 'MyData.7z.backup'
f,←'MyData...' 'MyData' 'MyData_v1.0.tar.bz2' 'MyData_V1.0.bz2'
f,[1.5]e∘ext¨f
}</syntaxhighlight>
{{out}}
<pre> MyData.a## 1
MyData.tar.Gz 1
MyData.gzip 0
MyData.7z.backup 0
MyData... 0
MyData 0
MyData_v1.0.tar.bz2 1
MyData_V1.0.bz2 0</pre>
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">fileExtensions: map ["zip" "rar" "7z" "gz" "archive" "A##"] => ["." ++ lower &]
hasExtension?: function [file][
Line 334 ⟶ 357:
 
loop files 'file ->
print [file "=> hasExtension?:" hasExtension? file]</langsyntaxhighlight>
{{out}}
Line 344 ⟶ 367:
MyData... => hasExtension?: false
MyData => hasExtension?: false</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">
fileList := "MyData.a##,MyData.tar.Gz,MyData.gzip,MyData.7z.backup,MyData...,MyData,MyData_v1.0.tar.bz2,MyData_v1.0.bz2"
extList := "zip,rar,7z,gz,archive,A##,tar.bz2"
textOut := "File extension is in list (" extList ") ?`n"
loop,parse,fileList,CSV
{
textOut .= A_LoopField " ---> "
lastDotPos := InStr(A_LoopField,".",0,0)
extloop := SubStr(A_LoopField,lastDotPos+1)
if (extloop = "bz2")
{
lastDotPos := InStr(A_LoopField,".",0,0,2)
extloop := SubStr(A_LoopField,lastDotPos+1)
}
if !lastDotPos or !extloop
textOut .= "NO`n"
else if extloop in %extList%
textOut .= "YES`n"
else
textOut .= "NO`n"
}
MsgBox % textOut
ExitApp
</syntaxhighlight>
{{Out}}
<pre>File extension is in list (zip,rar,7z,gz,archive,A##,tar.bz2) ?
MyData.a## ---> YES
MyData.tar.Gz ---> YES
MyData.gzip ---> NO
MyData.7z.backup ---> NO
MyData... ---> NO
MyData ---> NO
MyData_v1.0.tar.bz2 ---> YES
MyData_v1.0.bz2 ---> NO</pre>
 
=={{header|AWK}}==
Line 349 ⟶ 408:
This solution meets the extended criteria stated in the problem.
 
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FILE_EXTENSION_IS_IN_EXTENSIONS_LIST.AWK
BEGIN {
Line 371 ⟶ 430:
}
exit(0)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 388 ⟶ 447:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight BASIC256lang="basic256">arraybase 1
dim extensions$ = {".zip", ".rar", ".7z", ".gz", ".archive", ".a##", ".tar.bz2"}
 
Line 407 ⟶ 466:
if flag = False then print filenames$[n]; " -> "; "false"
next n
end</langsyntaxhighlight>
{{out}}
<pre>
Line 414 ⟶ 473:
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">Dim As String extensions(1 To 7) => {".zip", ".rar", ".7z", ".gz", ".archive", ".a##", ".tar.bz2"}
 
Dim As String filenames(1 To 8) => {"MyData.a##", "MyData.tar.gz", _
Line 434 ⟶ 493:
If flag = False Then Print filenames(n); " -> "; "false"
Next n
Sleep</langsyntaxhighlight>
{{out}}
<pre>MyData.a## -> .a## -> true
Line 449 ⟶ 508:
{{works with|QuickBasic}}
{{trans|FreeBASIC}}
<langsyntaxhighlight QBasiclang="qbasic">DIM extensions$(7)
FOR i = 1 TO 7
READ d$: extensions$(i) = d$
Line 474 ⟶ 533:
DATA ".zip", ".rar", ".7z", ".gz", ".archive", ".a##", ".tar.bz2"
DATA "MyData.a##", "MyData.tar.gz", "MyData.gzip", "MyData.7z.backup"
DATA "MyData...", "MyData", "MyData_v1.0.tar.bz2", "MyData_v1.0.bz2"</langsyntaxhighlight>
<pre>
Igual que la entrada de FreeBASIC.
Line 481 ⟶ 540:
==={{header|True BASIC}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="qbasic">DIM extensions$(7)
FOR i = 1 TO 7
READ d$
Line 506 ⟶ 565:
DATA "MyData.a##", "MyData.tar.gz", "MyData.gzip", "MyData.7z.backup"
DATA "MyData...", "MyData", "MyData_v1.0.tar.bz2", "MyData_v1.0.bz2"
END</langsyntaxhighlight>
{{out}}
<pre>
Line 514 ⟶ 573:
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">dim extensions$(7)
for i = 1 to 7
read d$: extensions$(i) = d$
Line 539 ⟶ 598:
data ".zip", ".rar", ".7z", ".gz", ".archive", ".a##", ".tar.bz2"
data "MyData.a##", "MyData.tar.gz", "MyData.gzip", "MyData.7z.backup"
data "MyData...", "MyData", "MyData_v1.0.tar.bz2", "MyData_v1.0.bz2"</langsyntaxhighlight>
{{out}}
<pre>
Line 548 ⟶ 607:
=={{header|Batch File}}==
This solution does '''not''' contain support for dots in extension names.
<langsyntaxhighlight lang="dos">
@echo off
setlocal enabledelayedexpansion
Line 568 ⟶ 627:
shift
goto loop
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 583 ⟶ 642:
===C: dots allowed ===
There is no magic in extensions: they are just trailing characters in names.
<langsyntaxhighlight lang="c">/*
* File extension is in extensions list (dots allowed).
*
Line 682 ⟶ 741:
getchar();
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 712 ⟶ 771:
According to this, an extension is whatever comes after the '''last''' dot. Dotless filename won't match anything.
 
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <stdbool.h>
Line 781 ⟶ 840:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
This solution allows extensions to contain dots.
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <cctype>
#include <iomanip>
Line 831 ⟶ 890:
test("MyData_v1.0.bz2", extensions);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 846 ⟶ 905:
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(defn matches-extension [ext s]
(re-find (re-pattern (str "\\." ext "$"))
(clojure.string/lower-case s)))
 
(defn matches-extension-list [ext-list s]
(some #(matches-extension % s) ext-list))</langsyntaxhighlight>
 
It is assumed that when extensions are "added to the system", they will be "normalized" to the expected (lower) case.
 
<langsyntaxhighlight Clojurelang="clojure">(defn normalize-extensions [ext-list]
(map clojure.string/lower-case ext-list))</langsyntaxhighlight>
 
{{out}}
Line 879 ⟶ 938:
=={{header|D}}==
===Variant 1===
<syntaxhighlight lang="d">
<lang d>
import std.stdio;
import std.string;
Line 916 ⟶ 975:
 
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 933 ⟶ 992:
 
===Variant 2===
<syntaxhighlight lang="d">
<lang d>
import std.stdio;
import std.string;
Line 971 ⟶ 1,030:
}
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 990 ⟶ 1,049:
{{libheader| System.SysUtils}}
{{Trans|D}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program File_extension_is_in_extensions_list;
 
Line 1,024 ⟶ 1,083:
 
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>extensions: [zip rar 7z gz archive A## tar.bz2 ]
Line 1,036 ⟶ 1,095:
MyData_v1.0.tar.bz2 : TRUE
MyData_v1.0.bz2 : FALSE</pre>
 
=={{header|EasyLang}}==
{{trans|BASIC256}}
<syntaxhighlight>
exts$[] = [ ".zip" ".rar" ".7z" ".gz" ".archive" ".a##" ".tar.bz2" ]
fnames$[] = [ "MyData.a##" "MyData.tar.gz" "MyData.gzip" "MyData.7z.backup" "MyData..." "MyData" "MyData_v1.0.tar.bz2" "MyData_v1.0.bz2" ]
for fn$ in fnames$[]
ext$ = ""
for ext$ in exts$[]
h = len ext$
if substr fn$ (len fn$ - h + 1) h = ext$
print fn$ & " -> " & ext$ & " -> true"
break 1
.
.
if ext$ = ""
print fn$ & " -> false"
.
.
</syntaxhighlight>
{{out}}
<pre>
MyData.a## -> .a## -> true
MyData.tar.gz -> .gz -> true
MyData.gzip -> false
MyData.7z.backup -> false
MyData... -> false
MyData -> false
MyData_v1.0.tar.bz2 -> .tar.bz2 -> true
MyData_v1.0.bz2 -> false
</pre>
 
=={{header|Factor}}==
This solution allows dots in file extensions.
<langsyntaxhighlight lang="factor">USING: formatting kernel qw sequences splitting unicode ;
IN: rosetta-code.file-extension-list
 
Line 1,056 ⟶ 1,146:
filenames [
dup extensions ext-in-list? "%19s %u\n" printf
] each</langsyntaxhighlight>
{{out}}
<pre>
Line 1,079 ⟶ 1,169:
Petty details include the list employing capitals only, as the match is to not distinguish capital from lower case letters, so in the usual way the candidate extension's text is converted to capitals. The list could be subjected to UPCASE, but it is bad form to damage what might be a constant, and one possibly in read-only storage at that. An internal working copy could be made which would then be fed to UPCASE, except that this would be a waste on every invocation. A further trick involves appending a period to the candidate text so that for example ".JP" becomes ".JP." - otherwise a ".JP" would be found in the sequence ".JPG" which would be wrong, so as a result, the list of texts must have a period appended to its last entry, otherwise it would not be findable. Again, this could be done internally, via <code>INDEX(LIST//".",EXT(1:L)//".")</code> at a run-time cost.
 
Some systems supply an UPCASE (or similar name) to convert text that hopefully would run faster than this example, which relies on searching for the position in a list of letters. A very common alternative is to calculate using character code numerical values, via something like<langsyntaxhighlight Fortranlang="fortran"> IT = ICHAR(TEXT(I:I)) - ICHAR("a") !More symbols precede "a" than "A".
IF (IT.GE.0 .AND. IT.LE.25) TEXT(I:I) = CHAR(IT + ICHAR("A")) !In a-z? Convert!</langsyntaxhighlight> except that this relies on the letters having contiguous character codes, and in EBCDIC they don't - other symbols are mixed in. (Honest!) Faster still would be to use the character code to index an array of 256 pre-computed values.
 
A final annoyance is the presence of trailing spaces because character variables are of fixed size and so must be made "surely long enough" for the longest expectation. This may not cause trouble on output as spaces look just as blank as blank space, but they may well cause trouble in the internal tests. Thus integer function LSTNB reports the last non-blank, and so a test text can be presented with no trailing spaces, as in TEST(I)(1:LSTNB(TEST(I))) or similar. With Fortran 2003, there is a facility for redefining the sizes of character variables on-the-fly so that this problem can be evaded.
 
The MODULE protocol is employed for the convenience of not having to respecify the type of the functions in every calling routine, and also to facilitate the collection of types of characters. Otherwise, prior to F90 there would have to be various COMMON statements, or routines would each simply respecify whatever character sets they needed.<langsyntaxhighlight Fortranlang="fortran"> MODULE TEXTGNASH !Some text inspection.
CHARACTER*10 DIGITS !Integer only.
CHARACTER*11 DDIGITS !With a full stop masquerading as a decimal point.
Line 1,220 ⟶ 1,310:
END DO !On to the next.
END
</syntaxhighlight>
</lang>
The previous results were when only "image" style files were approved. The approval is no longer so restrictive.
<pre>
Line 1,246 ⟶ 1,336:
=={{header|Go}}==
This allows extensions to include a dot and, in the case of success, displays the extension matched (ignoring case) in the list. In the case of failure, the actual (minimum) file extension is displayed.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,288 ⟶ 1,378:
fmt.Printf("%-20s => %-5t (extension = %s)\n", test, ok, ext)
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,304 ⟶ 1,394:
MyData_v1.0.bz2 => false (extension = bz2)
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
void local fn DoIt
CFArrayRef extensions = @[@"zip",@"rar",@"7z",@"gz",@"archive",@"A##"]
CFArrayRef filenames = @[@"MyData.a##",@"MyData.tar.Gz",@"MyData.gzip",@"MyData.7z.backup",@"MyData...",@"MyData"]
CFStringRef name
NSLogSetTabInterval( 130 )
for name in filenames
NSLog(@"%@\t",name)
if ( fn ArrayContainsObject( fn ArrayValueForKey( extensions, @"lowercaseString" ), lcase(fn StringPathExtension(name) ) ) )
NSLog(@"true")
else
NSLog(@"false")
end if
next
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
 
=={{header|Haskell}}==
 
<langsyntaxhighlight Haskelllang="haskell">import Data.List
import qualified Data.Char as Ch
 
Line 1,314 ⟶ 1,429:
 
isExt :: String -> [String] -> Bool
isExt filename extensions = any (`elem` (tails . toLower $ filename)) $ map toLower extensions</langsyntaxhighlight>
 
The code defining isExt could be done point free:
<langsyntaxhighlight Haskelllang="haskell">isExt filename = any (`elem` (tails . toLower $ filename)) . map toLower</langsyntaxhighlight>
 
Overcoming the one-liner urge on behalf of being more comprehensible would give:
<langsyntaxhighlight Haskelllang="haskell">isExt filename extensions = any (`elem` allTails) lowerExtensions
where allTails = tails . toLower $ filename
lowerExtensions = map toLower extensions</langsyntaxhighlight>
 
Given definition handles composite extensions as well.
Line 1,328 ⟶ 1,443:
'''Testing:'''
 
<langsyntaxhighlight Haskelllang="haskell">main = mapM_ (\f -> putStr(f ++ "\t") >> print (f `isExt` extensions)) files
where
files = [ "MyData.a##"
Line 1,338 ⟶ 1,453:
, "MyData_v1.0.tar.bz2"
, "MyData_v1.0.bz2" ]
extensions = ["zip", "rar", "7z", "gz", "archive", "A##", "tar.bz2"]</langsyntaxhighlight>
{{Out}}
<pre>λ> main
Line 1,353 ⟶ 1,468:
We can also express this directly in terms of the '''isSuffix''' function, taking care not to reproduce the small glitch in the draft above, which shows a false positive for '''zip''' in '''.gzip''' (see task description bullet 2).
 
<syntaxhighlight lang ="haskell">import Data.ListChar (find, isSuffixOftoLower)
import Data.List (find, isSuffixOf)
import Data.Maybe (fromMaybe)
import Data.Char (toLower)
 
----------- FILE EXTENSION IS IN EXTENSIONS LIST ---------
 
extensionFound :: [String] -> String -> Maybe String
extensionFound xs fp = find (`isSuffixOf` fp) ($ ('.' :) <$> xs)
 
 
-- TESTS ------------------------- TESTS -------------------------
main :: IO ()
main =
Line 1,371 ⟶ 1,487:
(fromMaybe "n/a")
(extensionFound
(lcasedlowerCased ["zip", "rar", "7z", "gz", "archive", "A##", "tar.bz2"]))
(lcasedlowerCased
[ "MyData.a##"
, "MyData.tar.Gz"
Line 1,383 ⟶ 1,499:
])
 
------------------------- STRINGS ------------------------
 
-- STRINGS ------------------------------------------------
 
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
fTable s xShow fxShow f xs =
unlines $
let w = maximum (length . xShow <$> xs)
s : fmap (((<>) . rjust nw c' = (drop' . lengthxShow) <*> (replicate(" n-> c" ++<>) . fxShow . f)) xs
where
in unlines $
rjust sn :c fmap= (((++)drop . rjust w ' ' . xShow)length <*> (("replicate ->n "c ++<>) . fxShow . f)) xs
w = maximum (length . xShow <$> xs)
 
lcasedlowerCased :: [String] -> [String]
lcasedlowerCased = fmap (fmap toLower)</langsyntaxhighlight>
{{Out}}
<pre>Any matching extensions found:
Line 1,410 ⟶ 1,525:
 
'''Solution:'''
<langsyntaxhighlight lang="j">isSuffix=: -~&# = {:@I.@E.
isExt=: ('.'&,&.>@[ ([: +./ isSuffix&(tolower@>)/) boxopen@])</langsyntaxhighlight>
 
'''Usage:'''
<langsyntaxhighlight lang="j"> Exts=: <;._1 ' zip rar 7z gz archive A## tar.bz2'
TestFiles=: <;._1 ' MyData.a## MyData.tar.Gz MyData.gzip MyData.7z.backup MyData... MyData MyData_v1.0.tar.bz2 MyData_v1.0.bz2'
Exts isExt TestFiles
1 1 0 0 0 0 1 0</langsyntaxhighlight>
 
=={{header|Java}}==
Line 1,423 ⟶ 1,538:
 
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">import java.util.Arrays;
import java.util.Comparator;
 
Line 1,456 ⟶ 1,571:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>Extensions: [.txt, .gz, , .bat]
Line 1,470 ⟶ 1,585:
{{works with|Java|6+}}
This version is the same as the main version only replace the definition for <code>extIsIn</code> with:
<langsyntaxhighlight lang="java5">public static boolean extIsIn(String test, String... exts){
for(int i = 0; i < exts.length; i++){
exts[i] = exts[i].replaceAll("\\.", "");
}
return (new FileNameExtensionFilter("extension test", exts)).accept(new File(test));
}</langsyntaxhighlight>
It would be one line if not for the dot requirement. <code>FileNameExtensionFilter</code> requires that the extensions have no dots. It also requires that the extensions contain characters (i.e. not the empty string) so that test would need to be removed.
 
=={{header|JavaScript}}==
 
This solution may not be the most performant but works for most implementations. Allows for dotted extensions, as well.
 
'''NOTE''' String.endsWith() was introduced with ECMAScript 6 (ES6), so that is a minimum requirement of your JS processor
 
<syntaxhighlight lang="javascript">
function fileExtensionInExtensionsList(filename) {
let foundIt = false;
let filenameLC = filename.toLowerCase();
let extensions = ["zip", "rar", "7z", "gz", "archive", "A##" ,"tar.bz2"];
extensions.forEach(extension => {
if (filenameLC.endsWith("." + extension.toLowerCase())) { foundIt = true; }
} );
return foundIt;
}
 
// perform tests below
 
var fileNamesToTest = [
"MyData.a##"
,"MyData.tar.Gz"
,"MyData.gzip"
,"MyData.7z.backup"
,"MyData..."
,"MyData"
,"MyData_v1.0.tar.bz2"
,"MyData_v1.0.bz2"
];
 
fileNamesToTest.forEach(filename => {
console.log(filename + " -> " + fileExtensionInExtensionsList(filename));
});
</syntaxhighlight>
 
{{out}}
(from tests - function returns true or false)
<pre>
MyData.a## -> true
MyData.tar.Gz -> true
MyData.gzip -> false
MyData.7z.backup -> false
MyData... -> false
MyData -> false
MyData_v1.0.tar.bz2 -> true
MyData_v1.0.bz2 -> false
</pre>
 
=={{header|jq}}==
Line 1,483 ⟶ 1,646:
 
{{works with|jq|1.4}}
<langsyntaxhighlight lang="jq"># Input: filename
# Output: if the filename ends with one of the extensions (ignoring case), output that extension; else output null.
# Assume that the list of file extensions consists of lower-case strings, including a leading period.
Line 1,492 ⟶ 1,655:
| if list | index($ext) then $ext else null end
else null
end;</langsyntaxhighlight>
'''Examples:'''
<langsyntaxhighlight lang="jq">("c:", "txt", "text.txt", "text.TXT", "foo.c", "foo.C++", "document.pdf")
| has_extension([".txt", ".c"]) as $ext
| if $ext then "\(.) has extension \($ext)"
else "\"\(.)\" does not have an admissible file extension"
end</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang="jq">$ jq -r -n -f File_extension_is_in_extensions_list.jq
"c:" does not have an admissible file extension
"txt" does not have an admissible file extension
Line 1,508 ⟶ 1,671:
foo.c has extension .c
"foo.C++" does not have an admissible file extension
"document.pdf" does not have an admissible file extension</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">isext(filename, extensions) = any(x -> endswith(lowercase(filename), lowercase(x)), "." .* extensions)
 
# Test
Line 1,519 ⟶ 1,682:
for f in ["MyData.a##", "MyData.tar.Gz", "MyData.gzip", "MyData.7z.backup", "MyData...", "MyData", "MyData_v1.0.tar.bz2", "MyData_v1.0.bz2"]
@printf("%20s : %5s\n", f, isext(f, extensions))
end</langsyntaxhighlight>
 
{{out}}
Line 1,532 ⟶ 1,695:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1
 
/* implicitly allows for extensions containing dots */
Line 1,555 ⟶ 1,718:
println("${fileName.padEnd(19)} -> ${fileName.isFileExtensionListed(extensions)}")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,570 ⟶ 1,733:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- Data declarations
local extentions = {"zip", "rar", "7z", "gz", "archive", "A##", "tar.bz2"}
local testCases = {
Line 1,596 ⟶ 1,759:
for _, case in pairs(testCases) do
print(case .. ": " .. tostring(extMatch(extentions, case)))
end</langsyntaxhighlight>
{{out}}
<pre>MyData.a##: true
Line 1,608 ⟶ 1,771:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[CheckExtension]
CheckExtension[fn_String, e : {_String ..}] := StringMatchQ[ToLowerCase[FileExtension[fn]], Alternatives @@ ToLowerCase[e]]
exts = {"zip", "rar", "7z", "gz", "archive", "A##"};
Line 1,616 ⟶ 1,779:
CheckExtension["MyData.7z.backup", exts]
CheckExtension["MyData..", exts]
CheckExtension["MyData", exts]</langsyntaxhighlight>
{{out}}
<pre>True
Line 1,630 ⟶ 1,793:
 
To build the extension list, we can use a compile time function:
<langsyntaxhighlight Nimlang="nim">import os, strutils
 
let fileNameList = ["MyData.a##", "MyData.tar.Gz", "MyData.gzip",
Line 1,642 ⟶ 1,805:
 
for fileName in fileNameList:
echo fileName, " → ", fileName.splitFile().ext.toLowerAscii() in ExtList</langsyntaxhighlight>
 
Another way consists to use “mapIt” from standard module “sequtils”:
<langsyntaxhighlight Nimlang="nim">import os, sequtils, strutils
 
let fileNameList = ["MyData.a##", "MyData.tar.Gz", "MyData.gzip",
Line 1,653 ⟶ 1,816:
 
for fileName in fileNameList:
echo fileName, " → ", fileName.splitFile().ext.toLowerAscii() in ExtList</langsyntaxhighlight>
 
{{out}}
Line 1,666 ⟶ 1,829:
The “splitFile” procedure no longer works in this case. We have to use “endsWith” from the “strutils” module, which changes the logic:
 
<langsyntaxhighlight lang="nim">import strutils, sequtils
 
let fileNameList = ["MyData.a##", "MyData.tar.Gz", "MyData.gzip",
Line 1,675 ⟶ 1,838:
 
for fileName in fileNameList:
echo fileName, " → ", ExtList.anyIt(fileName.toLowerAscii().endsWith(it))</langsyntaxhighlight>
 
{{out}}
Line 1,688 ⟶ 1,851:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
class FileExtension {
function : Main(args : String[]) ~ Nil {
Line 1,712 ⟶ 1,875:
IO.Console->Print(file)->PrintLine(" does not have an extension in the list");
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,730 ⟶ 1,893:
The examples were taken from the [[#AWK|AWK]] code.
 
<langsyntaxhighlight lang="parigp">lower(s)=
{
my(v=Vecsmall(s));
Line 1,752 ⟶ 1,915:
ext=["txt","gz","bat","c","c++","exe","pdf"];
filenames=["c:","txt","text.txt","text.TXT","test.tar.gz","test/test2.exe","test","foo.c","foo.C","foo.C++","foo.c#","foo.zkl","document.pdf"];
select(f -> checkExt(ext, f), filenames)</langsyntaxhighlight>
{{out}}
<pre>%1 = ["text.txt", "text.TXT", "test.tar.gz", "test/test2.exe", "foo.c", "foo.C", "foo.C++", "document.pdf"]</pre>
Line 1,762 ⟶ 1,925:
{{trans|Raku}}
 
<langsyntaxhighlight lang="perl">sub check_extension {
my ($filename, @extensions) = @_;
my $extensions = join '|', map quotemeta, @extensions;
scalar $filename =~ / \. (?: $extensions ) $ /xi
}</langsyntaxhighlight>
 
Testing:
<langsyntaxhighlight lang="perl">my @extensions = ('zip', 'rar', '7z', 'gz', 'archive', 'A##', 'tar.bz2');
my @files= (
'MyData.a##', 'MyData.tar.Gz', 'MyData.gzip', 'MyData.7z.backup',
Line 1,776 ⟶ 1,939:
printf "%-19s - %s\n",
$_, check_extension($_, @extensions) ? 'true' : 'false'
for @files;</langsyntaxhighlight>
 
{{out}}
Line 1,793 ⟶ 1,956:
This solution does support the extra credit in the task
 
<langsyntaxhighlight lang="php">
$allowed = ['zip', 'rar', '7z', 'gz', 'archive', 'A##', 'tar.bz2'];
$lc_allowed = array_map('strtolower', $allowed);
Line 1,816 ⟶ 1,979:
printf("%20s : %s \n", $test[0],$result);
}
</syntaxhighlight>
</lang>
{{out}}
MyData.a## : true
Line 1,828 ⟶ 1,991:
=={{header|Phix}}==
Note there is builtin, however get_file_extension("MyData_v1.0.tar.bz2") yields "bz2", since it scans right-to-left for the '.', and also it also yields "so" for "libglfw.so.3.1" (it saves the "1", and the "3", but when they are all-number it carries on scanning left and replaces if it finds another '.'). So to match the task requirements more precisely we shall write a custom version.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">extensions</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">lower</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"zip"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"rar"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"7z"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"gz"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"archive"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"A##"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"tar.bz2"</span><span style="color: #0000FF;">})</span>
Line 1,858 ⟶ 2,021:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%-20s %-10s %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ext</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"true"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"false"</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">-(</span><span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ext</span><span style="color: #0000FF;">,</span><span style="color: #000000;">extensions</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,873 ⟶ 2,036:
=={{header|Python}}==
 
<syntaxhighlight lang="python">
<lang Python>
def isExt(fileName, extensions):
return True in map(fileName.lower().endswith, ("." + e.lower() for e in extensions))
</syntaxhighlight>
</lang>
 
and we could also enrich the return value to a Maybe option type, returning Nothing if no file extension match is found, or Just the particular extension, if a match is found:
 
<langsyntaxhighlight lang="python">'''Check for a specific set of file extensions'''
 
 
Line 1,987 ⟶ 2,150:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Check for a specific set of file extensions:
Line 2,002 ⟶ 2,165:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 2,033 ⟶ 2,196:
(for ((f (in-list filenames)))
(printf "~a ~a~%" (~a #:width 20 f) (file-extension-in-list? f extensions)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,051 ⟶ 2,214:
Does the extra credit requirement.
 
<syntaxhighlight lang="raku" perl6line>sub check-extension ($filename, *@extensions) {
so $filename ~~ /:i '.' @extensions $/
}
Line 2,062 ⟶ 2,225:
MyData_v1.0.tar.bz2 MyData_v1.0.bz2
>;
say "{$_.fmt: '%-19s'} - {check-extension $_, @extensions}" for @files;</langsyntaxhighlight>
 
{{out}}
Line 2,078 ⟶ 2,241:
=={{header|REXX}}==
This REXX version handles the extra credit requirement.
<langsyntaxhighlight lang="rexx">/*REXX pgm displays if a filename has a known extension (as per a list of extensions). */
$= 'zip rar 7z gz archive A## tar.bz2'; upper $ /*a list of "allowable" file extensions*/
parse arg fn /*obtain optional argument from the CL.*/
Line 2,099 ⟶ 2,262:
end /*k*/ /* [↓] display file, and a nay or yea.*/
say right(@.j, 40) ' ' right( word( "false true", 1 + (k<=#) ), 5)
end /*j*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 2,113 ⟶ 2,276:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : File extension is in extensions list
 
Line 2,134 ⟶ 2,297:
ok
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,148 ⟶ 2,311:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def is_ext(filename, extensions)
if filename.respond_to?(:each)
filename.each do |fn|
Line 2,161 ⟶ 2,324:
end
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,174 ⟶ 2,337:
=={{header|Rust}}==
Does extra credit.
<langsyntaxhighlight Rustlang="rust">fn main() {
let exts = ["zip", "rar", "7z", "gz", "archive", "A##", "tar.bz2"];
let filenames = [
Line 2,198 ⟶ 2,361:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,215 ⟶ 2,378:
=={{header|Scala}}==
 
<langsyntaxhighlight Scalalang="scala"> def isExt(fileName: String, extensions: List[String]): Boolean = {
extensions.map { _.toLowerCase }.exists { fileName.toLowerCase endsWith "." + _ }
}
</syntaxhighlight>
</lang>
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func check_extension(filename, extensions) {
filename ~~ Regex('\.(' + extensions.map { .escape }.join('|') + ')\z', :i)
}</langsyntaxhighlight>
 
Testing:
<langsyntaxhighlight lang="ruby">var extensions = ['zip', 'rar', '7z', 'gz', 'archive', 'A##', 'tar.bz2']
 
var files = [
Line 2,236 ⟶ 2,399:
for file in files {
printf("%-19s - %s\n", file, check_extension(file, extensions))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,251 ⟶ 2,414:
=={{header|Tcl}}==
 
<langsyntaxhighlight lang="tcl">
 
# This example includes the extra credit.
Line 2,317 ⟶ 2,480:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,328 ⟶ 2,491:
MyData_v1.0.tar.bz2 true
MyData_v1.0.bz2 false
</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Zig">
const (
extensions = [".zip", ".rar", ".7z", ".gz", ".archive", ".a##", ".tar.bz2"]
filenames = ["MyData.a##", "MyData.tar.gz", "MyData.gzip", "MyData.7z.backup",
"MyData...", "MyData", "MyData_v1.0.tar.bz2", "MyData_v1.0.bz2"]
)
 
fn main() {
outer:
for oidx, file in filenames {
for idx, extension in extensions {
if file.substr_ni(file.len - extension.len, file.len) == extension {
println("${filenames[oidx]} -> ${extensions[idx]} -> true")
continue outer
}
}
println("${filenames[oidx]} -> false")
}
}
</syntaxhighlight>
 
{{out}}
<pre>
MyData.a## -> .a## -> true
MyData.tar.gz -> .gz -> true
MyData.gzip -> false
MyData.7z.backup -> false
MyData... -> false
MyData -> false
MyData_v1.0.tar.bz2 -> .tar.bz2 -> true
MyData_v1.0.bz2 -> false
</pre>
 
Line 2,334 ⟶ 2,530:
{{libheader|Wren-str}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./str" for Str
import "./fmt" for Fmt
 
var exts = ["zip", "rar", "7z", "gz", "archive", "A##", "tar.bz2"]
Line 2,358 ⟶ 2,554:
}
if (!hasExt) Fmt.print("$-20s $-5s", test, hasExt)
}</langsyntaxhighlight>
 
{{out}}
Line 2,370 ⟶ 2,566:
MyData_v1.0.tar.bz2 true (extension: tar.bz2)
MyData_v1.0.bz2 false
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">string 0;
 
func ToUpper(C); \Convert character to upper case
int C;
return if C>=^a & C<=^z then C&$DF else C;
 
func HasExt(File, Ext); \Return 'true' if File has Ext(ension)
char File, Ext;
int I, J;
[I:= 0;
while File(I) # 0 do I:= I+1;
while File(I) # ^. and I > 0 do I:= I-1;
I:= I+1;
J:= 0;
loop [if ToUpper(File(I+J)) # ToUpper(Ext(J)) then return false;
if Ext(J) = 0 then return true;
J:= J+1;
];
];
 
int Exts, Ext, Files, Test;
[Exts:= ["zip", "rar", "7z", "gz", "archive", "A##"];
Files:= ["MyData.a##", "MyData.tar.Gz", "MyData.gzip" , "MyData.7z.backup",
"MyData...", "MyData" ];
for Test:= 0 to 5 do
[for Ext:= 0 to 5 do
[if HasExt(Files(Test), Exts(Ext)) then
[Text(0, " true : ");
Ext:= 100;
];
];
if Ext = 6 then Text(0, "false : ");
Text(0, Files(Test));
CrLf(0);
];
]</syntaxhighlight>
{{out}}
<pre>
true : MyData.a##
true : MyData.tar.Gz
false : MyData.gzip
false : MyData.7z.backup
false : MyData...
false : MyData
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn hasExtension(fnm){
var [const] extensions=T(".zip",".rar",".7z",".gz",".archive",".a##");
nm,ext:=File.splitFileName(fnm)[-2,*].apply("toLower");
Line 2,383 ⟶ 2,626:
"MyData...","MyData",
"MyData_v1.0.tAr.bz2","MyData_v1.0.bz2");
foreach nm in (nms){ println("%20s : %s".fmt(nm,hasExtension(nm))); }</langsyntaxhighlight>
{{out}}
<pre>
1,983

edits