about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorKirill Elagin <kirelagin@gmail.com>2015-07-13T12·25+0300
committerEelco Dolstra <eelco.dolstra@logicblox.com>2015-08-07T01·35+0200
commit3b0f60e5c2a1a573bec46740982b48ca32b835c4 (patch)
treecc00f328f76e3071d33856104f43a71243bca22b /src
parent896428c81850bf53488e35fee2c40f8218f4574e (diff)
baseNameOf: Enhance `basename` compatibility
* If the path ends with a slash, drop it.
* If the remaining path doesn’t contain slashes, just return it.

Fixes #574.
Diffstat (limited to 'src')
-rw-r--r--src/libutil/util.cc15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 7959b76f868d..11c75d2cda4c 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);
 }