ref: a5a4e162cec3bb59c7d88a703d45edc916392453
dir: /libstd/pathjoin.myr/
use "alloc.use" use "strjoin.use" use "strsplit.use" use "sleq.use" use "sldup.use" use "slcp.use" use "die.use" use "fmt.use" pkg std = const pathcat : (a : byte[:], b : byte[:] -> byte[:]) const pathjoin : (p : byte[:][:] -> byte[:]) const pathnorm : (p : byte[:] -> byte[:]) ;; const pathcat = {a, b -> pathjoin([a, b][:]) } const pathjoin = {l var p, q p = strjoin(l, "/") q = pathnorm(p) slfree(p) -> q } const pathnorm = {p var comps var i, del, dst var s, ret comps = strsplit(p, "/") /* "." is a no-op component, so we drop it. In order to drop a component, we set it to the empty string, and remove it later on in the code. */ for i = 0; i < comps.len; i++ if sleq(comps[i], ".") comps[i] = "" ;; ;; /* then, resolve '..' by cancelling out previous components. Scan backwards in the component list for the first real component, and delete both it and the '..' that lead to it. Leave in extra '..' components, so that, eg, ../foo doesn't get mangled. */ for i = 0; i < comps.len; i++ if !sleq(comps[i], "..") continue ;; for del = 1; del <= i; del++ if comps[i - del].len > 0 && !sleq(comps[i-del], "..") comps[i - del] = "" comps[i] = "" break ;; ;; ;; /* clear out the path nodes we decided to drop */ dst = 0 for i = 0; i < comps.len; i++ if comps[i].len > 0 comps[dst++] = comps[i] ;; ;; comps = comps[:dst] /* and reassemble. If we have an absolute path, make it absolute. If we have an empty path, return ".". Otherwise, just return the path. */ if p.len > 0 && sleq(p[:1], "/") for i = 0; i < comps.len; i++ if !sleq(comps[i], "..") break ;; ;; s = strjoin(comps[i:], "/") ret = fmt("/%s", s) slfree(s) elif comps.len == 0 ret = sldup(".") else ret = strjoin(comps, "/") ;; slfree(comps) -> ret }