Compare commits
2 Commits
master
...
rewrite-pa
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
05b1bcb284 | ||
|
|
f461a3d701 |
|
|
@ -1,149 +1,124 @@
|
||||||
local path = {}
|
local PathObj = {}
|
||||||
|
|
||||||
function path.Common_Root( Left, Right, Separator )
|
local posix_separator = '/'
|
||||||
local Sep = Separator or "/"
|
local windows_separator = '\\'
|
||||||
local smallest_string = nil
|
|
||||||
local largest_string = nil
|
local function split( s, sep, include_sep )
|
||||||
if string.len(Left) > string.len(Right) then
|
local include = include_sep or false
|
||||||
smallest_string = Right
|
local fields, index = {}, 1
|
||||||
largest_string = Left
|
repeat
|
||||||
else
|
local occurence = s:find(sep, index, true)
|
||||||
smallest_string = Left
|
if include == true then
|
||||||
largest_string = Right
|
fields[#fields + 1] = s:sub(index - 1, occurence and occurence - 1)
|
||||||
|
else
|
||||||
|
fields[#fields + 1] = s:sub(index, occurence and occurence - 1)
|
||||||
|
end
|
||||||
|
index = occurence and occurence + #sep
|
||||||
|
until not index
|
||||||
|
return fields
|
||||||
|
end
|
||||||
|
|
||||||
|
local function slice( t, first, last, step )
|
||||||
|
local sliced = {}
|
||||||
|
|
||||||
|
local first_index = first or 1
|
||||||
|
local last_index = last or #t
|
||||||
|
local step_size = step or 1
|
||||||
|
|
||||||
|
for i = first_index, last_index, step_size do
|
||||||
|
sliced[#sliced + 1] = t[i]
|
||||||
end
|
end
|
||||||
|
|
||||||
local last_sep_idx = 1
|
return sliced
|
||||||
local sep = string.byte(Sep)
|
end
|
||||||
for i = 1, #smallest_string do
|
|
||||||
if smallest_string:byte(i) == sep then
|
|
||||||
last_sep_idx = i
|
|
||||||
end
|
|
||||||
|
|
||||||
if smallest_string:byte(i) ~= largest_string:byte(i) then
|
function PathObj:new( path )
|
||||||
|
local obj = {}
|
||||||
|
local path_blocks = split(path, posix_separator)
|
||||||
|
|
||||||
|
local slash_count = 0
|
||||||
|
for i, part in ipairs(path_blocks) do
|
||||||
|
if part == '' then
|
||||||
|
slash_count = slash_count + 1
|
||||||
|
else
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local match = string.sub(smallest_string, 1, last_sep_idx - 1)
|
if slash_count == 0 then
|
||||||
|
obj.root = ''
|
||||||
if match == '' then
|
elseif slash_count == 2 then
|
||||||
return nil
|
obj.root = posix_separator .. posix_separator
|
||||||
|
path_blocks = slice(path_blocks, 3)
|
||||||
else
|
else
|
||||||
return match
|
obj.root = posix_separator
|
||||||
end
|
path_blocks = slice(path_blocks, slash_count + 1)
|
||||||
end
|
|
||||||
|
|
||||||
function path.Get_Extension( P )
|
|
||||||
if P == nil then
|
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local rev_string = string.reverse(P)
|
if obj.root ~= '' then
|
||||||
local start_idx = 0
|
obj.parts = {obj.root, table.unpack(path_blocks)}
|
||||||
local end_idx = 0
|
|
||||||
start_idx, end_idx = string.find(rev_string, ".", 1, true)
|
|
||||||
|
|
||||||
if start_idx == nil then
|
|
||||||
return nil
|
|
||||||
else
|
else
|
||||||
return string.sub(P, #P - (start_idx -2))
|
obj.parts = path_block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
obj.drive = ''
|
||||||
|
obj.anchor = obj.drive .. obj.root
|
||||||
|
obj._segments = path_blocks
|
||||||
|
obj.name = path_blocks[#path_blocks]
|
||||||
|
|
||||||
|
local suffices = slice(split(obj.name, ".", true), 2)
|
||||||
|
|
||||||
|
obj.suffix = suffices[#suffices]
|
||||||
|
obj.suffices = suffices
|
||||||
|
|
||||||
|
setmetatable(obj, self)
|
||||||
|
self.__index = self
|
||||||
|
|
||||||
|
return obj
|
||||||
end
|
end
|
||||||
|
|
||||||
function path.Get_Extension_From_List( P, Extension_List )
|
function PathObj:to_string()
|
||||||
if Extension_List == nil or P == nil then
|
local out = self.drive .. self.root
|
||||||
return nil
|
|
||||||
|
for i = 1, #self._segments - 1 do
|
||||||
|
out = out .. self._segments[i] .. '/'
|
||||||
end
|
end
|
||||||
|
|
||||||
local ext_length = 0
|
out = out .. self._segments[#self._segments]
|
||||||
for _,item in ipairs(Extension_List) do
|
|
||||||
local current_ext_length = #item
|
|
||||||
if current_ext_length > ext_length then
|
|
||||||
ext_length = current_ext_length
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local rev_string = string.reverse(P)
|
return out
|
||||||
local start_idx = 0
|
|
||||||
local end_idx = 0
|
|
||||||
local match = nil
|
|
||||||
while ( true ) do
|
|
||||||
start_idx, end_idx = string.find(rev_string, ".", start_idx+1, true)
|
|
||||||
if start_idx == nil or (start_idx-1) > ext_length then
|
|
||||||
break
|
|
||||||
end
|
|
||||||
|
|
||||||
for _,exts in ipairs(Extension_List) do
|
|
||||||
if exts == string.sub(P, #P - (start_idx -2)) then
|
|
||||||
match = exts
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return match
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function path.Get_Ext( P, Extension_List )
|
function PathObj:data_repr()
|
||||||
if Extension_List == nil then
|
print("root = " .. self.root)
|
||||||
return path.Get_Extension(P)
|
local segments = "segments = ["
|
||||||
else
|
for _, seg in ipairs(self.parts) do
|
||||||
return path.Get_Extension_From_List(P, Extension_List)
|
segments = segments .. seg .. ","
|
||||||
end
|
end
|
||||||
end
|
segments = segments .. "]"
|
||||||
|
print(segments)
|
||||||
|
|
||||||
function path.Walk_Up( P )
|
print("name = " .. self.name)
|
||||||
if P == "/" then
|
print("suffix = " .. self.suffix)
|
||||||
return nil
|
|
||||||
|
local suffices = "suffices = ["
|
||||||
|
for _, seg in ipairs(self.suffices) do
|
||||||
|
suffices = suffices .. seg .. ","
|
||||||
end
|
end
|
||||||
local rev = string.reverse(P)
|
suffices = suffices .. "]"
|
||||||
local start = 1
|
print(suffices)
|
||||||
if P:sub(-1) == "/" then
|
|
||||||
start = start + 1
|
|
||||||
end
|
|
||||||
|
|
||||||
local slash_first_idx, _ = string.find(rev, "/", start, true)
|
|
||||||
|
|
||||||
return string.sub(P, 1, #P - slash_first_idx + 1)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function path.Get_File( P )
|
function PathObj:is_absolute()
|
||||||
local rev = string.reverse(P)
|
return self.root ~= ''
|
||||||
local start = 1
|
|
||||||
|
|
||||||
local slash_first_idx, _ = string.find(rev, "/", start, true)
|
|
||||||
|
|
||||||
return string.sub(P, #P - slash_first_idx + 2)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function path.Get_Filename( File )
|
print(PathObj:new( "home/folkert/test.ads" ):to_string())
|
||||||
local dot_first_idx, _ = string.find(File, ".", 1, true)
|
print(PathObj:new( "/home/folkert/test.ads" ):to_string())
|
||||||
|
print(PathObj:new( "//home/folkert/test.ads" ):to_string())
|
||||||
|
print(PathObj:new( "///home/folkert/test.ads" ):is_absolute())
|
||||||
|
|
||||||
return string.sub(File, 1, dot_first_idx - 1)
|
local x = PathObj:new( "/home/foo/bar/baz.c" )
|
||||||
end
|
print(x:to_string())
|
||||||
|
|
||||||
function path.Generate_Dir_Walk( Base, Descend_List )
|
return PathObj
|
||||||
Descend_List = Descend_List or {}
|
|
||||||
local Descend_Length = #Descend_List
|
|
||||||
|
|
||||||
local i = -1
|
|
||||||
local Base_Dir = Base
|
|
||||||
function Inner()
|
|
||||||
if i == Descend_Length then
|
|
||||||
local Temp_Dir = path.Walk_Up(Base_Dir)
|
|
||||||
if Temp_Dir ~= nil then
|
|
||||||
Base_Dir = Temp_Dir
|
|
||||||
i = -1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
i = i + 1
|
|
||||||
if i == 0 then
|
|
||||||
return Base_Dir
|
|
||||||
elseif i <= Descend_Length then
|
|
||||||
return Base_Dir .. Descend_List[i] .. "/"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return Inner
|
|
||||||
end
|
|
||||||
|
|
||||||
return path
|
|
||||||
|
|
|
||||||
24
test/PathObj.lua
Normal file
24
test/PathObj.lua
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
package.path = package.path .. ";../lua/SpecSwitcher/path.lua"
|
||||||
|
local PathObj = require("path")
|
||||||
|
|
||||||
|
local tests = {}
|
||||||
|
|
||||||
|
function tests.Always_Fail()
|
||||||
|
assert(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
function tests.Test_Path_Functionality()
|
||||||
|
local path_1 = "/foo/bar/baz/beef.c"
|
||||||
|
local path_2 = "//foo/bar/baz/beef.c"
|
||||||
|
local path_3 = "///foo/bar/baz/beef.c"
|
||||||
|
local path_4 = "foo/bar/baz/beef.c"
|
||||||
|
|
||||||
|
assert( PathObj:new(path_1):to_string() == path_1 )
|
||||||
|
assert( PathObj:new(path_2):to_string() == path_2 )
|
||||||
|
assert( PathObj:new(path_3):to_string() == path_1 )
|
||||||
|
assert( PathObj:new(path_4):to_string() == path_4 )
|
||||||
|
end
|
||||||
|
|
||||||
|
for test_functions in pairs(tests) do
|
||||||
|
tests[test_functions]()
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user