splitpath

function splitpath( path ) --> tab

Description

Splits a path into its single elements.

Parameters

path

String path that should be split.

Return Values

tab

String path elements.

Code

--ZFUNC-splitpath-v1
local function splitpath( path ) --> tab
   local tab = {}
   for token in string.gmatch( path, "[^/]+" ) do
      if #token > 0 then
         table.insert( tab, token )
      end
   end
   return tab
end

return splitpath

Examples

local t = require( "taptest" )
local same = require( "same" )
local splitpath = require( "splitpath" )

t( same( splitpath( "a/b/c" ), { "a", "b", "c" } ), true )
t( same( splitpath( "a/b/c/" ), { "a", "b", "c" } ), true )
t( same( splitpath( "/a/b/c" ), { "a", "b", "c" } ), true )
t( same( splitpath( "c:/a/b/c" ), { "c:", "a", "b", "c" } ), true )

t()

See also