[code] Add basename to lfs

From: Pedro Andres Aranda Gutierrez <paaguti.att.gmail.com>
Date: Sat, 20 Jan 2018 08:46:15 +0100

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.

Best, /PA

-- 
Fragen sind nicht da um beantwortet zu werden,
Fragen sind da um gestellet zu werden
Georg Kreisler
-- 
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 - 02:46:15 EST

This archive was generated by hypermail 2.2.0 : Sat 20 Jan 2018 - 06:54:44 EST