Merge pull request #11972 from Minty-Meeo/string-improvements-part-1c

Replace std::ostringstream usage with fmt::format
This commit is contained in:
Admiral H. Curtiss 2023-06-19 02:18:30 +02:00 committed by GitHub
commit 5d7b5822c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 49 deletions

View File

@ -6,6 +6,8 @@
#include <string> #include <string>
#include <fmt/format.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"
@ -159,9 +161,9 @@ void WaveFileWriter::AddStereoSamplesBE(const short* sample_data, u32 count,
{ {
Stop(); Stop();
file_index++; file_index++;
std::ostringstream filename; const std::string filename =
filename << File::GetUserPath(D_DUMPAUDIO_IDX) << basename << file_index << ".wav"; fmt::format("{}{}{}.wav", File::GetUserPath(D_DUMPAUDIO_IDX), basename, file_index);
Start(filename.str(), sample_rate_divisor); Start(filename, sample_rate_divisor);
current_sample_rate_divisor = sample_rate_divisor; current_sample_rate_divisor = sample_rate_divisor;
} }

View File

@ -7,6 +7,8 @@
#include <locale> #include <locale>
#include <sstream> #include <sstream>
#include <fmt/format.h>
namespace Common::Debug namespace Common::Debug
{ {
Watch::Watch(u32 address_, std::string name_, State is_enabled_) Watch::Watch(u32 address_, std::string name_, State is_enabled_)
@ -107,13 +109,9 @@ void Watches::LoadFromStrings(const std::vector<std::string>& watches)
std::vector<std::string> Watches::SaveToStrings() const std::vector<std::string> Watches::SaveToStrings() const
{ {
std::vector<std::string> watches; std::vector<std::string> watches;
watches.reserve(m_watches.size());
for (const auto& watch : m_watches) for (const auto& watch : m_watches)
{ watches.emplace_back(fmt::format("{:x} {}", watch.address, watch.name));
std::ostringstream ss;
ss.imbue(std::locale::classic());
ss << std::hex << watch.address << " " << watch.name;
watches.push_back(ss.str());
}
return watches; return watches;
} }

View File

@ -11,7 +11,6 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <span> #include <span>
#include <sstream>
#include <thread> #include <thread>
#include <tuple> #include <tuple>
#include <type_traits> #include <type_traits>
@ -535,10 +534,7 @@ void NetPlayClient::OnChatMessage(sf::Packet& packet)
INFO_LOG_FMT(NETPLAY, "Player {} ({}) wrote: {}", player.name, player.pid, msg); INFO_LOG_FMT(NETPLAY, "Player {} ({}) wrote: {}", player.name, player.pid, msg);
// add to gui // add to gui
std::ostringstream ss; m_dialog->AppendChat(fmt::format("{}[{}]: {}", player.name, pid, msg));
ss << player.name << '[' << char(pid + '0') << "]: " << msg;
m_dialog->AppendChat(ss.str());
} }
void NetPlayClient::OnChunkedDataStart(sf::Packet& packet) void NetPlayClient::OnChunkedDataStart(sf::Packet& packet)

View File

@ -162,15 +162,12 @@ void Device::AddCombinedInput(std::string name, const std::pair<std::string, std
std::string DeviceQualifier::ToString() const std::string DeviceQualifier::ToString() const
{ {
if (source.empty() && (cid < 0) && name.empty()) if (source.empty() && (cid < 0) && name.empty())
return ""; return {};
std::ostringstream ss;
ss << source << '/';
if (cid > -1) if (cid > -1)
ss << cid; return fmt::format("{}/{}/{}", source, cid, name);
ss << '/' << name; else
return fmt::format("{}//{}", source, name);
return ss.str();
} }
// //

View File

@ -7,9 +7,10 @@
#include <limits> #include <limits>
#include <mutex> #include <mutex>
#include <set> #include <set>
#include <sstream>
#include <type_traits> #include <type_traits>
#include <fmt/format.h>
#include "Common/HRWrap.h" #include "Common/HRWrap.h"
#include "Common/Logging/Log.h" #include "Common/Logging/Log.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h" #include "InputCommon/ControllerInterface/ControllerInterface.h"
@ -266,37 +267,21 @@ void Joystick::UpdateInput()
std::string Joystick::Button::GetName() const std::string Joystick::Button::GetName() const
{ {
std::ostringstream ss; return fmt::format("Button {}", m_index);
ss << "Button " << (int)m_index;
return ss.str();
} }
std::string Joystick::Axis::GetName() const std::string Joystick::Axis::GetName() const
{ {
std::ostringstream ss; const char sign = m_range < 0 ? '-' : '+';
// axis if (m_index < 6) // axis
if (m_index < 6) return fmt::format("Axis {:c}{}{:c}", 'X' + m_index % 3, m_index > 2 ? "r" : "", sign);
{ else // slider
ss << "Axis " << (char)('X' + (m_index % 3)); return fmt::format("Slider {}{:c}", m_index - 6, sign);
if (m_index > 2)
ss << 'r';
}
// slider
else
{
ss << "Slider " << (int)(m_index - 6);
}
ss << (m_range < 0 ? '-' : '+');
return ss.str();
} }
std::string Joystick::Hat::GetName() const std::string Joystick::Hat::GetName() const
{ {
char tmpstr[] = "Hat . ."; return fmt::format("Hat {} {:c}", m_index, "NESW"[m_direction]);
tmpstr[4] = (char)('0' + m_index);
tmpstr[6] = "NESW"[m_direction];
return tmpstr;
} }
// get / set state // get / set state

View File

@ -16,6 +16,8 @@
#include <wil/resource.h> #include <wil/resource.h>
#endif #endif
#include <fmt/format.h>
#include "Common/Common.h" #include "Common/Common.h"
#include "Common/CommonPaths.h" #include "Common/CommonPaths.h"
#include "Common/Config/Config.h" #include "Common/Config/Config.h"
@ -551,10 +553,8 @@ std::string FormatSize(u64 bytes, int decimals)
// Don't need exact values, only 5 most significant digits // Don't need exact values, only 5 most significant digits
const double unit_size = std::pow(2, unit * 10); const double unit_size = std::pow(2, unit * 10);
std::ostringstream ss; return fmt::format("{:.{}Lf} {}", bytes / unit_size, decimals,
ss << std::fixed << std::setprecision(decimals); Common::GetStringT(unit_symbols[unit]));
ss << bytes / unit_size << ' ' << Common::GetStringT(unit_symbols[unit]);
return ss.str();
} }
} // namespace UICommon } // namespace UICommon