[code] Bugfix: Relative to absolute path conversion

From: Niklas W <nikw.att.gmx.com>
Date: Wed, 18 Nov 2015 14:59:13 +0100

When trying to open a file from the commandline using a relative path
like "../../../somedir/somefile", the resulting absolute path does not
come out right, resulting in textadept opening a new file at the wrong


-- Returns the absolute path to string *filename*.
-- *prefix* or `lfs.currentdir()` is prepended to a relative filename. The
-- returned path is not guaranteed to exist.
-- @param filename The relative or absolute path to a file.
-- @param prefix Optional prefix path prepended to a relative filename.
-- @return string absolute path
-- @name abspath
function lfs.abspath(filename, prefix)
  if WIN32 then filename = filename:gsub('/', '\\') end
  if not filename:find(not WIN32 and '^/' or '^%a:[/\\]') and
     not (WIN32 and filename:find('^\\\\')) then
    prefix = prefix or lfs.currentdir()
    filename = prefix..(not WIN32 and '/' or '\\')..filename
  filename = filename:gsub('%f[^/\\]%.[/\\]', '') -- clean up './'
- while filename:find('[^/\\]+[/\\]%.%.[/\\]') do
-   filename = filename:gsub('[^/\\]+[/\\]%.%.[/\\]', '') -- clean up '../'
- end
+ while true do
+   a, b = filename:find('[^/\\]+[/\\]%.%.[/\\]')
+   if not a then break; end
+   filename = filename:sub(1, a-1) .. filename:sub(b+1)
+ end
  return filename
The problem was that filename:gsub(...) removes more than expected.
One can instead just use the result of filename:find(....) to remove
the matched string.
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Wed 18 Nov 2015 - 08:59:13 EST

This archive was generated by hypermail 2.2.0 : Thu 19 Nov 2015 - 06:27:19 EST