perf: rename string_replace_all_batch -> string_replace_all

This commit is contained in:
zzyyyl
2022-09-03 21:02:14 +08:00
parent cb1a63dbf6
commit d4cb91bbda
6 changed files with 60 additions and 41 deletions

View File

@@ -68,10 +68,7 @@ asst::Rect asst::AbstractImageAnalyzer::empty_rect_to_full(const Rect& rect, con
bool asst::AbstractImageAnalyzer::save_img()
{
std::string stem = utils::get_format_time();
stem = utils::string_replace_all_batch(stem, {
{ ":", "-" },
{ " ", "_" },
});
stem = utils::string_replace_all(stem, { { ":", "-" }, { " ", "_" } });
std::filesystem::create_directory("debug");
bool ret = cv::imwrite("debug/" + stem + "_raw.png", m_image);

View File

@@ -191,7 +191,7 @@ void asst::AbstractTask::click_return_button()
void asst::AbstractTask::save_image()
{
std::string stem = utils::get_format_time();
stem = utils::string_replace_all_batch(stem, { { ":", "-" }, { " ", "_" } });
stem = utils::string_replace_all(stem, { { ":", "-" }, { " ", "_" } });
std::filesystem::create_directory("debug");
cv::imwrite("debug/" + stem + "_raw.png", m_ctrler->get_image());
}

View File

@@ -33,11 +33,14 @@ namespace asst::utils
template <typename dst_t, typename src_t>
requires ranges::range<src_t> && ranges::range<dst_t>
dst_t view_cast(const src_t& src)
dst_t view_cast(src_t&& src)
{
return dst_t(src.begin(), src.end());
}
template <typename char_t = char>
using pair_of_string_view = std::pair<std::basic_string_view<char_t>, std::basic_string_view<char_t>>;
template <typename char_t>
inline void _string_replace_all(std::basic_string<char_t>& str, std::basic_string_view<char_t> old_value,
std::basic_string_view<char_t> new_value)
@@ -51,8 +54,22 @@ namespace asst::utils
}
template <typename char_t, typename old_value_t, typename new_value_t>
requires std::is_constructible_v<std::basic_string_view<char_t>, old_value_t> &&
std::is_constructible_v<std::basic_string_view<char_t>, new_value_t>
requires std::convertible_to<old_value_t, std::basic_string_view<char_t>> &&
std::convertible_to<new_value_t, std::basic_string_view<char_t>>
inline void _string_replace_all(std::basic_string<char_t>& str, old_value_t&& old_value, new_value_t&& new_value)
{
_string_replace_all(str, { old_value }, { new_value });
}
template <typename char_t>
inline void _string_replace_all(std::basic_string<char_t>& str, const pair_of_string_view<char_t>& replace_pair)
{
_string_replace_all(str, replace_pair.first, replace_pair.second);
}
template <typename char_t, typename old_value_t, typename new_value_t>
requires std::convertible_to<old_value_t, std::basic_string_view<char_t>> &&
std::convertible_to<new_value_t, std::basic_string_view<char_t>>
inline std::basic_string<char_t> string_replace_all(const std::basic_string<char_t>& src, old_value_t&& old_value,
new_value_t&& new_value)
{
@@ -61,30 +78,35 @@ namespace asst::utils
return str;
}
template <typename char_t, typename old_value_t = std::basic_string_view<char_t>,
typename new_value_t = std::basic_string_view<char_t>>
requires std::is_constructible_v<std::basic_string_view<char_t>, old_value_t> &&
std::is_constructible_v<std::basic_string_view<char_t>, new_value_t>
inline std::basic_string<char_t> string_replace_all_batch(
const std::basic_string<char_t>& src,
std::initializer_list<std::pair<old_value_t, new_value_t>>&& replace_pairs)
template <typename char_t>
inline std::basic_string<char_t> string_replace_all(const std::basic_string<char_t>& src,
const pair_of_string_view<char_t>& replace_pair)
{
std::basic_string<char_t> str = src;
for (auto&& [old_value, new_value] : replace_pairs) {
_string_replace_all(str, { old_value }, { new_value });
_string_replace_all(str, replace_pair);
return str;
}
template <typename char_t>
inline std::basic_string<char_t> string_replace_all(
const std::basic_string<char_t>& src, std::initializer_list<pair_of_string_view<char_t>>&& replace_pairs)
{
std::basic_string<char_t> str = src;
for (const auto& [old_value, new_value] : replace_pairs) {
_string_replace_all(str, old_value, new_value);
}
return str;
}
template <typename char_t, typename map_t>
[[deprecated]] inline std::basic_string<char_t> string_replace_all_batch(const std::basic_string<char_t>& src,
const map_t& replace_pairs)
requires std::derived_from<typename map_t::value_type::first_type, std::basic_string<char_t>> &&
std::derived_from<typename map_t::value_type::second_type, std::basic_string<char_t>>
[[deprecated]] inline std::basic_string<char_t> string_replace_all(const std::basic_string<char_t>& src,
const map_t& replace_pairs)
{
std::basic_string<char_t> str = src;
for (const auto& [old_value, new_value] : replace_pairs) {
_string_replace_all(str, { old_value }, { new_value });
_string_replace_all(str, old_value, new_value);
}
return str;
}

View File

@@ -796,8 +796,8 @@ int asst::Controller::click_without_scale(const Point& p, bool block)
if (p.x < 0 || p.x >= m_width || p.y < 0 || p.y >= m_height) {
Log.error("click point out of range");
}
std::string cur_cmd = utils::string_replace_all_batch(
m_adb.click, { { "[x]", std::to_string(p.x) }, { "[y]", std::to_string(p.y) } });
std::string cur_cmd =
utils::string_replace_all(m_adb.click, { { "[x]", std::to_string(p.x) }, { "[y]", std::to_string(p.y) } });
int id = push_cmd(cur_cmd);
if (block) {
wait(id);
@@ -842,20 +842,20 @@ int asst::Controller::swipe_without_scale(const Point& p1, const Point& p2, int
y1 = std::clamp(y1, 0, m_height - 1);
}
std::string cur_cmd = utils::string_replace_all_batch(
m_adb.swipe, {
{ "[x1]", std::to_string(x1) },
{ "[y1]", std::to_string(y1) },
{ "[x2]", std::to_string(x2) },
{ "[y2]", std::to_string(y2) },
{ "[duration]", duration <= 0 ? "" : std::to_string(duration) },
});
std::string cur_cmd =
utils::string_replace_all(m_adb.swipe, {
{ "[x1]", std::to_string(x1) },
{ "[y1]", std::to_string(y1) },
{ "[x2]", std::to_string(x2) },
{ "[y2]", std::to_string(y2) },
{ "[duration]", duration <= 0 ? "" : std::to_string(duration) },
});
int id = 0;
// 额外的滑动adb有bug同样的参数偶尔会划得非常远。额外做一个短程滑动把之前的停下来
const auto& opt = Configer.get_options();
if (extra_swipe && opt.adb_extra_swipe_duration > 0) {
std::string extra_cmd = utils::string_replace_all_batch(
std::string extra_cmd = utils::string_replace_all(
m_adb.swipe, {
{ "[x1]", std::to_string(x2) },
{ "[y1]", std::to_string(y2) },
@@ -926,13 +926,13 @@ bool asst::Controller::connect(const std::string& adb_path, const std::string& a
// 里面的值每次执行命令后可能更新,所以要用 lambda 拿最新的
auto cmd_replace = [&](const std::string& cfg_cmd) -> std::string {
return utils::string_replace_all_batch(cfg_cmd, {
{ "[Adb]", adb_path },
{ "[AdbSerial]", address },
{ "[DisplayId]", display_id },
{ "[NcPort]", std::to_string(nc_port) },
{ "[NcAddress]", nc_address },
});
return utils::string_replace_all(cfg_cmd, {
{ "[Adb]", adb_path },
{ "[AdbSerial]", address },
{ "[DisplayId]", display_id },
{ "[NcPort]", std::to_string(nc_port) },
{ "[NcAddress]", nc_address },
});
};
/* connect */

View File

@@ -129,7 +129,7 @@ asst::http::Response asst::ReportDataTask::escape_and_request(const std::string&
{
LogTraceFunction;
std::string body_escape = utils::string_replace_all_batch(m_body, { { "\"", "\\\"" } });
std::string body_escape = utils::string_replace_all(m_body, { { "\"", "\\\"" } });
#ifdef _WIN32
std::string body_escapes = utils::utf8_to_unicode_escape(body_escape);
@@ -138,7 +138,7 @@ asst::http::Response asst::ReportDataTask::escape_and_request(const std::string&
#endif
std::string cmd_line =
utils::string_replace_all_batch(format, { { "[body]", body_escapes }, { "[extra]", m_extra_param } });
utils::string_replace_all(format, { { "[body]", body_escapes }, { "[extra]", m_extra_param } });
Log.info("request:\n", cmd_line);
http::Response response = utils::callcmd(cmd_line);

View File

@@ -20,7 +20,7 @@ bool asst::StageDropsImageAnalyzer::analyze()
#ifdef ASST_DEBUG
std::string stem = utils::get_format_time();
stem = utils::string_replace_all_batch(stem, { { ":", "-" }, { " ", "_" } });
stem = utils::string_replace_all(stem, { { ":", "-" }, { " ", "_" } });
std::filesystem::create_directory("debug");
cv::imwrite("debug/" + stem + "_raw.png", m_image);
#endif