Deeper C++-ification

This commit is contained in:
Nathan Woods 2016-07-11 08:27:03 -04:00
parent 130e05cc4e
commit 2bd5932b42

View File

@ -1272,17 +1272,21 @@ core_file::core_file()
std::string core_filename_extract_base(const std::string &name, bool strip_extension) std::string core_filename_extract_base(const std::string &name, bool strip_extension)
{ {
/* find the start of the name */ // find the start of the basename
const char *start = name.c_str() + name.length(); auto start = name.end();
while (start > name.c_str() && !util::is_directory_separator(start[-1])) while (start != name.begin() && !util::is_directory_separator(start[-1]))
start--; start--;
/* copy the rest into an astring */ // find the end of the basename
std::string result(start); auto chop_position = strip_extension
? name.find_last_of('.')
: std::string::npos;
auto end = chop_position != std::string::npos
? name.begin() + chop_position
: name.end();
/* chop the extension if present */ // copy the result into an string
if (strip_extension) std::string result(start, end);
result = result.substr(0, result.find_last_of('.'));
return result; return result;
} }