From 3b0f60e5c2a1a573bec46740982b48ca32b835c4 Mon Sep 17 00:00:00 2001
From: Kirill Elagin <kirelagin@gmail.com>
Date: Mon, 13 Jul 2015 15:25:13 +0300
Subject: [PATCH] baseNameOf: Enhance `basename` compatibility
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* If the path ends with a slash, drop it.
* If the remaining path doesn’t contain slashes, just return it.

Fixes #574.
---
 src/libutil/util.cc | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 7959b76f8..11c75d2cd 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -149,10 +149,19 @@ Path dirOf(const Path & path)
 
 string baseNameOf(const Path & path)
 {
-    Path::size_type pos = path.rfind('/');
+    if (path.empty())
+        return string("");
+
+    Path::size_type last = path.length() - 1;
+    if (path[last] == '/' && last > 0)
+        last -= 1;
+
+    Path::size_type pos = path.rfind('/', last);
     if (pos == string::npos)
-        throw Error(format("invalid file name ‘%1%’") % path);
-    return string(path, pos + 1);
+        pos = 0;
+    else
+        pos += 1;
+    return string(path, pos, last - pos + 1);
 }
 
 
-- 
GitLab