Using a standard "{self}/{path}" does not work in the following cases:
self
is empty.path
starts with '/'
.This method ensures that the join is valid.
assert "hello".join_path("world") == "hello/world"
assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
assert "".join_path("world") == "world"
assert "hello".join_path("/world") == "/world"
assert "hello/".join_path("world") == "hello/world"
assert "hello/".join_path("/world") == "/world"
Note: You may want to use simplify_path
on the result.
Note: This method works only with POSIX paths.
# Correctly join two path using the directory separator.
#
# Using a standard "{self}/{path}" does not work in the following cases:
#
# * `self` is empty.
# * `path` starts with `'/'`.
#
# This method ensures that the join is valid.
#
# assert "hello".join_path("world") == "hello/world"
# assert "hel/lo".join_path("wor/ld") == "hel/lo/wor/ld"
# assert "".join_path("world") == "world"
# assert "hello".join_path("/world") == "/world"
# assert "hello/".join_path("world") == "hello/world"
# assert "hello/".join_path("/world") == "/world"
#
# Note: You may want to use `simplify_path` on the result.
#
# Note: This method works only with POSIX paths.
fun join_path(path: Text): String
do
if path.is_empty then return self.to_s
if self.is_empty then return path.to_s
if path.chars[0] == '/' then return path.to_s
if self.last == '/' then return "{self}{path}"
return "{self}/{path}"
end
lib/core/file.nit:1099,2--1125,4