1 /* 2 SPDX-FileCopyrightText: 2021 Nicolas Fella <nicolas.fella@gmx.de> 3 4 SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7 #ifndef KERFUFFLE_UTILS_H 8 #define KERFUFFLE_UTILS_H 9 10 namespace Kerfuffle { 11 namespace Util { 12 13 // Get the name segment from a path 14 // e.g. /foo/bar/bla -> bla 15 // /foo/bar/ -> bar lastPathSegment(const QString & path)16 QString lastPathSegment(const QString &path) 17 { 18 if (path.endsWith(QLatin1Char('/'))) { 19 const int index = path.lastIndexOf(QLatin1Char('/'), -2); 20 return path.mid(index + 1).chopped(1); 21 } else { 22 const int index = path.lastIndexOf(QLatin1Char('/')); 23 return path.mid(index + 1); 24 } 25 } 26 } 27 } 28 #endif 29