longextname

function longextname( path ) --> ext

Description

Returns the longest file name extension used by the path. The extension is the suffix beginning at the first dot in in the final slash-separated element of path.

Parameters

path

Path with file name.

Return Values

ext

Longest file name extension.

Code

--ZFUNC-longextname-v1
local function longextname( path ) --> ext
   local i = #path
   local doti = nil
   local c = string.sub( path, i, i )
   while i > 0 and c ~= "/" do
      i = i - 1
      c = string.sub( path, i, i )
      if c == "." then doti = i end
   end

   if doti then return string.sub( path, doti ) end

   return ""
end

return longextname

Examples

local t = require( "taptest" )
local longextname = require( "longextname" )

t( longextname( "index.html" ), ".html" )
t( longextname( "index.coffee.md" ), ".coffee.md" )

t( longextname( "index.." ), ".." )
t( longextname( "index" ), "" )

t( longextname( ".index" ), ".index" )
t( longextname( "/.index" ), ".index" )

t( longextname( "abc.def/index" ), "" )

t()