Skip to content

Commit

Permalink
Fix up converters and other constexpr utils
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Jan 24, 2025
1 parent 9d1e885 commit 039ffe0
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 182 deletions.
3 changes: 2 additions & 1 deletion include/Convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <string>
#include <string_view>

namespace OpenShock::Convert {
namespace OpenShock::Convert { // TODO: C++23 make this use std::from_chars instead
void FromInt8(int8_t val, std::string& str);
void FromUint8(uint8_t val, std::string& str);
void FromInt16(int16_t val, std::string& str);
Expand All @@ -26,6 +26,7 @@ namespace OpenShock::Convert {
bool ToUint32(std::string_view str, uint32_t& val);
bool ToInt64(std::string_view str, int64_t& val);
bool ToUint64(std::string_view str, uint64_t& val);
bool ToSizeT(std::string_view str, size_t& val);
bool ToBool(std::string_view str, bool& val);
bool ToGpioNum(std::string_view str, gpio_num_t& val);
} // namespace OpenShock::Convert
2 changes: 1 addition & 1 deletion include/radio/rmt/internal/Shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace OpenShock::Rmt::Internal {
template<size_t N, typename T>
constexpr void EncodeBits(rmt_data_t* sequence, T data, const rmt_data_t& rmtOne, const rmt_data_t& rmtZero)
{
static_assert(std::is_unsigned<T>::value, "T must be an unsigned integer");
static_assert(std::is_unsigned_v<T>, "T must be an unsigned integer");
static_assert(N > 0, "N must be greater than 0");
static_assert(N < std::numeric_limits<T>::digits, "N must be less or equal to the number of bits in T");

Expand Down
18 changes: 3 additions & 15 deletions include/util/DigitCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,16 @@

namespace OpenShock::Util {
template<typename T>
constexpr std::size_t Digits10CountMax()
{
static_assert(std::is_integral<T>::value);
uint64_t num = std::numeric_limits<T>::max();

std::size_t digits = std::is_signed<T>::value ? 2 : 1;
while (num >= 10) {
num /= 10;
digits++;
}

return digits;
}
inline constexpr int Digits10CountMax = std::numeric_limits<T>::digits10 + (std::is_signed_v<T> ? 2 : 1);

template<typename T>
constexpr std::size_t Digits10Count(T val)
{
static_assert(std::is_integral<T>::value);
static_assert(std::is_integral_v<T>);

std::size_t digits = 1;

if (std::is_signed<T>::value && val < 0) {
if constexpr (std::is_signed_v<T> && val < 0) {
digits++;
val = -val;
}
Expand Down
Loading

0 comments on commit 039ffe0

Please sign in to comment.