fmt: add more string_view versions of string functions

This commit is contained in:
Megamouse
2025-12-08 19:49:42 +01:00
parent c840c98e9e
commit 27f39d2ac0
36 changed files with 413 additions and 90 deletions
+81 -1
View File
@@ -83,7 +83,8 @@ std::string fmt::win_error_to_string(unsigned long error, void* module_handle)
if (FormatMessageW((module_handle ? FORMAT_MESSAGE_FROM_HMODULE : FORMAT_MESSAGE_FROM_SYSTEM) | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
module_handle, error, 0, reinterpret_cast<LPWSTR>(&message_buffer), 0, nullptr))
{
message = fmt::format("%s (0x%x)", fmt::trim(wchar_to_utf8(message_buffer), " \t\n\r\f\v"), error);
const std::string utf8 = wchar_to_utf8(message_buffer);
message = fmt::format("%s (0x%x)", fmt::trim_sv(utf8, " \t\n\r\f\v"), error);
}
else
{
@@ -823,6 +824,50 @@ std::vector<std::string> fmt::split(std::string_view source, std::initializer_li
return result;
}
std::vector<std::string_view> fmt::split_sv(std::string_view source, std::initializer_list<std::string_view> separators, bool is_skip_empty)
{
std::vector<std::string_view> result;
for (usz index = 0; index < source.size();)
{
usz pos = -1;
usz sep_size = 0;
for (auto& separator : separators)
{
if (usz pos0 = source.find(separator, index); pos0 < pos)
{
pos = pos0;
sep_size = separator.size();
}
}
if (!sep_size)
{
result.emplace_back(&source[index], source.size() - index);
return result;
}
std::string_view piece = {&source[index], pos - index};
index = pos + sep_size;
if (piece.empty() && is_skip_empty)
{
continue;
}
result.emplace_back(std::move(piece));
}
if (result.empty() && !is_skip_empty)
{
result.emplace_back();
}
return result;
}
std::string fmt::trim(const std::string& source, std::string_view values)
{
const usz begin = source.find_first_not_of(values);
@@ -838,6 +883,21 @@ std::string fmt::trim(const std::string& source, std::string_view values)
return source.substr(begin, end + 1 - begin);
}
std::string_view fmt::trim_sv(std::string_view source, std::string_view values)
{
const usz begin = source.find_first_not_of(values);
if (begin == source.npos)
return {};
const usz end = source.find_last_not_of(values);
if (end == source.npos)
return source.substr(begin);
return source.substr(begin, end + 1 - begin);
}
std::string fmt::trim_front(const std::string& source, std::string_view values)
{
const usz begin = source.find_first_not_of(values);
@@ -848,12 +908,32 @@ std::string fmt::trim_front(const std::string& source, std::string_view values)
return source.substr(begin);
}
std::string_view fmt::trim_front_sv(std::string_view source, std::string_view values)
{
const usz begin = source.find_first_not_of(values);
if (begin == source.npos)
return {};
return source.substr(begin);
}
void fmt::trim_back(std::string& source, std::string_view values)
{
const usz index = source.find_last_not_of(values);
source.resize(index + 1);
}
std::string_view fmt::trim_back_sv(std::string_view source, std::string_view values)
{
const usz index = source.find_last_not_of(values);
if (index == std::string_view::npos)
return {};
source.remove_suffix(source.size() - (index + 1));
return source;
}
std::string fmt::to_upper(std::string_view string)
{
std::string result;
+40 -7
View File
@@ -139,57 +139,90 @@ namespace fmt
// Splits the string into a vector of strings using the separators. The vector may contain empty strings unless is_skip_empty is true.
std::vector<std::string> split(std::string_view source, std::initializer_list<std::string_view> separators, bool is_skip_empty = true);
// Splits the string_view into a vector of string_views using the separators. The vector may contain empty string_views unless is_skip_empty is true.
std::vector<std::string_view> split_sv(std::string_view source, std::initializer_list<std::string_view> separators, bool is_skip_empty = true);
// Removes all preceding and trailing characters specified by 'values' from 'source'.
std::string trim(const std::string& source, std::string_view values = " \t");
// Removes all preceding and trailing characters specified by 'values' from 'source' and returns the result.
std::string_view trim_sv(std::string_view source, std::string_view values = " \t");
// Removes all preceding characters specified by 'values' from 'source'.
std::string trim_front(const std::string& source, std::string_view values = " \t");
// Removes all preceding characters specified by 'values' from 'source' and returns the result.
std::string_view trim_front_sv(std::string_view source, std::string_view values = " \t");
// Removes all trailing characters specified by 'values' from 'source'.
void trim_back(std::string& source, std::string_view values = " \t");
// Removes all trailing characters specified by 'values' from 'source' and returns the result.
std::string_view trim_back_sv(std::string_view source, std::string_view values = " \t");
template <typename T>
std::string merge(const T& source, const std::string& separator)
std::string merge(const T& source, std::string_view separator)
{
if (source.empty())
{
return {};
}
usz total = (source.size() - 1) * separator.size();
for (const auto& s : source)
{
total += s.size();
}
std::string result;
result.reserve(total);
auto it = source.begin();
auto end = source.end();
for (--end; it != end; ++it)
{
result += std::string{*it} + separator;
result.append(*it);
if (!separator.empty())
result.append(separator);
}
return result + std::string{source.back()};
return result.append(source.back());
}
template <typename T>
std::string merge(std::initializer_list<T> sources, const std::string& separator)
std::string merge(std::initializer_list<T> sources, std::string_view separator)
{
if (!sources.size())
{
return {};
}
usz total = (sources.size() - 1) * separator.size();
for (const auto& s : sources)
{
if (s.empty()) continue;
total += s.size() + (s.size() - 1) * separator.size();
}
std::string result;
result.reserve(total);
bool first = true;
for (const auto& v : sources)
{
if (first)
{
result = fmt::merge(v, separator);
first = false;
}
else
else if (!separator.empty())
{
result += separator + fmt::merge(v, separator);
result.append(separator);
}
result.append(fmt::merge(v, separator));
}
return result;
+2 -2
View File
@@ -27,9 +27,9 @@ void fmt_class_string<cheat_type>::format(std::string& out, u64 arg)
});
}
bool cheat_info::from_str(const std::string& cheat_line)
bool cheat_info::from_str(std::string_view cheat_line)
{
auto cheat_vec = fmt::split(cheat_line, {"@@@"}, false);
const auto cheat_vec = fmt::split(cheat_line, {"@@@"}, false);
s64 val64 = 0;
if (cheat_vec.size() != 5 || !try_to_int64(&val64, cheat_vec[2], 0, cheat_type_max - 1))
+1 -1
View File
@@ -28,6 +28,6 @@ struct cheat_info
u32 offset{};
std::string red_script{};
bool from_str(const std::string& cheat_line);
bool from_str(std::string_view cheat_line);
std::string to_str() const;
};
+6 -6
View File
@@ -598,15 +598,15 @@ bool package_reader::read_param_sfo()
const bool is_psp = (entry.type & PKG_FILE_ENTRY_PSP) != 0u;
std::string name(entry.name_size + BUF_PADDING, '\0');
std::string name_buf(entry.name_size + BUF_PADDING, '\0');
if (usz read_size = decrypt(entry.name_offset, entry.name_size, is_psp ? PKG_AES_KEY2 : m_dec_key.data(), name.data()); read_size < entry.name_size)
if (usz read_size = decrypt(entry.name_offset, entry.name_size, is_psp ? PKG_AES_KEY2 : m_dec_key.data(), name_buf.data()); read_size < entry.name_size)
{
pkg_log.error("PKG name could not be read (size=0x%x, offset=0x%x)", entry.name_size, entry.name_offset);
continue;
}
fmt::trim_back(name, "\0"sv);
std::string_view name = fmt::trim_back_sv(name_buf, "\0"sv);
// We're looking for the PARAM.SFO file, if there is any
if (usz ndelim = name.find_first_not_of('/'); ndelim == umax || name.substr(ndelim) != "PARAM.SFO")
@@ -854,18 +854,18 @@ bool package_reader::fill_data(std::map<std::string, install_entry*>& all_instal
break;
}
std::string name(entry.name_size + BUF_PADDING, '\0');
std::string name_buf(entry.name_size + BUF_PADDING, '\0');
const bool is_psp = (entry.type & PKG_FILE_ENTRY_PSP) != 0u;
if (const usz read_size = decrypt(entry.name_offset, entry.name_size, is_psp ? PKG_AES_KEY2 : m_dec_key.data(), name.data()); read_size < entry.name_size)
if (const usz read_size = decrypt(entry.name_offset, entry.name_size, is_psp ? PKG_AES_KEY2 : m_dec_key.data(), name_buf.data()); read_size < entry.name_size)
{
num_failures++;
pkg_log.error("PKG name could not be read (size=0x%x, offset=0x%x)", entry.name_size, entry.name_offset);
break;
}
fmt::trim_back(name, "\0"sv);
std::string_view name = fmt::trim_back_sv(name_buf, "\0"sv);
std::string path = m_install_path + vfs::escape(name);
+4 -4
View File
@@ -7320,7 +7320,7 @@ spu_program spu_recompiler_base::analyse(const be_t<u32>* ls, u32 entry_point, s
}
// spu_log.success("PUTLLC0 Pattern Detected! (put_pc=0x%x, %s) (putllc0=%d, putllc16+0=%d, all=%d)", pattern.put_pc, func_hash, ++stats.nowrite, ++stats.single, +stats.all);
// add_pattern(false, inst_attr::putllc0, pattern.put_pc - lsa, value.data);
// add_pattern(inst_attr::putllc0, pattern.put_pc - lsa, value.data);
continue;
}
@@ -7411,7 +7411,7 @@ spu_program spu_recompiler_base::analyse(const be_t<u32>* ls, u32 entry_point, s
if (allow_pattern)
{
add_pattern(false, inst_attr::putllc16, pattern.put_pc - result.entry_point, value.data);
add_pattern(inst_attr::putllc16, pattern.put_pc - result.entry_point, value.data);
}
spu_log.success("PUTLLC16 Pattern Detected! (mem_count=%d, put_pc=0x%x, pc_rel=%d, offset=0x%x, const=%u, two_regs=%d, reg=%u, runtime=%d, 0x%x-%s, pattern-hash=%s) (putllc0=%d, putllc16+0=%d, all=%d)"
@@ -7433,7 +7433,7 @@ spu_program spu_recompiler_base::analyse(const be_t<u32>* ls, u32 entry_point, s
if (inst_attr attr = m_inst_attrs[(read_pc - entry_point) / 4]; attr == inst_attr::none)
{
add_pattern(false, inst_attr::rchcnt_loop, read_pc - result.entry_point, 0);
add_pattern(inst_attr::rchcnt_loop, read_pc - result.entry_point, 0);
spu_log.error("Channel Loop Pattern Detected! Report to developers! (read_pc=0x%x, branch_pc=0x%x, branch_target=0x%x, 0x%x-%s)", read_pc, pattern.branch_pc, pattern.branch_target, entry_point, func_hash);
}
@@ -8519,7 +8519,7 @@ std::array<reg_state_t, s_reg_max>& block_reg_info::evaluate_start_state(const s
return walkby_state;
}
void spu_recompiler_base::add_pattern(bool fill_all, inst_attr attr, u32 start, u64 info)
void spu_recompiler_base::add_pattern(inst_attr attr, u32 start, u64 info)
{
m_patterns[start] = pattern_info{info};
m_inst_attrs[start / 4] = attr;
+1 -1
View File
@@ -402,7 +402,7 @@ protected:
std::unordered_map<u32, pattern_info> m_patterns;
void add_pattern(bool fill_all, inst_attr attr, u32 start, u64 info);
void add_pattern(inst_attr attr, u32 start, u64 info);
private:
// For private use
+3 -2
View File
@@ -321,7 +321,7 @@ static void LIBUSB_CALL log_cb(libusb_context* /*ctx*/, enum libusb_log_level le
if (!str)
return;
const std::string msg = fmt::trim(str, " \t\n");
const std::string_view msg = fmt::trim_sv(str, " \t\n");
switch (level)
{
@@ -555,7 +555,8 @@ usb_handler_thread::usb_handler_thread()
usb_devices.push_back(std::make_shared<usb_device_vfs>(usb_info, get_new_location()));
}
const std::vector<std::string> devices_list = fmt::split(g_cfg.io.midi_devices.to_string(), { "@@@" });
const std::string midi_devices = g_cfg.io.midi_devices.to_string();
const std::vector<std::string_view> devices_list = fmt::split_sv(midi_devices, { "@@@" });
for (usz index = 0; index < std::min(max_midi_devices, devices_list.size()); index++)
{
const midi_device device = midi_device::from_string(::at32(devices_list, index));
+1 -1
View File
@@ -205,7 +205,7 @@ protected:
std::set<T> key_codes;
const std::string& def = cfg_string.def;
const std::vector<std::string> names = cfg_pad::get_buttons(cfg_string);
const std::vector<std::string> names = cfg_pad::get_buttons(cfg_string.to_string());
T def_code = umax;
for (const std::string& nam : names)
+5 -4
View File
@@ -177,7 +177,7 @@ Note str_to_note(const std::string_view name)
std::optional<std::pair<Id, Note>> parse_midi_override(const std::string_view config)
{
auto split = fmt::split(config, {"="});
const auto split = fmt::split_sv(config, {"="});
if (split.size() != 2)
{
return {};
@@ -236,8 +236,9 @@ std::unordered_map<Id, Note> create_id_to_note_mapping()
};
// Apply configured overrides.
const std::vector<std::string> segments = fmt::split(g_cfg_rb3drums.midi_overrides.to_string(), {","});
for (const std::string& segment : segments)
const std::string midi_overrides = g_cfg_rb3drums.midi_overrides.to_string();
const std::vector<std::string_view> segments = fmt::split_sv(midi_overrides, {","});
for (const std::string_view& segment : segments)
{
if (const auto midi_override = parse_midi_override(segment))
{
@@ -259,7 +260,7 @@ std::vector<u8> parse_combo(const std::string_view name, const std::string_view
return {};
}
std::vector<u8> notes;
const auto& note_names = fmt::split(csv, {","});
const auto note_names = fmt::split_sv(csv, {","});
for (const auto& note_name : note_names)
{
const auto note = str_to_note(note_name);
+2 -2
View File
@@ -36,7 +36,7 @@ void cfg_camera::save() const
}
}
cfg_camera::camera_setting cfg_camera::get_camera_setting(const std::string& camera, bool& success)
cfg_camera::camera_setting cfg_camera::get_camera_setting(std::string_view camera, bool& success)
{
camera_setting setting;
const std::string value = cameras.get_value(camera);
@@ -64,7 +64,7 @@ std::string cfg_camera::camera_setting::to_string() const
return fmt::format("%d,%d,%f,%f,%d", width, height, min_fps, max_fps, format);
}
void cfg_camera::camera_setting::from_string(const std::string& text)
void cfg_camera::camera_setting::from_string(std::string_view text)
{
if (text.empty())
{
+2 -2
View File
@@ -19,9 +19,9 @@ struct cfg_camera final : cfg::node
static constexpr u32 member_count = 5;
std::string to_string() const;
void from_string(const std::string& text);
void from_string(std::string_view text);
};
camera_setting get_camera_setting(const std::string& camera, bool& success);
camera_setting get_camera_setting(std::string_view camera, bool& success);
void set_camera_setting(const std::string& camera, const camera_setting& setting);
const std::string path;
+2 -2
View File
@@ -27,11 +27,11 @@ void fmt_class_string<midi_device>::format(std::string& out, u64 arg)
fmt::append(out, "%sßßß%s", obj.type, obj.name);
}
midi_device midi_device::from_string(const std::string& str)
midi_device midi_device::from_string(std::string_view str)
{
midi_device res{};
if (const std::vector<std::string> parts = fmt::split(str, {"ßßß"}); !parts.empty())
if (const std::vector<std::string_view> parts = fmt::split_sv(str, {"ßßß"}); !parts.empty())
{
u64 result;
+2 -1
View File
@@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <string_view>
static constexpr usz max_midi_devices = 3;
@@ -17,5 +18,5 @@ struct midi_device
midi_device_type type{};
std::string name;
static midi_device from_string(const std::string& str);
static midi_device from_string(std::string_view str);
};
+1 -1
View File
@@ -5,7 +5,7 @@
extern std::string g_input_config_override;
std::vector<std::string> cfg_pad::get_buttons(const std::string& str)
std::vector<std::string> cfg_pad::get_buttons(std::string_view str)
{
std::vector<std::string> vec = fmt::split(str, {","});
+1 -1
View File
@@ -25,7 +25,7 @@ struct cfg_pad final : cfg::node
cfg_pad() {};
cfg_pad(node* owner, const std::string& name) : cfg::node(owner, name) {}
static std::vector<std::string> get_buttons(const std::string& str);
static std::vector<std::string> get_buttons(std::string_view str);
static std::string get_buttons(std::vector<std::string> vec);
u8 get_motor_speed(VibrateMotor& motor, f32 multiplier) const;
+2 -1
View File
@@ -28,7 +28,8 @@ namespace np
dnshook::dnshook()
{
// Init switch map for dns
auto swaps = fmt::split(g_cfg.net.swap_list.to_string(), {"&&"});
const std::string swap_list = g_cfg.net.swap_list.to_string();
const auto swaps = fmt::split_sv(swap_list, {"&&"});
for (usz i = 0; i < swaps.size(); i++)
{
auto host_and_ip = fmt::split(swaps[i], {"="});
+2 -1
View File
@@ -79,7 +79,8 @@ std::string cfg_rpcn::get_host() const
std::vector<std::pair<std::string, std::string>> cfg_rpcn::get_hosts()
{
std::vector<std::pair<std::string, std::string>> vec_hosts;
auto hosts_list = fmt::split(hosts.to_string(), {"|||"});
const std::string host_str = hosts.to_string();
const auto hosts_list = fmt::split_sv(host_str, {"|||"});
for (const auto& cur_host : hosts_list)
{
+4 -4
View File
@@ -244,10 +244,10 @@ public:
std::vector<std::string> swizzles;
ShaderVariable() = default;
ShaderVariable(const std::string& var)
ShaderVariable(std::string_view var)
{
// Separate 'double destination' variables 'X=Y=SRC'
std::string simple_var;
std::string_view simple_var;
const auto eq_pos = var.find('=');
if (eq_pos != umax)
@@ -267,11 +267,11 @@ public:
simple_var = simple_var.substr(brace_pos);
}
auto var_blocks = fmt::split(simple_var, { "." });
const auto var_blocks = fmt::split_sv(simple_var, { "." });
ensure((!var_blocks.empty()));
name = prefix + var_blocks[0];
name = prefix + std::string(var_blocks[0]);
if (var_blocks.size() == 1)
{
+9 -9
View File
@@ -1866,8 +1866,8 @@ game_boot_result Emulator::Load(const std::string& title_id, bool is_disc_patch,
// PS1 Classic located in dev_hdd0/game
sys_log.notice("PS1 Game: %s, %s", m_title_id, m_title);
const std::string tail = m_path.substr(hdd0_game.size());
const std::string dirname = fmt::trim_front(tail, fs::delim).substr(0, tail.find_first_of(fs::delim));
const std::string_view tail = std::string_view(m_path).substr(hdd0_game.size());
const std::string dirname = std::string(fmt::trim_front_sv(tail, fs::delim).substr(0, tail.find_first_of(fs::delim)));
const std::string game_path = "/dev_hdd0/game/" + dirname;
argv.resize(9);
@@ -1894,8 +1894,8 @@ game_boot_result Emulator::Load(const std::string& title_id, bool is_disc_patch,
// PSP Remaster located in dev_hdd0/game
sys_log.notice("PSP Remaster Game: %s, %s", m_title_id, m_title);
const std::string tail = m_path.substr(hdd0_game.size());
const std::string dirname = fmt::trim_front(tail, fs::delim).substr(0, tail.find_first_of(fs::delim));
const std::string_view tail = std::string_view(m_path).substr(hdd0_game.size());
const std::string dirname = std::string(fmt::trim_front_sv(tail, fs::delim).substr(0, tail.find_first_of(fs::delim)));
const std::string game_path = "/dev_hdd0/game/" + dirname;
argv.resize(2);
@@ -1913,7 +1913,7 @@ game_boot_result Emulator::Load(const std::string& title_id, bool is_disc_patch,
// Add HG games not in HDD0 to games.yml
[[maybe_unused]] const games_config::result res = m_games_config.add_external_hdd_game(m_title_id, game_dir);
const std::string dir = fmt::trim(game_dir.substr(fs::get_parent_dir_view(game_dir).size() + 1), fs::delim);
const std::string dir = std::string(fmt::trim_sv(std::string_view(game_dir).substr(fs::get_parent_dir_view(game_dir).size() + 1), fs::delim));
vfs::mount("/dev_hdd0/game/" + dir, game_dir + '/');
}
}
@@ -2265,10 +2265,10 @@ game_boot_result Emulator::Load(const std::string& title_id, bool is_disc_patch,
auto unescape = [](std::string_view path)
{
// Unescape from host FS
std::vector<std::string> escaped = fmt::split(path, {std::string_view{&fs::delim[0], 1}, std::string_view{&fs::delim[1], 1}});
const std::vector<std::string_view> escaped = fmt::split_sv(path, {std::string_view{&fs::delim[0], 1}, std::string_view{&fs::delim[1], 1}});
std::vector<std::string> result;
for (auto& sv : escaped)
result.emplace_back(vfs::unescape(sv));
for (const auto& sv : escaped)
result.push_back(vfs::unescape(sv));
return fmt::merge(result, "/");
};
@@ -2315,7 +2315,7 @@ game_boot_result Emulator::Load(const std::string& title_id, bool is_disc_patch,
game_dir = game_dir.substr(0, game_dir.size() - 4);
}
const std::string dir = fmt::trim(game_dir.substr(fs::get_parent_dir_view(game_dir).size() + 1), fs::delim);
const std::string dir = std::string(fmt::trim_sv(std::string_view(game_dir).substr(fs::get_parent_dir_view(game_dir).size() + 1), fs::delim));
m_dir = "/dev_hdd0/game/" + dir + '/';
argv[0] = m_dir + unescape(resolved_path.substr(GetCallbacks().resolve_path(game_dir).size()));

Some files were not shown because too many files have changed in this diff Show More