refactor: move functions to string misc

This commit is contained in:
Horror Proton
2022-10-04 18:40:25 +08:00
parent f05e1ecf85
commit e5f56cb322
5 changed files with 135 additions and 118 deletions

View File

@@ -118,6 +118,7 @@
<ClInclude Include="Utils\AsstImageIo.hpp" />
<ClInclude Include="Utils\AsstInfrastDef.h" />
<ClInclude Include="Utils\AsstMsg.h" />
<ClInclude Include="Utils\StringMisc.hpp" />
<ClInclude Include="Utils\Platform\AsstPlatform.h" />
<ClInclude Include="Utils\Platform\AsstPlatformPosix.h" />
<ClInclude Include="Utils\Platform\AsstPlatformWin32.h" />

View File

@@ -653,6 +653,9 @@
<ClInclude Include="Utils\AsstTypes.h">
<Filter>源文件\Utils</Filter>
</ClInclude>
<ClInclude Include="Utils\StringMisc.hpp">
<Filter>源文件\Utils</Filter>
</ClInclude>
<ClInclude Include="Utils\Meta.hpp">
<Filter>源文件\Utils</Filter>
</ClInclude>

View File

@@ -3,6 +3,7 @@
#include "Resource/GeneralConfiger.h"
#include "Utils/AsstUtils.hpp"
#include "Utils/Logger.hpp"
#include "Utils/StringMisc.hpp"
#include <meojson/json.hpp>
@@ -112,7 +113,7 @@ void asst::ReportDataTask::report_to_penguin()
}
Log.info("Re-report to penguin-stats.cn");
utils::_string_replace_all(new_cmd_format, "https://penguin-stats.io", "https://penguin-stats.cn");
utils::string_replace_all_in_place(new_cmd_format, "https://penguin-stats.io", "https://penguin-stats.cn");
backoff = 10 * 1000; // 10s
response = report(
"ReportToPenguinStats", new_cmd_format,

View File

@@ -19,127 +19,13 @@
#include <sys/time.h>
#endif
#include "Meta.hpp"
#include "SingletonHolder.hpp"
#include "Meta.hpp"
#include "StringMisc.hpp"
namespace asst::utils
{
template <typename char_t = char>
using pair_of_string_view = std::pair<std::basic_string_view<char_t>, std::basic_string_view<char_t>>;
#ifdef ASST_USE_RANGES_RANGE_V3
// workaround for P2210R2
template <ranges::forward_range Rng>
requires(requires(Rng rng) { std::basic_string_view(std::addressof(*rng.begin()), ranges::distance(rng)); })
inline auto make_string_view(Rng rng)
{
return std::basic_string_view(std::addressof(*rng.begin()), ranges::distance(rng));
}
template <std::forward_iterator It, std::sized_sentinel_for<It> End>
requires(requires(It beg, End end) { std::basic_string_view(std::addressof(*beg), std::distance(beg, end)); })
inline auto make_string_view(It beg, End end)
{
return std::basic_string_view(std::addressof(*beg), std::distance(beg, end));
}
#else
template <ranges::contiguous_range Rng>
inline auto make_string_view(Rng rng)
{
return std::basic_string_view(rng.begin(), rng.end());
}
template <std::contiguous_iterator It, std::sized_sentinel_for<It> End>
requires(requires(It beg, End end) { std::basic_string_view(beg, end); })
inline auto make_string_view(It beg, End end)
{
return std::basic_string_view(beg, end);
}
#endif
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)
{
for (typename std::basic_string<char_t>::size_type pos(0); pos != str.npos; pos += new_value.length()) {
if ((pos = str.find(old_value, pos)) != str.npos)
str.replace(pos, old_value.length(), new_value);
else
break;
}
}
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 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)
{
std::basic_string<char_t> str = src;
_string_replace_all(str, { old_value }, { new_value });
return str;
}
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;
_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>
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);
}
return str;
}
inline void string_trim(std::string& s)
{
auto not_space = [](unsigned char c) { return !std::isspace(c); };
s.erase(ranges::find_if(s | views::reverse, not_space).base(), s.end());
s.erase(s.begin(), ranges::find_if(s, not_space));
}
template <ranges::input_range Rng>
requires std::convertible_to<ranges::range_value_t<Rng>, char>
void tolowers(Rng& rng)
{
ranges::transform(rng, rng.begin(), [](char c) -> char { return static_cast<char>(std::tolower(c)); });
}
inline std::string get_format_time()
{

View File

@@ -0,0 +1,126 @@
#pragma once
#include <initializer_list>
#include <iterator>
#include <locale>
#include <memory>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include "AsstRanges.hpp"
#include "Meta.hpp"
namespace asst::utils
{
namespace detail
{
template <typename StringT = std::string>
using sv_type = std::basic_string_view<typename std::remove_reference_t<StringT>::value_type,
typename std::remove_reference_t<StringT>::traits_type>;
template <typename StringT = std::string>
using sv_pair = std::pair<sv_type<StringT>, sv_type<StringT>>;
} // namespace detail
template <typename StringT = std::string>
inline constexpr void string_replace_all_in_place(StringT& str, detail::sv_type<StringT> from,
detail::sv_type<StringT> to)
{
using size_type = std::string_view::size_type;
for (size_type pos(0);; pos += to.length()) {
if ((pos = str.find(from, pos)) == StringT::npos) return;
str.replace(pos, from.length(), to);
}
}
template <typename StringT = std::string>
inline constexpr void string_replace_all_in_place(StringT& str, const detail::sv_pair<StringT>& replace_pair)
{
string_replace_all_in_place(str, replace_pair.first, replace_pair.second);
}
#ifdef ASST_USE_RANGES_RANGE_V3
// workaround for P2210R2
template <ranges::forward_range Rng>
requires(requires(Rng rng) { std::basic_string_view(std::addressof(*rng.begin()), ranges::distance(rng)); })
inline auto make_string_view(Rng rng)
{
return std::basic_string_view(std::addressof(*rng.begin()), ranges::distance(rng));
}
template <std::forward_iterator It, std::sized_sentinel_for<It> End>
requires(requires(It beg, End end) { std::basic_string_view(std::addressof(*beg), std::distance(beg, end)); })
inline auto make_string_view(It beg, End end)
{
return std::basic_string_view(std::addressof(*beg), std::distance(beg, end));
}
#else
template <ranges::contiguous_range Rng>
inline auto make_string_view(Rng rng)
{
return std::basic_string_view(rng.begin(), rng.end());
}
template <std::contiguous_iterator It, std::sized_sentinel_for<It> End>
requires(requires(It beg, End end) { std::basic_string_view(beg, end); })
inline auto make_string_view(It beg, End end)
{
return std::basic_string_view(beg, end);
}
#endif
template <typename StringT>
inline constexpr auto string_replace_all(StringT&& src, detail::sv_type<StringT> from, detail::sv_type<StringT> to)
{
std::basic_string result { std::forward<StringT>(src) };
string_replace_all_in_place(result, from, to);
return result;
}
template <typename StringT>
inline constexpr auto string_replace_all(StringT&& src, const detail::sv_pair<StringT>& replace_pair)
{
std::basic_string result { std::forward<StringT>(src) };
string_replace_all_in_place(result, replace_pair);
return result;
}
template <typename StringT>
inline constexpr auto string_replace_all(StringT&& src,
std::initializer_list<detail::sv_pair<StringT>> replace_pairs)
{
std::basic_string result { std::forward<StringT>(src) };
for (auto&& [from, to] : replace_pairs) {
string_replace_all_in_place(result, from, to);
}
return result;
}
template <typename StringT, typename MapT>
[[deprecated]] inline constexpr auto string_replace_all(StringT&& src, MapT&& replace_pairs)
{
std::basic_string result { std::forward<StringT>(src) };
for (auto&& [from, to] : replace_pairs) {
string_replace_all_in_place(result, from, to);
}
return result;
}
template <typename Pred = decltype([](unsigned char c) -> bool { return !std::isspace(c); })>
inline void string_trim(std::string& s, Pred not_space = Pred {})
{
s.erase(ranges::find_if(s | views::reverse, not_space).base(), s.end());
s.erase(s.begin(), ranges::find_if(s, not_space));
}
template <ranges::input_range Rng>
requires std::convertible_to<ranges::range_value_t<Rng>, char>
void tolowers(Rng& rng)
{
ranges::for_each(rng, [](char& c) -> void { c = static_cast<char>(std::tolower(c)); });
}
} // namespace asst::utils