path.lua

changeset 7
038275cd92ed
parent 3
b2df1b3f2c83
--- a/path.lua	Sun May 07 20:02:53 2017 +0100
+++ b/path.lua	Tue Jan 16 16:39:48 2018 +0000
@@ -1,14 +1,15 @@
 
-module("path", package.seeall)
+--@module path
+local path={}
 
-require("tuple")
-require("lfs")
-require("err")
-require("config")
+local tuple=require("tuple")
+local lfs=require("lfs")
+local err=require("err")
+local config=require("config")
 
 local sep=config.dirsep
 
-function join(p, f)
+function path.join(p, f)
     if p=="" then
         return f
     else
@@ -16,7 +17,7 @@
     end
 end
 
-function dirbasename(p)
+function path.dirbasename(p)
     local d, b = string.match(p,
         "^(.*)"..sep.."+([^"..sep.."]+)"..sep.."*$")
     if not b then
@@ -26,11 +27,11 @@
     end
 end
 
-function dirname(p)
+function path.dirname(p)
     return tuple.fst(dirbasename(p))
 end
 
-function dirname_slash(p)
+function path.dirname_slash(p)
     local dn=dirname(p)
     if dn=="" then
         return dn
@@ -39,17 +40,17 @@
     end
 end
 
-function basename(p)
+function path.basename(p)
     return tuple.snd(dirbasename(p))
 end
 
-function rmext(p)
+function path.rmext(p)
     return string.gsub(p, "%.[^.]*$", "")
 end
 
 -- would rather do this as an iterator, but can't
 -- coroutine.yield from gsub handler
-function split(p)
+function path.split(p)
     local t={}
     local head=""
     local s, p2 = string.match(p, "^("..sep.."+)(.*)$")
@@ -63,7 +64,7 @@
 end
 
 --[[
-function parts(p)
+function path.parts(p)
     local s={i=1, t=split(p)}
     local function g(s)
         local v=s.t[s.i]
@@ -73,7 +74,7 @@
     return g, s
 end
 
-function makepath(p, head)
+function path.makepath(p, head)
     head=head or ""
     for d in parts(p) do
         head=head..d
@@ -91,7 +92,7 @@
 end
 ]]
 
-function simplify(p)
+function path.simplify(p)
     local capts={}
     local start=string.match(p, "^(/)")
     string.gsub(p, "([^/]+)",
@@ -105,6 +106,8 @@
     return (start or "")..table.concat(capts, "/")
 end
 
-function to_root(p)
-    return string.rep("../",#(split(p))-1)
+function path.to_root(p)
+    return string.rep("../",#(path.split(p))-1)
 end
+
+return path

mercurial