StringUtil: Remove `JoinStrings`

With 12 uses of `JoinStrings` in the codebase vs 36 uses of `fmt::join`, fmtlib's range adapter for string concatenation with delimiters is clearly the preferred option.
This commit is contained in:
mitaclaw 2024-09-22 00:24:21 -07:00
parent de67c4c93b
commit 5f90673686
13 changed files with 29 additions and 38 deletions

View File

@ -6,6 +6,9 @@
#include <jni.h> #include <jni.h>
#include <fmt/format.h>
#include <fmt/ranges.h>
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/IniFile.h" #include "Common/IniFile.h"
#include "Core/ConfigManager.h" #include "Core/ConfigManager.h"
@ -58,7 +61,7 @@ Java_org_dolphinemu_dolphinemu_features_cheats_model_GeckoCheat_getCreator(JNIEn
JNIEXPORT jstring JNICALL JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_cheats_model_GeckoCheat_getNotes(JNIEnv* env, jobject obj) Java_org_dolphinemu_dolphinemu_features_cheats_model_GeckoCheat_getNotes(JNIEnv* env, jobject obj)
{ {
return ToJString(env, JoinStrings(GetPointer(env, obj)->notes, "\n")); return ToJString(env, fmt::to_string(fmt::join(GetPointer(env, obj)->notes, "\n")));
} }
JNIEXPORT jstring JNICALL JNIEXPORT jstring JNICALL

View File

@ -28,6 +28,7 @@
#endif #endif
#include <fmt/format.h> #include <fmt/format.h>
#include <fmt/ranges.h>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
@ -332,5 +333,5 @@ std::string CPUInfo::Summarize()
if (bSHA2) if (bSHA2)
sum.push_back("SHA2"); sum.push_back("SHA2");
return JoinStrings(sum, ","); return fmt::to_string(fmt::join(sum, ","));
} }

View File

@ -8,6 +8,7 @@
#include <vector> #include <vector>
#include <fmt/format.h> #include <fmt/format.h>
#include <fmt/ranges.h>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
@ -141,7 +142,7 @@ std::string EscapePath(const std::string& path)
for (const std::string& split_string : split_strings) for (const std::string& split_string : split_strings)
escaped_split_strings.push_back(EscapeFileName(split_string)); escaped_split_strings.push_back(EscapeFileName(split_string));
return JoinStrings(escaped_split_strings, "/"); return fmt::to_string(fmt::join(escaped_split_strings, "/"));
} }
std::string UnescapeFileName(const std::string& filename) std::string UnescapeFileName(const std::string& filename)

View File

@ -368,21 +368,6 @@ std::vector<std::string> SplitString(const std::string& str, const char delim)
return output; return output;
} }
std::string JoinStrings(const std::vector<std::string>& strings, const std::string& delimiter)
{
// Check if we can return early, just for speed
if (strings.empty())
return "";
std::ostringstream res;
std::copy(strings.begin(), strings.end(),
std::ostream_iterator<std::string>(res, delimiter.c_str()));
// Drop the trailing delimiter.
std::string joined = res.str();
return joined.substr(0, joined.length() - delimiter.length());
}
std::string TabsToSpaces(int tab_size, std::string str) std::string TabsToSpaces(int tab_size, std::string str)
{ {
const std::string spaces(tab_size, ' '); const std::string spaces(tab_size, ' ');

View File

@ -200,7 +200,6 @@ std::from_chars_result FromChars(std::string_view sv, T& value,
std::string TabsToSpaces(int tab_size, std::string str); std::string TabsToSpaces(int tab_size, std::string str);
std::vector<std::string> SplitString(const std::string& str, char delim); std::vector<std::string> SplitString(const std::string& str, char delim);
std::string JoinStrings(const std::vector<std::string>& strings, const std::string& delimiter);
// "C:/Windows/winhelp.exe" to "C:/Windows/", "winhelp", ".exe" // "C:/Windows/winhelp.exe" to "C:/Windows/", "winhelp", ".exe"
// This requires forward slashes to be used for the path separators, even on Windows. // This requires forward slashes to be used for the path separators, even on Windows.

View File

@ -14,6 +14,7 @@
#include <thread> #include <thread>
#include <fmt/format.h> #include <fmt/format.h>
#include <fmt/ranges.h>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/Intrinsics.h" #include "Common/Intrinsics.h"
@ -275,5 +276,5 @@ std::string CPUInfo::Summarize()
if (bSHA2) if (bSHA2)
sum.push_back("SHA2"); sum.push_back("SHA2");
return JoinStrings(sum, ","); return fmt::to_string(fmt::join(sum, ","));
} }

View File

@ -15,6 +15,8 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <fmt/ranges.h>
#include "Common/Align.h" #include "Common/Align.h"
#include "Common/CommonPaths.h" #include "Common/CommonPaths.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
@ -90,7 +92,7 @@ static std::vector<std::string> ReadM3UFile(const std::string& m3u_path,
if (!nonexistent.empty()) if (!nonexistent.empty())
{ {
PanicAlertFmtT("Files specified in the M3U file \"{0}\" were not found:\n{1}", m3u_path, PanicAlertFmtT("Files specified in the M3U file \"{0}\" were not found:\n{1}", m3u_path,
JoinStrings(nonexistent, "\n")); fmt::join(nonexistent, "\n"));
return {}; return {};
} }

View File

@ -3,6 +3,9 @@
#include "Core/FreeLookManager.h" #include "Core/FreeLookManager.h"
#include <fmt/format.h>
#include <fmt/ranges.h>
#include "Common/Common.h" #include "Common/Common.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/Config/Config.h" #include "Common/Config/Config.h"
@ -129,7 +132,7 @@ void FreeLookController::LoadDefaults(const ControllerInterface& ciface)
#ifndef ANDROID #ifndef ANDROID
auto hotkey_string = [](std::vector<std::string> inputs) { auto hotkey_string = [](std::vector<std::string> inputs) {
return "@(" + JoinStrings(inputs, "+") + ')'; return fmt::format("@({})", fmt::join(inputs, "+"));
}; };
m_move_buttons->SetControlExpression(MoveButtons::Up, hotkey_string({"Shift", "E"})); m_move_buttons->SetControlExpression(MoveButtons::Up, hotkey_string({"Shift", "E"}));

View File

@ -9,12 +9,12 @@
#include <vector> #include <vector>
#include <fmt/format.h> #include <fmt/format.h>
#include <fmt/ranges.h>
#include "Common/Common.h" #include "Common/Common.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/IniFile.h" #include "Common/IniFile.h"
#include "Common/StringUtil.h"
#include "InputCommon/ControllerEmu/Control/Input.h" #include "InputCommon/ControllerEmu/Control/Input.h"
#include "InputCommon/ControllerEmu/ControlGroup/Buttons.h" #include "InputCommon/ControllerEmu/ControlGroup/Buttons.h"
@ -442,7 +442,7 @@ void HotkeyManager::LoadDefaults(const ControllerInterface& ciface)
}; };
auto hotkey_string = [](std::vector<std::string> inputs) { auto hotkey_string = [](std::vector<std::string> inputs) {
return "@(" + JoinStrings(inputs, "+") + ')'; return fmt::format("@({})", fmt::join(inputs, "+"));
}; };
// General hotkeys // General hotkeys

