RosettaCodeData/Task/File-extension-is-in-extensions-list/00-TASK.txt
2023-07-01 13:44:08 -04:00

66 lines
2.2 KiB
Text

[[wp:Filename extension|Filename extensions]] are a rudimentary but commonly used way of identifying files types.
;Task:
Given an arbitrary filename and a list of extensions, tell whether the filename has one of those extensions.
Notes:
* The check should be case insensitive.
* The extension must occur at the very end of the filename, and be immediately preceded by a dot (<code>.</code>).
* You may assume that none of the given extensions are the empty string, and none of them contain a dot. Other than that they may be arbitrary strings.
;''Extra credit:''
: Allow extensions to contain dots. This way, users of your function/program have full control over what they consider as the extension in cases like:
<big>archive.tar.gz</big>
: Please state clearly whether or not your solution does this.
{{task heading|Test cases}}
The following test cases all assume this list of extensions: &nbsp; <code>zip</code>, <code>rar</code>, <code>7z</code>, <code>gz</code>, <code>archive</code>, <code>A##</code>
:::::::: {| class="wikitable"
|-
! Filename
! Result
|-
| <code>MyData.a##</code> || true
|-
| <code>MyData.tar.Gz</code> || true
|-
| <code>MyData.gzip</code> || false
|-
| <code>MyData.7z.backup</code> || false
|-
| <code>MyData...</code> || false
|-
| <code>MyData</code> || false
|}
If your solution does the extra credit requirement, add <code>tar.bz2</code> to the list of extensions, and check the following additional test cases:
:::::::: {| class="wikitable"
|-
! Filename
! Result
|-
| <code>MyData_v1.0.tar.bz2</code> || true
|-
| <code>MyData_v1.0.bz2</code> || false
|}
{{task heading|Motivation}}
Checking if a file is in a certain category of file formats with known extensions (e.g. archive files, or image files) is a common problem in practice, and may be approached differently from extracting and outputting an arbitrary extension ''(see e.g. <code>FileNameExtensionFilter</code> in Java)''.
It also requires less assumptions about the format of an extension, because the calling code can decide what extensions are valid.
For these reasons, this task exists in addition to the [[Extract file extension]] task.
;Related tasks:
* [[Extract file extension]]
* [[String matching]]
<br><br>