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 10df0c1 commit 39a972e
Show file tree
Hide file tree
Showing 18 changed files with 297 additions and 318 deletions.
19 changes: 9 additions & 10 deletions common/buffer_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,25 @@ namespace moon
}

template<typename T>
typename std::enable_if<
!std::is_same<T, bool>::value &&
!std::is_same<T, std::string>::value, T>::type
std::enable_if_t<
!std::is_same_v<T, bool> &&
!std::is_same_v<T, std::string>, T>
read()
{
static_assert(std::is_trivially_copyable<T>::value, "type T must be trivially copyable.");
return _read<T>();
}

template<typename T>
typename std::enable_if<
std::is_same<T, bool>::value, T>::type read()
std::enable_if_t<
std::is_same_v<T, bool>, T> read()
{
return (_read<uint8_t>() != 0) ? true : false;
}

template<typename T>
typename std::enable_if<
std::is_same<T, std::string>::value, T>::type read()
std::enable_if_t<
std::is_same_v<T, std::string>, T> read()
{
std::string tmp;
while (readpos_ < size_)
Expand All @@ -76,7 +76,7 @@ namespace moon

std::string to_string() const
{
return std::string((const char*)data(), size());
return std::string(data(), size());
}

template<class T>
Expand Down Expand Up @@ -141,8 +141,7 @@ namespace moon
std::string_view read_delim(char c)
{
std::string_view strref(data_ + readpos_, size());
size_t pos = strref.find(c);
if (pos != std::string_view::npos)
if (size_t pos = strref.find(c); pos != std::string_view::npos)
{
readpos_ += (pos + sizeof(c));
return std::string_view(strref.data(), pos);
Expand Down
4 changes: 2 additions & 2 deletions common/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace moon
logic_error& operator=(const logic_error& e) = default;
logic_error& operator=(logic_error&& e) = default;

virtual const char* what() const noexcept override {
const char* what() const noexcept override {
return w.c_str();
}
};
Expand All @@ -44,7 +44,7 @@ namespace moon
#define MOON_CHECK(cnd,msg) {if(!(cnd)) throw moon::logic_error{(msg),__FILENAME__,__LINE__};}

#ifdef DEBUG
#define MOON_ASSERT(cnd,msg) assert(cnd && msg);
#define MOON_ASSERT(cnd,msg) assert(cnd && msg)
#else
#define MOON_ASSERT(cnd,msg)
#endif
35 changes: 16 additions & 19 deletions common/http_utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
#include "string.hpp"
#include "buffer_view.hpp"

namespace moon
namespace moon::http
{
namespace http
{
using case_insensitive_multimap = std::unordered_multimap<std::string, std::string, moon::ihash_string_view_functor_t, moon::iequal_string_view_functor_t>;
using case_insensitive_multimap_view = std::unordered_multimap<std::string_view, std::string_view, moon::ihash_string_view_functor_t, moon::iequal_string_view_functor_t>;

Expand All @@ -22,7 +20,7 @@ namespace moon

for (const auto &chr : value) {
if (!((chr >= '0' && chr <= '9') || (chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z') || chr == '-' || chr == '.' || chr == '_' || chr == '~'))
result += std::string("%") + hex_chars[static_cast<unsigned char>(chr) >> 4] + hex_chars[static_cast<unsigned char>(chr) & 15];
result += std::string("%") + hex_chars[static_cast<uint8_t>(chr) >> 4] + hex_chars[static_cast<uint8_t>(chr) & 15];
else
result += chr;
}
Expand Down Expand Up @@ -61,8 +59,8 @@ namespace moon
std::string result;

bool first = true;
for (auto &field : fields) {
result += (!first ? "&" : "") + field.first + '=' + percent::encode(field.second);
for (const auto &[k,v] : fields) {
result += (!first ? "&" : "") + k + '=' + percent::encode(v);
first = false;
}

Expand All @@ -81,10 +79,9 @@ namespace moon
auto value_pos = std::string_view::npos;
for (std::size_t c = 0; c < query_string.size(); ++c) {
if (query_string[c] == '&') {
auto name = query_string.substr(name_pos, (name_end_pos == std::string_view::npos ? c : name_end_pos) - name_pos);
if (!name.empty()) {
if (auto name = query_string.substr(name_pos, (name_end_pos == std::string_view::npos ? c : name_end_pos) - name_pos); !name.empty()) {
auto value = value_pos == std::string_view::npos ? std::string_view{} : query_string.substr(value_pos, c - value_pos);
result.emplace(std::move(name), percent::decode(value));
result.emplace(name, percent::decode(value));
}
name_pos = c + 1;
name_end_pos = std::string_view::npos;
Expand All @@ -99,7 +96,7 @@ namespace moon
auto name = query_string.substr(name_pos, name_end_pos - name_pos);
if (!name.empty()) {
auto value = value_pos >= query_string.size() ? std::string_view{} : query_string.substr(value_pos);
result.emplace(std::move(name), percent::decode(value));
result.emplace(name, percent::decode(value));
}
}

Expand All @@ -116,8 +113,7 @@ namespace moon
std::string_view line = br.readline();
size_t param_end = 0;
while ((param_end = line.find(':')) != std::string_view::npos) {
size_t value_start = param_end + 1;
if (value_start < line.size()) {
if (size_t value_start = param_end + 1; value_start < line.size()) {
if (line[value_start] == ' ')
value_start++;
if (value_start < line.size())
Expand All @@ -138,8 +134,7 @@ namespace moon
buffer_view br(sv.data(), sv.size());
auto line = br.readline();

size_t method_end;
if ((method_end = line.find(' ')) != std::string_view::npos)
if (size_t method_end; (method_end = line.find(' ')) != std::string_view::npos)
{
method = line.substr(0, method_end);
size_t query_start = std::string_view::npos;
Expand All @@ -164,8 +159,7 @@ namespace moon
}
else
path = line.substr(method_end + 1, path_and_query_string_end - method_end - 1);
size_t protocol_end;
if ((protocol_end = line.find('/', path_and_query_string_end + 1)) != std::string_view::npos) {
if (size_t protocol_end; (protocol_end = line.find('/', path_and_query_string_end + 1)) != std::string_view::npos) {
if (line.compare(path_and_query_string_end + 1, protocol_end - path_and_query_string_end - 1, "HTTP") != 0)
return false;
http_version = line.substr(protocol_end + 1, line.size() - protocol_end - 1);
Expand All @@ -192,8 +186,12 @@ namespace moon
{
buffer_view br(sv.data(), sv.size());
auto line = br.readline();
std::size_t version_end;
if (!line.empty() && (version_end = line.find(' ')) != std::string_view::npos) {
if(line.empty()){
return false;
}


if (std::size_t version_end = line.find(' '); version_end != std::string_view::npos) {
if (5 < line.size())
version = line.substr(5, version_end - 5);
else
Expand All @@ -210,7 +208,6 @@ namespace moon
return true;
}
};
}

}

7 changes: 3 additions & 4 deletions common/lua_utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#define luaL_rawsetfield(L, tbindex, kname, valueexp)\
lua_pushliteral(L, kname);\
(valueexp);\
lua_rawset(L, tbindex);\
lua_rawset(L, tbindex)\

namespace moon
{
struct lua_scope_pop
{
lua_State* lua;
lua_scope_pop(lua_State* L)
explicit lua_scope_pop(lua_State* L)
:lua(L)
{
}
Expand Down Expand Up @@ -213,8 +213,7 @@ namespace moon
};

inline int traceback(lua_State* L) {
const char* msg = lua_tostring(L, 1);
if (msg)
if (const char* msg = lua_tostring(L, 1))
luaL_traceback(L, L, msg, 1);
else {
lua_pushliteral(L, "(no error message)");
Expand Down
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
44 changes: 22 additions & 22 deletions common/zset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ namespace moon
return *this;
}

bool operator!=(const skip_list_iterator_sentinel) const {
return node_ != nullptr;
friend bool operator!=(skip_list_iterator self, skip_list_iterator_sentinel) {
return self.node_ != nullptr;
}

bool operator==(const skip_list_iterator_sentinel) const {
return node_ == nullptr;
friend bool operator==(skip_list_iterator self, skip_list_iterator_sentinel) {
return self.node_ == nullptr;
}
};

Expand Down Expand Up @@ -161,7 +161,7 @@ namespace moon
friend class skip_list_iterator< skip_list>;
using const_iterator = skip_list_iterator< skip_list>;

skip_list(const allocator_type& alloc = allocator_type())
explicit skip_list(const allocator_type& alloc = allocator_type())
:alloc_(alloc)
, gen_(std::random_device{}())
{
Expand Down Expand Up @@ -409,42 +409,42 @@ namespace moon
int64_t score = 0;
int64_t timestamp = 0;

bool operator==(const context& val) const
friend bool operator==(const context& self, const context& val)
{
return key == val.key;
return self.key == val.key;
}

bool operator<(const context& val) const
friend bool operator<(const context& self, const context& val)
{
if (score == val.score)
if (self.score == val.score)
{
if (timestamp == val.timestamp)
if (self.timestamp == val.timestamp)
{
return key < val.key;
return self.key < val.key;
}
return timestamp < val.timestamp;
return self.timestamp < val.timestamp;
}
return score > val.score;
return self.score > val.score;
}

bool operator<=(const context& val) const
friend bool operator<=(const context& self, const context& val)
{
if (key == val.key)
if (self.key == val.key)
return true;
return operator<(val);
return operator<(self, val);
}

bool operator>(const context& val) const
friend bool operator>(const context& self, const context& val)
{
if (score == val.score)
if (self.score == val.score)
{
if (timestamp == val.timestamp)
if (self.timestamp == val.timestamp)
{
return key > val.key;
return self.key > val.key;
}
return timestamp > val.timestamp;
return self.timestamp > val.timestamp;
}
return score < val.score;
return self.score < val.score;
}
};
public:
Expand Down
Loading

0 comments on commit 39a972e

Please sign in to comment.