Example 1:
|
?split_path("C:\\Program Files (x86)\\Phix\\demo\\edix")
-- {"C:","Program Files (x86)","Phix","demo","edix"}
?split_path("demo\\edix") -- {"demo","edix"}
?split_path("\\demo\\edix") -- {"\\demo","edix"}
?split_path("usr/home/john") -- {"usr","home","john"}
?split_path("/usr/home/john") -- {"/usr","home","john"}
?split_path("demo\\edix\\") -- {"demo","edix"}
?split_path("demo\\edix\\",preservetrailsep:=true) -- {"demo","edix\\"}
?split_path("demo\\edix",preservetrailsep:=true) -- {"demo","edix"}
?split_path("demo/edix/") -- {"demo","edix"}
?split_path("demo/edix/",preservetrailsep:=true) -- {"demo","edix/"}
?split_path("") -- {}
The use of a named parameter for preservetrailsep makes the intent much clearer.
|
Technicalia
|
It may be that many programs, in which the following kind of path separator confusion occurs internally,
actually work perfectly fine anyway - but there is certainly no guarantee of that.
Note that join_path automatically uses the appropriate platform-specific separator, however should split_path preserve
any leading/trailing separators, it will do so without substituting them for the correct platform-specific one, which
as shown in this second example may cause mishaps (as marked with (*)). Should this occur, my recommendation would be to
use substitute() on path before passing it to split_path(), as shown in the third example,
or better yet (as long as you do not mind that any relative paths are replaced with fully qualified ones)
get_proper_path().
UPDATE: two of the original errors have now been fixed, but one more new one has been found (though my attitude remains
that you would be far better off properly testing an application on different platforms, rather than complaining that
this routine occasionally does daft things if you ask it to, such as use /home on Windows or C:\\ on Linux).
Example 2:
?join_path(split_path("C:\\Program Files (x86)\\Phix\\demo\\edix"))
-- "C:\\Program Files (x86)\\Phix\\demo\\edix" (on Windows)
?join_path(split_path("demo\\edix")) -- "demo\\edix" (on Windows)
?join_path(split_path("\\demo\\edix")) -- "\\demo\\edix" (on Windows)
?join_path(split_path("\\demo\\edix")) -- "\\demo/edix" (on Linux (*))
?join_path(split_path("home/john")) -- "home\\john" (on Windows)
?join_path(split_path("/home/john")) -- "/home/john" (on Linux)
?join_path(split_path("/home/john")) -- "/home\\john" (on Windows (*))
?join_path(split_path("demo\\edix\\"),1) -- "demo\\edix\\" (on Windows)
?join_path(split_path("demo/edix/",1),1) -- "demo\\edix\\" (on Windows) [now fixed]
?join_path(split_path("demo/edix/"),1) -- "demo/edix/" (on Linux)
?join_path(split_path("demo\\edix\\",1),1) -- "demo/edix/" (on Linux) [now fixed]
?join_path(split_path("")) -- ""
It is of course join_path(), rather than split_path(), that is giving us the platform-specific results here.
Example 3:
string {sep1,sep2} = iff(platform()=WINDOWS?{"/","\\"}:{"\\","/"}),
path = substitute("demo/edix/",sep1,sep2)
--or
--string path = get_proper_path("demo/edix/",1)
-- (path may be "C:\\Program Files (x86)\\Phix\\demo\\edix\\")
?join_path(split_path(path,1)) -- "/demo/edix" (on Linux)
?join_path(split_path(path,1),1) -- "demo\\edix\\" (on Windows)
?join_path(split_path(path,1),1) -- "demo/edix/" (on Linux)
I should also note that there is no direct correlation between the preservetrailsep parameter of split_path and the
trailsep argument of join_path - sometimes both may be needed, sometimes one but not the other, sometimes the other
way round, and sometimes neither.
The relatively trivial implementation of this routine can be found in the autoinclude builtins\psplit.e
|