Re: [code] Add basename to lfs

From: Mitchell <m.att.foicica.com>
Date: Sat, 20 Jan 2018 09:48:31 -0500 (EST)

Hi Pedro,

On Sat, 20 Jan 2018, Pedro Andres Aranda Gutierrez wrote:

> Hi
>
> here is a patch to move the extraction of the basename to lfs. I use it in
> several modules and snippets.
>
> diff -r c42366c06ff4 core/lfs_ext.lua
> --- a/core/lfs_ext.lua Wed Jan 17 12:33:34 2018 -0500
> +++ b/core/lfs_ext.lua Sat Jan 20 08:40:44 2018 +0100
> @@ -127,3 +127,16 @@
> end
> return filename
> end
> +
> +---
> +-- Returns the basename of string *filename*.
> +-- returned path is not guaranteed to exist.
> +-- @param filename The relative or absolute path of a file
> +-- @return string the path stripped of all directories (and drives in
> WIN32)
> +-- @name basename
> +function lfs.basename(filename)
> + fpattern = WIN32 and "^(.*[\\:])([^\\]+)$" or "^(.*/)([^/]+)$"
> + path, fname = (filename or ""):match(fpattern)
> +
> + return fname or fspec
> +end
>
> returning path instead of fname implements the dirname functionality of
> bash and other shells. I don't have it, because I have not found a use case
> for it.

This is a one-liner in Lua:

   (buffer.filename or ''):match('[^/\\]+$')

I don't see a need to add it as an explicit extension function to `lfs`.

For extra credit, fetching the path part is also a one-liner:

   (buffer.filename or ''):match('^.+[/\\]')

Textadept makes extensive use of these calls internally for dealing with file parts.

Cheers,
Mitchell

-- 
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 Sat 20 Jan 2018 - 09:48:31 EST

This archive was generated by hypermail 2.2.0 : Sun 21 Jan 2018 - 06:35:22 EST