diff --git a/src/music/midifile.cpp b/src/music/midifile.cpp index 39e9f17c1f..80b2f56a18 100644 --- a/src/music/midifile.cpp +++ b/src/music/midifile.cpp @@ -1051,12 +1051,7 @@ std::string MidiFile::GetSMFFile(const MusicSongInfo &song) char basename[MAX_PATH]; { - const char *fnstart = strrchr(song.filename.c_str(), PATHSEPCHAR); - if (fnstart == nullptr) { - fnstart = song.filename.c_str(); - } else { - fnstart++; - } + const char *fnstart = StrLastPathSegment(song.filename); /* Remove all '.' characters from filename */ char *wp = basename; diff --git a/src/network/network_survey.cpp b/src/network/network_survey.cpp index 9c7e429fc7..d2ef43c88f 100644 --- a/src/network/network_survey.cpp +++ b/src/network/network_survey.cpp @@ -150,13 +150,7 @@ static void SurveyConfiguration(nlohmann::json &survey) { survey["network"] = _networking ? (_network_server ? "server" : "client") : "no"; if (_current_language != nullptr) { - std::string_view language_basename(_current_language->file); - auto e = language_basename.rfind(PATHSEPCHAR); - if (e != std::string::npos) { - language_basename = language_basename.substr(e + 1); - } - - survey["language"]["filename"] = language_basename; + survey["language"]["filename"] = StrLastPathSegment(_current_language->file); survey["language"]["name"] = _current_language->name; survey["language"]["isocode"] = _current_language->isocode; } diff --git a/src/string.cpp b/src/string.cpp index 685594f8b4..b68112f71c 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -420,6 +420,17 @@ void StrTrimInPlace(std::string &str) StrRightTrimInPlace(str); } +const char *StrLastPathSegment(const char *path) +{ + const char *best = path; + for (; *path != '\0'; path++) { + if (*path == PATHSEPCHAR || *path == '/') { + if (*(path + 1) != '\0') best = path + 1; + } + } + return best; +} + /** * Check whether the given string starts with the given prefix. * @param str The string to look at. diff --git a/src/string_func.h b/src/string_func.h index 2b117e4fed..739c26bf17 100644 --- a/src/string_func.h +++ b/src/string_func.h @@ -66,6 +66,13 @@ bool strtolower(std::string &str, std::string::size_type offs = 0); [[nodiscard]] bool StrValid(const char *str, const char *last) NOACCESS(2); void StrTrimInPlace(std::string &str); +const char *StrLastPathSegment(const char *path); + +inline const char *StrLastPathSegment(const std::string &path) +{ + return StrLastPathSegment(path.c_str()); +} + [[nodiscard]] bool StrStartsWith(const std::string_view str, const std::string_view prefix); [[nodiscard]] bool StrStartsWithIgnoreCase(std::string_view str, const std::string_view prefix); [[nodiscard]] bool StrEndsWith(const std::string_view str, const std::string_view suffix); diff --git a/src/strings.cpp b/src/strings.cpp index 954ddfe716..2b396a016b 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -2268,7 +2268,7 @@ bool ReadLanguagePack(const LanguageMetadata *lang) _current_language = lang; const TextDirection old_text_dir = _current_text_dir; _current_text_dir = (TextDirection)_current_language->text_dir; - const char *c_file = strrchr(_current_language->file, PATHSEPCHAR) + 1; + const char *c_file = StrLastPathSegment(_current_language->file); _config_language_file = c_file; SetCurrentGrfLangID(_current_language->newgrflangid); @@ -2444,7 +2444,7 @@ void InitializeLanguagePacks() /* We are trying to find a default language. The priority is by * configuration file, local environment and last, if nothing found, * English. */ - const char *lang_file = strrchr(lng.file, PATHSEPCHAR) + 1; + const char *lang_file = StrLastPathSegment(lng.file); if (_config_language_file == lang_file) { chosen_language = &lng; break;