From 413d1ef8a74c42ead47f3740835e0f0d0203eb7f Mon Sep 17 00:00:00 2001 From: V1K1NGbg Date: Sun, 9 Jun 2024 18:53:57 +0200 Subject: core.dir - converted --- src/luarocks/core/dir-origianl.lua | 98 ++++++++++++++++++++++++++++++++++++++ src/luarocks/core/dir.lua | 51 ++++++++++---------- src/luarocks/core/dir.tl | 98 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 221 insertions(+), 26 deletions(-) create mode 100644 src/luarocks/core/dir-origianl.lua create mode 100644 src/luarocks/core/dir.tl diff --git a/src/luarocks/core/dir-origianl.lua b/src/luarocks/core/dir-origianl.lua new file mode 100644 index 00000000..5d6f2c9f --- /dev/null +++ b/src/luarocks/core/dir-origianl.lua @@ -0,0 +1,98 @@ + +local dir = {} + +local require = nil +-------------------------------------------------------------------------------- + +local dir_sep = package.config:sub(1, 1) + +local function unquote(c) + local first, last = c:sub(1,1), c:sub(-1) + if (first == '"' and last == '"') or + (first == "'" and last == "'") then + return c:sub(2,-2) + end + return c +end + +--- Describe a path in a cross-platform way. +-- Use this function to avoid platform-specific directory +-- separators in other modules. Removes trailing slashes from +-- each component given, to avoid repeated separators. +-- Separators inside strings are kept, to handle URLs containing +-- protocols. +-- @param ... strings representing directories +-- @return string: a string with a platform-specific representation +-- of the path. +function dir.path(...) + local t = {...} + while t[1] == "" do + table.remove(t, 1) + end + for i, c in ipairs(t) do + t[i] = unquote(c) + end + return dir.normalize(table.concat(t, "/")) +end + +--- Split protocol and path from an URL or local pathname. +-- URLs should be in the "protocol://path" format. +-- For local pathnames, "file" is returned as the protocol. +-- @param url string: an URL or a local pathname. +-- @return string, string: the protocol, and the pathname without the protocol. +function dir.split_url(url) + assert(type(url) == "string") + + url = unquote(url) + local protocol, pathname = url:match("^([^:]*)://(.*)") + if not protocol then + protocol = "file" + pathname = url + end + return protocol, pathname +end + +--- Normalize a url or local path. +-- URLs should be in the "protocol://path" format. +-- Removes trailing and double slashes, and '.' and '..' components. +-- for 'file' URLs, the native system's slashes are used. +-- @param url string: an URL or a local pathname. +-- @return string: Normalized result. +function dir.normalize(name) + local protocol, pathname = dir.split_url(name) + pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/") + local pieces = {} + local drive = "" + if pathname:match("^.:") then + drive, pathname = pathname:match("^(.:)(.*)$") + end + pathname = pathname .. "/" + for piece in pathname:gmatch("(.-)/") do + if piece == ".." then + local prev = pieces[#pieces] + if not prev or prev == ".." then + table.insert(pieces, "..") + elseif prev ~= "" then + table.remove(pieces) + end + elseif piece ~= "." then + table.insert(pieces, piece) + end + end + if #pieces == 0 then + pathname = drive .. "." + elseif #pieces == 1 and pieces[1] == "" then + pathname = drive .. "/" + else + pathname = drive .. table.concat(pieces, "/") + end + if protocol ~= "file" then + pathname = protocol .. "://" .. pathname + else + pathname = pathname:gsub("/", dir_sep) + end + return pathname +end + +return dir + diff --git a/src/luarocks/core/dir.lua b/src/luarocks/core/dir.lua index 5d6f2c9f..ce73363b 100644 --- a/src/luarocks/core/dir.lua +++ b/src/luarocks/core/dir.lua @@ -1,31 +1,31 @@ - +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local package = _tl_compat and _tl_compat.package or package; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table local dir = {} local require = nil --------------------------------------------------------------------------------- + local dir_sep = package.config:sub(1, 1) local function unquote(c) - local first, last = c:sub(1,1), c:sub(-1) + local first, last = c:sub(1, 1), c:sub(-1) if (first == '"' and last == '"') or (first == "'" and last == "'") then - return c:sub(2,-2) + return c:sub(2, -2) end return c end ---- Describe a path in a cross-platform way. --- Use this function to avoid platform-specific directory --- separators in other modules. Removes trailing slashes from --- each component given, to avoid repeated separators. --- Separators inside strings are kept, to handle URLs containing --- protocols. --- @param ... strings representing directories --- @return string: a string with a platform-specific representation --- of the path. + + + + + + + + + function dir.path(...) - local t = {...} + local t = { ... } while t[1] == "" do table.remove(t, 1) end @@ -35,11 +35,11 @@ function dir.path(...) return dir.normalize(table.concat(t, "/")) end ---- Split protocol and path from an URL or local pathname. --- URLs should be in the "protocol://path" format. --- For local pathnames, "file" is returned as the protocol. --- @param url string: an URL or a local pathname. --- @return string, string: the protocol, and the pathname without the protocol. + + + + + function dir.split_url(url) assert(type(url) == "string") @@ -52,12 +52,12 @@ function dir.split_url(url) return protocol, pathname end ---- Normalize a url or local path. --- URLs should be in the "protocol://path" format. --- Removes trailing and double slashes, and '.' and '..' components. --- for 'file' URLs, the native system's slashes are used. --- @param url string: an URL or a local pathname. --- @return string: Normalized result. + + + + + + function dir.normalize(name) local protocol, pathname = dir.split_url(name) pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/") @@ -95,4 +95,3 @@ function dir.normalize(name) end return dir - diff --git a/src/luarocks/core/dir.tl b/src/luarocks/core/dir.tl new file mode 100644 index 00000000..55604e64 --- /dev/null +++ b/src/luarocks/core/dir.tl @@ -0,0 +1,98 @@ + +local dir = {} --! + +local require = nil --! +-------------------------------------------------------------------------------- + +local dir_sep = package.config:sub(1, 1) --! + +local function unquote(c: string): string + local first, last = c:sub(1,1), c:sub(-1) + if (first == '"' and last == '"') or + (first == "'" and last == "'") then + return c:sub(2,-2) + end + return c +end + +--- Describe a path in a cross-platform way. +-- Use this function to avoid platform-specific directory +-- separators in other modules. Removes trailing slashes from +-- each component given, to avoid repeated separators. +-- Separators inside strings are kept, to handle URLs containing +-- protocols. +-- @param ... strings representing directories +-- @return string: a string with a platform-specific representation +-- of the path. +function dir.path(...: string): string + local t = {...} + while t[1] == "" do + table.remove(t, 1) + end + for i, c in ipairs(t) do + t[i] = unquote(c) + end + return dir.normalize(table.concat(t, "/")) --! +end + +--- Split protocol and path from an URL or local pathname. +-- URLs should be in the "protocol://path" format. +-- For local pathnames, "file" is returned as the protocol. +-- @param url string: an URL or a local pathname. +-- @return string, string: the protocol, and the pathname without the protocol. +function dir.split_url(url: string): string, string + assert(type(url) == "string") + + url = unquote(url) + local protocol, pathname = url:match("^([^:]*)://(.*)") + if not protocol then + protocol = "file" + pathname = url + end + return protocol, pathname +end + +--- Normalize a url or local path. +-- URLs should be in the "protocol://path" format. +-- Removes trailing and double slashes, and '.' and '..' components. +-- for 'file' URLs, the native system's slashes are used. +-- @param url string: an URL or a local pathname. +-- @return string: Normalized result. +function dir.normalize(name: string): string + local protocol, pathname = dir.split_url(name) + pathname = pathname:gsub("\\", "/"):gsub("(.)/*$", "%1"):gsub("//", "/") + local pieces: {string} = {} + local drive = "" + if pathname:match("^.:") then + drive, pathname = pathname:match("^(.:)(.*)$") + end + pathname = pathname .. "/" + for piece in pathname:gmatch("(.-)/") do + if piece == ".." then + local prev = pieces[#pieces] + if not prev or prev == ".." then + table.insert(pieces, "..") + elseif prev ~= "" then + table.remove(pieces) + end + elseif piece ~= "." then + table.insert(pieces, piece) + end + end + if #pieces == 0 then + pathname = drive .. "." + elseif #pieces == 1 and pieces[1] == "" then + pathname = drive .. "/" + else + pathname = drive .. table.concat(pieces, "/") + end + if protocol ~= "file" then + pathname = protocol .. "://" .. pathname + else + pathname = pathname:gsub("/", dir_sep) + end + return pathname +end + +return dir + -- cgit v1.2.3-55-g6feb