Extract file extension: Difference between revisions
Content deleted Content added
→{{header|Go}}: Removed {{Update}} marker |
|||
Line 932: | Line 932: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
⚫ | |||
{{update|Racket|The format of a suffix has been clarified, and the test-cases have been replaced with new ones.}} |
|||
#lang racket |
|||
⚫ | |||
;; Note that for a real implementation, Racket has a |
;; Note that for a real implementation, Racket has a |
||
Line 941: | Line 940: | ||
;; and handles path values so might run into problems with unicode |
;; and handles path values so might run into problems with unicode |
||
;; string inputs. |
;; string inputs. |
||
(define (string-extension x) |
(define (string-extension x) |
||
(cadr (regexp-match #px"(\\.[[:alnum:]]+|)$" x))) |
(cadr (regexp-match #px"(\\.[[:alnum:]]+|)$" x))) |
||
(define examples '("http://example.com/download.tar.gz" |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
(define (string-extension/unicode x) |
|||
(cadr (regexp-match #px"(\\.(?:\\p{L}|\\p{N}|\\p{M})+|)$" x))) |
|||
(define examples '("picture.jpg" |
|||
"http://mywebsite.com/picture/image.png" |
|||
"myuniquefile.longextension" |
|||
"IAmAFileWithoutExtension" |
|||
⚫ | |||
⚫ | |||
⚫ | |||
;; Extra, with unicode |
|||
⚫ | |||
⚫ | |||
;; with unicode combining characters |
|||
⚫ | |||
"file.resume\u0301")) |
|||
(printf "Official task:\n") |
|||
(for ([x (in-list examples)]) |
(for ([x (in-list examples)]) |
||
(printf "~ |
(printf "~a | ~a\n" (~a x #:width 34) (~a (string-extension x) #:width 8))) |
||
⚫ | |||
(printf "\nWith unicode support:\n") |
|||
(for ([x (in-list examples)]) |
|||
(printf "~s ==> ~s\n" x (string-extension/unicode x))) |
|||
⚫ | |||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
http://example.com/download.tar.gz | .gz |
|||
"picture.jpg" ==> ".jpg" |
|||
CharacterModel.3DS | .3DS |
|||
"http://mywebsite.com/picture/image.png" ==> ".png" |
|||
.desktop | .desktop |
|||
"myuniquefile.longextension" ==> ".longextension" |
|||
⚫ | |||
"IAmAFileWithoutExtension" ==> "" |
|||
document.txt_backup | |
|||
"/path/to.my/file" ==> "" |
|||
/etc/pam.d/login | |
|||
"file.odd_one" ==> "" |
|||
"" ==> "" |
|||
"café.png" ==> ".png" |
|||
"file.resumé" ==> "" |
|||
"café.png" ==> ".png" |
|||
"file.resumé" ==> "" |
|||
With unicode support: |
|||
"picture.jpg" ==> ".jpg" |
|||
"http://mywebsite.com/picture/image.png" ==> ".png" |
|||
"myuniquefile.longextension" ==> ".longextension" |
|||
"IAmAFileWithoutExtension" ==> "" |
|||
"/path/to.my/file" ==> "" |
|||
"file.odd_one" ==> "" |
|||
"" ==> "" |
|||
"café.png" ==> ".png" |
|||
"file.resumé" ==> ".resumé" |
|||
"café.png" ==> ".png" |
|||
"file.resumé" ==> ".resumé" |
|||
</pre> |
</pre> |
||