# join_path

Definition: | string path = join_path(sequence path_elements, bool trailsep=false)
Description: | Join several path segments together using the appropriate separator. path_elements: fragments to be joined together, each of which must be a string. trailsep: if true, ensure any non-empty result has a trailing separator. Returns: a string, the joined path.
pwa/p2js: | Supported (after all it is only a bit of text manipulation, not that you can really do much with the result in a browser, also note that p2js.js declares SLASH as 0x2f aka '/', so results tend to match Linux rather than Windows).
Comments: | Unlike some other routines here, any path separators already present in any path_elements fragments should be appropriate for the operating system, namely backslash ('\\') on Windows and forwardslash ('/') on Linux, though it should handle/replace incorrect separators at the end of fragments correctly - just not those at the start or somewhere in the middle. As far as reasonably possible (not mid-fragment), duplicate path separators are eliminated from the result. Empty fragments are permitted, and ignored. A leading separator is preserved, as per the examples. By default the result never has a trailing slash, which makes the routine suitable for constructing both file and directory paths, however that can be overriden (for directory path use) by specifying a non-zero trailsep, as long as the result would not otherwise be "". There is also a split_path(string path) function, which is the logical counterpart to join_path(). The relatively trivial implementation of this routine can be found in the autoinclude builtins\pflatten.e
Examples: |
    ?join_path({"demo","edix"})                 -- `demo\edix` (on Windows)
    ?join_path({`\demo\`,`\edix\`})             -- `\demo\edix` (on Windows)
    ?join_path({"home/","/john"})               -- "home/john" (on Linux)
    ?join_path({"/home","john"})                -- "/home/john" (on Linux)
    ?join_path({"demo","edix"},trailsep:=true)  -- `demo\edix\` (on Windows)
    ?join_path({"demo","edix"},trailsep:=true)  -- "demo/edix/" (on Linux)
    ?join_path({"",""},trailsep:=true)          -- "" (0-length string, on Windows and Linux)
    ?join_path({},trailsep:=true)               -- "" (ditto)
    ?join_path({})                              -- "" (ditto)
The use of a named parameter for trailsep makes the intent much clearer.
Implementation: | See builtins\pflatten.e (an autoinclude) for details of the actual implementation.