Skip to content

Commit

Permalink
code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
sniper00 committed Aug 10, 2024
1 parent d7fb8e4 commit 3326989
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
38 changes: 27 additions & 11 deletions common/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ namespace moon
return r;
}

//format string
inline std::string format(const char* fmt, ...)
{
if (!fmt) return std::string("");
Expand Down Expand Up @@ -255,8 +254,7 @@ namespace moon
//" /t/n/r"
inline std::string_view trim_right(std::string_view v)
{
const auto words_end(v.find_last_not_of(" \t\n\r"));
if (words_end != std::string_view::npos) {
if (const auto words_end(v.find_last_not_of(" \t\n\r")); words_end != std::string_view::npos) {
v.remove_suffix(v.size() - words_end - 1);
}
return v;
Expand All @@ -271,8 +269,7 @@ namespace moon

inline std::string_view trim(std::string_view v)
{
const auto words_end(v.find_last_not_of(" \t\n\r"));
if (words_end != std::string_view::npos) {
if (const auto words_end(v.find_last_not_of(" \t\n\r")); words_end != std::string_view::npos) {
v.remove_suffix(v.size() - words_end - 1);
}
const auto words_begin(v.find_first_not_of(" \t\n\r"));
Expand All @@ -282,11 +279,14 @@ namespace moon

inline void replace(std::string& src, std::string_view old, std::string_view strnew)
{
for (std::string::size_type pos(0); pos != std::string::npos; pos += strnew.size()) {
if ((pos = src.find(old, pos)) != std::string::npos)
src.replace(pos, old.size(), strnew);
else
break;
if (old.empty()) {
return;
}

std::string::size_type pos = 0;
while ((pos = src.find(old, pos)) != std::string::npos) {
src.replace(pos, old.size(), strnew);
pos += strnew.size();
}
}

Expand Down Expand Up @@ -316,7 +316,7 @@ namespace moon
}

//! case insensitive
inline bool iequal_string_locale(const std::string&str1, const std::string& str2, const std::locale& Loc = std::locale())
inline bool iequal_string_locale(std::string_view str1, std::string_view str2, const std::locale& Loc = std::locale())
{
if (str1.size() != str2.size())
return false;
Expand Down Expand Up @@ -373,6 +373,22 @@ namespace moon
return res;
}

inline std::string escape_non_printable(std::string_view input) {
static constexpr std::string_view hex = "0123456789abcdef";
std::string res;
for (char ch : input) {
if (isprint(static_cast<unsigned char>(ch))) {
res.push_back(ch);
} else {
res.push_back('\\');
res.push_back('x');
res.push_back(hex[ch >> 4]);
res.push_back(hex[ch & 0xf]);
}
}
return res;
}

template<typename TString>
struct ihash_string_functor
{
Expand Down
4 changes: 2 additions & 2 deletions moon-src/core/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ namespace moon
CONSOLE_DEBUG("Dead service [%08X] recv message from [%08X]: %s.",
receiver,
sender,
moon::hex_string({ msg.data(),msg.size() }).data()
moon::escape_non_printable({ msg.data(),msg.size() }).data()
);
}else{
std::string str = moon::format("Attemp call dead service [%08X]: %s."
, receiver
, moon::hex_string({ msg.data(),msg.size() }).data());
, moon::escape_non_printable({ msg.data(),msg.size() }).data());
msg.set_sessionid(-msg.sessionid());
server_->response(sender, str, msg.sessionid(), PTYPE_ERROR);
}
Expand Down

0 comments on commit 3326989

Please sign in to comment.