Skip to content

Commit

Permalink
javascript wins this one
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishura4 committed Oct 5, 2023
1 parent 3f09d16 commit 85a1817
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
21 changes: 12 additions & 9 deletions include/dpp/snowflake.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class DPP_EXPORT snowflake final {
*/
template <typename T, typename = std::enable_if_t<std::is_same_v<T, std::string>>>
snowflake(const T &string_value) noexcept : snowflake(std::string_view{string_value}) {}
/* ^ this exists to preserve `message_cache.find(std::get<std::string>(event.get_parameter("message_id")));`*/
/* ^ this exists to preserve `message_cache.find(std::get<std::string>(event.get_parameter("message_id")));` */

/**
* @brief Copy value from another snowflake
Expand All @@ -124,14 +124,6 @@ class DPP_EXPORT snowflake final {
*/
constexpr dpp::snowflake &operator=(dpp::snowflake&& rhs) noexcept = default;

/**
* @brief Assign value converted from a string to the snowflake
*
* On invalid string the value will be 0
* @param snowflake_val snowflake value as a string
*/
dpp::snowflake &operator=(std::string_view snowflake_val) noexcept;

/**
* @brief Assign integer value to the snowflake
*
Expand All @@ -143,6 +135,17 @@ class DPP_EXPORT snowflake final {
return *this = dpp::snowflake{snowflake_val};
}

/**
* @brief Assign value converted from a string to the snowflake
*
* On invalid string the value will be 0
* @param snowflake_val snowflake value as a string
*/
template <typename T, typename = std::enable_if_t<std::is_convertible_v<T, std::string_view>>>
constexpr dpp::snowflake &operator=(T&& snowflake_val) noexcept {
return *this = dpp::snowflake{std::forward<T>(snowflake_val)};
}

/**
* @brief Returns true if the snowflake holds an empty value (is 0)
*
Expand Down
7 changes: 0 additions & 7 deletions src/dpp/snowflake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ snowflake::snowflake(std::string_view string_value) noexcept {
value = 0;
}

snowflake& snowflake::operator=(std::string_view string_value) noexcept {
auto [_, err] = std::from_chars(string_value.data(), string_value.data() + string_value.size(), value);
if (err != std::errc{})
value = 0;
return *this;
}

bool snowflake::operator==(std::string_view snowflake_val) const noexcept {
uint64_t v;
auto [end, err] = std::from_chars(snowflake_val.data(), snowflake_val.data() + snowflake_val.size(), v);
Expand Down
4 changes: 2 additions & 2 deletions src/unittest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
s = "69420";
success = success && s == 69420;
auto conversion_test = [](dpp::snowflake sl) {
return sl;
return sl.str();
};
s = conversion_test(std::string{"1337"});
success = success && s == 1337;
success = success && s == 1337; /* THIS BREAKS (and i do not care very much): && s == conversion_test(dpp::snowflake{"1337"}); */
success = success && dpp::snowflake{0} == 0;
set_test(SNOWFLAKE, success);
}
Expand Down

0 comments on commit 85a1817

Please sign in to comment.