diff --git a/docs/changelog.txt b/docs/changelog.txt index f91284001..219cf8d18 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -55,6 +55,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - ``gui.Painter``: fixed error when calling ``viewport()`` method - ``gui.dwarfmode``: new function: ``enterSidebarMode(sidebar_mode, max_esc)`` which uses keypresses to get into the specified sidebar mode from whatever the current screen is - `reveal`: now exposes ``unhideFlood(pos)`` functionality to Lua +- new utility function: ``utils.normalizePath()``: normalizes directory slashes across platoforms to ``/`` and coaleses adjacent directory separators - ``utils.processArgsGetopt()``: now returns negative numbers (e.g. ``-10``) in the list of positional parameters instead of treating it as an option string equivalent to ``-1 -0`` - ``utils.processArgsGetopt()``: now properly handles ``--`` like GNU ``getopt`` as a marker to treat all further parameters as non-options - ``utils.processArgsGetopt()``: now detects when required arguments to long-form options are missing diff --git a/library/lua/utils.lua b/library/lua/utils.lua index b0bc70af4..39a6c156e 100644 --- a/library/lua/utils.lua +++ b/library/lua/utils.lua @@ -544,6 +544,13 @@ function check_number(text) return nv ~= nil, nv end +-- Normalize directory separator slashes across platforms to '/' and collapse +-- adjacent slashes into a single slash. +local platformSlash = package.config:sub(1,1) +function normalizePath(path) + return path:gsub(platformSlash, '/'):gsub('/+', '/') +end + function invert(tab) local result = {} for k,v in pairs(tab) do diff --git a/scripts b/scripts index 98801f3dc..af53a985c 160000 --- a/scripts +++ b/scripts @@ -1 +1 @@ -Subproject commit 98801f3dc979cf6a3277dde0b6617bfe5ac5e017 +Subproject commit af53a985c54b29a5b3092127a5e8ec3495ef0cee diff --git a/test/library/utils.lua b/test/library/utils.lua index b2c63dafd..6232bd9a2 100644 --- a/test/library/utils.lua +++ b/test/library/utils.lua @@ -14,6 +14,20 @@ function test.OrderedTable() end end +function test.normalizePath() + expect.eq('imapath/file.csv', utils.normalizePath('imapath/file.csv')) + expect.eq('/ima/path', utils.normalizePath('/ima/path')) + expect.eq('ima/path', utils.normalizePath('ima//path')) + + expect.eq('imapath', utils.normalizePath('imapath')) + expect.eq('/imapath', utils.normalizePath('/imapath')) + expect.eq('/imapath', utils.normalizePath('//imapath')) + expect.eq('/imapath', utils.normalizePath('///imapath')) + + expect.eq('imapath/', utils.normalizePath('imapath/')) + expect.eq('imapath/', utils.normalizePath('imapath//')) +end + function test.invert() local t = {} local i = utils.invert{'a', 4.4, false, true, 5, t}