summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/corefile.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/corefile.cpp')
-rw-r--r--src/lib/util/corefile.cpp49
1 files changed, 26 insertions, 23 deletions
diff --git a/src/lib/util/corefile.cpp b/src/lib/util/corefile.cpp
index 09d99c2072b..c06426015f7 100644
--- a/src/lib/util/corefile.cpp
+++ b/src/lib/util/corefile.cpp
@@ -1270,19 +1270,21 @@ core_file::core_file()
assumptions about path separators
-------------------------------------------------*/
-std::string core_filename_extract_base(const char *name, bool strip_extension)
+std::string core_filename_extract_base(const std::string &name, bool strip_extension)
{
- /* find the start of the name */
- const char *start = name + strlen(name);
- while (start > name && !util::is_directory_separator(start[-1]))
- start--;
-
- /* copy the rest into an astring */
- std::string result(start);
-
- /* chop the extension if present */
- if (strip_extension)
- result = result.substr(0, result.find_last_of('.'));
+ // find the start of the basename
+ auto const start = std::find_if(name.rbegin(), name.rend(), [](char c) { return util::is_directory_separator(c); });
+
+ // find the end of the basename
+ auto const chop_position = strip_extension
+ ? std::find(name.rbegin(), start, '.')
+ : start;
+ auto const end = (chop_position != start)
+ ? chop_position + 1
+ : name.rbegin();
+
+ // copy the result into an string
+ std::string result(start.base(), end.base());
return result;
}
@@ -1292,19 +1294,20 @@ std::string core_filename_extract_base(const char *name, bool strip_extension)
filename end with the specified extension?
-------------------------------------------------*/
-int core_filename_ends_with(const char *filename, const char *extension)
+bool core_filename_ends_with(const std::string &filename, const std::string &extension)
{
- int namelen = strlen(filename);
- int extlen = strlen(extension);
- int matches = TRUE;
-
- /* work backwards checking for a match */
- while (extlen > 0)
+ auto namelen = filename.length();
+ auto extlen = extension.length();
+
+ // first if the extension is bigger than the name, we definitely don't match
+ bool matches = namelen >= extlen;
+
+ // work backwards checking for a match
+ while (matches && extlen > 0 && namelen > 0)
+ {
if (tolower((UINT8)filename[--namelen]) != tolower((UINT8)extension[--extlen]))
- {
- matches = FALSE;
- break;
- }
+ matches = false;
+ }
return matches;
}