View File

@ -7,6 +7,9 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <fmt/format.h>
#include <fmt/ranges.h>
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/IOFile.h" #include "Common/IOFile.h"
@ -423,7 +426,7 @@ std::string WFSSRVDevice::NormalizePath(const std::string& path) const
normalized_components.push_back(component); normalized_components.push_back(component);
} }
} }
return "/" + JoinStrings(normalized_components, "/"); return fmt::format("/{}", fmt::join(normalized_components, "/"));
} }
WFSSRVDevice::FileDescriptor* WFSSRVDevice::FindFileDescriptor(u16 fd) WFSSRVDevice::FileDescriptor* WFSSRVDevice::FindFileDescriptor(u16 fd)

View File

@ -7,6 +7,7 @@
#include <cmath> #include <cmath>
#include <fmt/format.h> #include <fmt/format.h>
#include <fmt/ranges.h>
#include "Common/Common.h" #include "Common/Common.h"
#include "Common/MathUtil.h" #include "Common/MathUtil.h"
@ -287,7 +288,7 @@ void ReshapableInput::SaveConfig(Common::IniFile::Section* section,
std::transform( std::transform(
m_calibration.begin(), m_calibration.end(), save_data.begin(), m_calibration.begin(), m_calibration.end(), save_data.begin(),
[](ControlState val) { return fmt::format("{:.2f}", val * CALIBRATION_CONFIG_SCALE); }); [](ControlState val) { return fmt::format("{:.2f}", val * CALIBRATION_CONFIG_SCALE); });
section->Set(group + CALIBRATION_CONFIG_NAME, JoinStrings(save_data, " "), ""); section->Set(group + CALIBRATION_CONFIG_NAME, fmt::to_string(fmt::join(save_data, " ")), "");
// Save center value. // Save center value.
static constexpr char center_format[] = "{:.2f} {:.2f}"; static constexpr char center_format[] = "{:.2f} {:.2f}";

View File

@ -9,6 +9,7 @@
#include <vector> #include <vector>
#include <fmt/format.h> #include <fmt/format.h>
#include <fmt/ranges.h>
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
#include "InputCommon/ControllerInterface/CoreDevice.h" #include "InputCommon/ControllerInterface/CoreDevice.h"
@ -96,12 +97,12 @@ BuildExpression(const std::vector<ciface::Core::DeviceContainer::InputDetection>
if (is_hotkey) if (is_hotkey)
{ {
alternations.push_back(fmt::format("@({})", JoinStrings(alternation, "+"))); alternations.push_back(fmt::format("@({})", fmt::join(alternation, "+")));
} }
else else
{ {
std::sort(alternation.begin(), alternation.end()); std::sort(alternation.begin(), alternation.end());
alternations.push_back(JoinStrings(alternation, "&")); alternations.push_back(fmt::to_string(fmt::join(alternation, "&")));
} }
}; };
@ -130,7 +131,7 @@ BuildExpression(const std::vector<ciface::Core::DeviceContainer::InputDetection>
std::sort(alternations.begin(), alternations.end()); std::sort(alternations.begin(), alternations.end());
alternations.erase(std::unique(alternations.begin(), alternations.end()), alternations.end()); alternations.erase(std::unique(alternations.begin(), alternations.end()), alternations.end());
return JoinStrings(alternations, "|"); return fmt::to_string(fmt::join(alternations, "|"));
} }
void RemoveSpuriousTriggerCombinations( void RemoveSpuriousTriggerCombinations(

View File

@ -7,15 +7,6 @@
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
TEST(StringUtil, JoinStrings)
{
EXPECT_EQ("", JoinStrings({}, ", "));
EXPECT_EQ("a", JoinStrings({"a"}, ","));
EXPECT_EQ("ab", JoinStrings({"a", "b"}, ""));
EXPECT_EQ("a, bb, c", JoinStrings({"a", "bb", "c"}, ", "));
EXPECT_EQ("???", JoinStrings({"?", "?"}, "?"));
}
TEST(StringUtil, StringPopBackIf) TEST(StringUtil, StringPopBackIf)
{ {
std::string abc = "abc"; std::string abc = "abc";