Skip to content

Commit

Permalink
fix(base): add ability to specify separator with :tostring
Browse files Browse the repository at this point in the history
  • Loading branch information
pysan3 committed Feb 18, 2024
1 parent 8d126d8 commit 0bf7423
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions lua/pathlib/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -920,29 +920,35 @@ end
---Convert path object to string
---@return string
function Path:__tostring()
if not self.__string_cache then
self.__string_cache = table.concat(self._raw_paths, self.sep_str):gsub([[^%./]], ""):gsub([[//]], "/")
return self:tostring()
end

---Alias to `tostring(self)`
---@param sep string|nil # If not nil, this is used as a path separator.
---@return string
function Path:tostring(sep)
local nocache = sep and sep ~= self.sep_str
if nocache or not self.__string_cache then
local s = table.concat(self._raw_paths, self.sep_str)
if self:is_absolute() then
if #self._raw_paths == 1 then
self.__string_cache = self.sep_str
s = self.sep_str
end
if self._drive_name:len() > 0 then
self.__string_cache = self._drive_name .. self.__string_cache
s = self._drive_name .. s
end
end
end
if self.__string_cache:len() == 0 then
return "."
if s:len() == 0 then
return "."
elseif nocache then
return s
else
self.__string_cache = s
end
end
return self.__string_cache
end

---Alias to `tostring(self)`
---@return string
function Path:tostring()
return self:__tostring()
end

-- ╭─────────────────────────────────────────────────────────╮ --
-- │ Watcher Methods │ --
-- ╰─────────────────────────────────────────────────────────╯ --
Expand Down

0 comments on commit 0bf7423

Please sign in to comment.