See Original text in context
multi method extension(IO::Path: --> Str)multi method extension(IO::Path: Int : --> Str)multi method extension(IO::Path: Range : --> Str)multi method extension(IO::Path: Str , Int :, Str : --> IO::Path)multi method extension(IO::Path: Str , Range :, Str : --> IO::Path)
Returns the extension consisting of $parts
parts (defaults to 1
), where a "part" is defined as a dot followed by possibly-empty string up to the end of the string, or previous part. That is "foo.tar.gz"
has an extension of two parts: first part is "gz"
and second part is "tar"
and calling "foo.tar.gz".IO.extension: :2parts
gives "tar.gz"
. If an extension with the specified number of $parts
is not found, returns an empty string.
$parts
can be a Range
, specifying the minimum number of parts and maximum number of parts the extension should have. The routine will attempt to much the most parts it can. If $parts
range's endpoints that are smaller than 0
they'll be treated as 0
; implementations may treat endpoints larger than 2⁶³-1
as 2⁶³-1
. Ranges with NaN
or Str
endpoints will cause an exception to be thrown.
If $subst
is provided, the extension will be instead replaced with $subst
and a new IO::Path
object will be returned. It will be joined to the file's name with $joiner
, which defaults to an empty string when $subst
is an empty string and to "."
when $subst
is not empty. Note: if as the result of replacement the basename
of the path ends up being empty, it will be assumed to be .
(a single dot).
# Getting an extension:say "foo.tar.gz".IO.extension; # OUTPUT: «gz»say "foo.tar.gz".IO.extension: :2parts; # OUTPUT: «tar.gz»say "foo.tar.gz".IO.extension: :parts(^5); # OUTPUT: «tar.gz»say "foo.tar.gz".IO.extension: :parts(0..1); # OUTPUT: «gz»# Replacing an extensionsay "foo.tar.gz".IO.extension: ''; # OUTPUT: «"foo.tar".IO»say "foo.tar.gz".IO.extension: 'ZIP'; # OUTPUT: «"foo.tar.ZIP".IO»say "foo.tar.gz".IO.extension: 'ZIP', :0parts; # OUTPUT: «"foo.tar.gz.ZIP".IO»say "foo.tar.gz".IO.extension: 'ZIP', :2parts; # OUTPUT: «"foo.ZIP".IO»say "foo.tar.gz".IO.extension: 'ZIP', :parts(^5); # OUTPUT: «"foo.ZIP".IO»# Replacing an extension using non-standard joiner:say "foo.tar.gz".IO.extension: '', :joiner<_>; # OUTPUT: «"foo.tar_".IO»say "foo.tar.gz".IO.extension: 'ZIP', :joiner<_>; # OUTPUT: «"foo.tar_ZIP".IO»say "foo.tar.gz".IO.extension: 'ZIP', :joiner<_>,:2parts; # OUTPUT: «"foo_ZIP".IO»say "foo.tar.gz".IO.extension: 'ZIP', :joiner<_>,:parts(^5); # OUTPUT: «"foo_ZIP".IO»# EDGE CASES:# There is no 5-part extension, so returned value is an empty stringsay "foo.tar.gz".IO.extension: :5parts; # OUTPUT: «»# There is no 5-part extension, so we replaced nothing:say "foo.tar.gz".IO.extension: 'ZIP', :5parts; # OUTPUT: «"foo.tar.gz".IO»# Replacing a 0-part extension is just appending:say "foo.tar.gz".IO.extension: 'ZIP', :0parts; # OUTPUT: «"foo.tar.gz.ZIP".IO»# Replace 1-part of the extension, using '.' joinersay "...".IO.extension: 'tar'; # OUTPUT: «"...tar".IO»# Replace 1-part of the extension, using empty string joinersay "...".IO.extension: 'tar', :joiner(''); # OUTPUT: «"..tar".IO»# Remove 1-part extension; results in empty basename, so result is ".".IOsay ".".IO.extension: ''; # OUTPUT: «".".IO»
See Original text in context
NOTE: Most users would want to use the higher-level routine IO::Path.extension
instead of this lower-level version.
method extension(Str --> Str)
Takes a string representing a base name and returns the characters after the last dot ("."
), or empty string if no dots are present. The routine makes no attempt to detect path separators and will return everything after the last dot.
.extension('foo.' ).raku.say; # OUTPUT: «""».extension('foo.txt' ).raku.say; # OUTPUT: «"txt"».extension('foo.tar.gz').raku.say; # OUTPUT: «"gz"».extension('foo' ).raku.say; # OUTPUT: «""».extension('bar.foo/foo').raku.say; # OUTPUT: «"foo/foo"»