Compare commits

7 Commits
Author SHA1 Message Date
feedc0de 96e318fd6d Fix compilation under linux 2023-03-21 17:37:16 +01:00
feedc0de 4d0c488701 Fix unit tests 2023-03-08 14:36:47 +01:00
feedc0de 90e31b7cf8 Updated to new esp-idf 2023-02-27 17:26:48 +01:00
feedc0de 3ce6e3cb13 Add is_std_array 2023-01-25 18:11:04 +01:00
feedc0de f64741d006 Add toString utils also for int and unsigned int 2022-12-19 14:09:50 +01:00
feedc0de 3ab7a6c5ef New compiler new printf chars 2022-09-17 22:12:47 +02:00
feedc0de 9fbd305dd9 Add base64_decode 2022-09-07 17:54:00 +02:00
9 changed files with 118 additions and 64 deletions
-1
View File
@@ -24,7 +24,6 @@ set(sources
)
set(dependencies
expected
fmt
)
+2 -2
View File
@@ -12,7 +12,7 @@ std::string toString(ColorHelper color)
return fmt::format("#{:02X}{:02X}{:02X}", color.r, color.g, color.b);
}
tl::expected<ColorHelper, std::string> parseColor(std::string_view str)
std::expected<ColorHelper, std::string> parseColor(std::string_view str)
{
// input may be "#FFF" or "#FFFFFF" or "#FFFFFFFF"
@@ -27,7 +27,7 @@ tl::expected<ColorHelper, std::string> parseColor(std::string_view str)
return helper;
}
return tl::make_unexpected(fmt::format("invalid color {}", str));
return std::unexpected(fmt::format("invalid color {}", str));
}
} // namespace cpputils
+2 -4
View File
@@ -4,9 +4,7 @@
#include <string>
#include <string_view>
#include <array>
// 3rdparty lib includes
#include <tl/expected.hpp>
#include <expected>
namespace cpputils {
struct ColorHelper
@@ -53,7 +51,7 @@ struct HsvColor
};
std::string toString(ColorHelper color);
tl::expected<ColorHelper, std::string> parseColor(std::string_view str);
std::expected<ColorHelper, std::string> parseColor(std::string_view str);
inline uint32_t colorToNumber(ColorHelper color)
{
+3 -3
View File
@@ -2,9 +2,9 @@
// system includes
#include <string>
#include <expected>
// 3rdparty lib includes
#include <tl/expected.hpp>
#include <fmt/core.h>
// local includes
@@ -32,12 +32,12 @@
} \
return fmt::format("Unknown " #Name "({})", int(value)); \
} \
inline tl::expected<Name, std::string> parse##Name(std::string_view str) \
inline std::expected<Name, std::string> parse##Name(std::string_view str) \
{ \
using TheEnum = Name; \
if (false) {} \
Values(DECLARE_TYPESAFE_ENUM_HELPER3) \
return tl::make_unexpected(fmt::format("invalid " #Name " ({})", str)); \
return std::unexpected(fmt::format("invalid " #Name " ({})", str)); \
} \
template<typename T> \
void iterate##Name(T &&cb) \
+14 -1
View File
@@ -5,6 +5,7 @@
#include <type_traits>
#include <algorithm>
#include <iterator>
#include <array>
namespace cpputils {
namespace literals {
@@ -177,9 +178,21 @@ void parallelForeach(T0 &container0, T1 &container1, T2 &container2, T3 &contain
callback(*iter0, *iter1, *iter2, *iter3);
}
template<typename Test, template<typename...> class Ref>
template<typename, template<typename...> class Ref>
struct is_specialization : std::false_type {};
template<template<typename...> class Ref, typename... Args>
struct is_specialization<Ref<Args...>, Ref> : std::true_type {};
template<typename ...T>
constexpr bool is_specialization_v = is_specialization<T...>::value;
template<class T>
struct is_std_array : std::false_type {};
template<class T, size_t N>
struct is_std_array<std::array<T, N>> : std::true_type {};
template<class T>
constexpr bool is_std_array_v = is_std_array<std::remove_cvref_t<T>>::value;
} // namespace cpputils
+24 -24
View File
@@ -5,93 +5,93 @@
#include <cstdio>
#include <inttypes.h>
#include <string>
#include <expected>
// 3rdparty lib includes
#include <tl/expected.hpp>
#include <fmt/format.h>
namespace cpputils {
template<typename T> tl::expected<T, std::string> fromString(std::string_view str) = delete;
template<typename T> std::expected<T, std::string> fromString(std::string_view str) = delete;
template<> inline tl::expected<int8_t, std::string> fromString<int8_t>(std::string_view str)
template<> inline std::expected<int8_t, std::string> fromString<int8_t>(std::string_view str)
{
int8_t val;
if (std::sscanf(str.data(), "%" SCNi8, &val) != 1)
return tl::make_unexpected(fmt::format("invalid int8_t {}", str));
return std::unexpected(fmt::format("invalid int8_t {}", str));
return val;
}
template<> inline tl::expected<uint8_t, std::string> fromString<uint8_t>(std::string_view str)
template<> inline std::expected<uint8_t, std::string> fromString<uint8_t>(std::string_view str)
{
uint8_t val;
if (std::sscanf(str.data(), "%" SCNu8, &val) != 1)
return tl::make_unexpected(fmt::format("invalid uint8_t {}", str));
return std::unexpected(fmt::format("invalid uint8_t {}", str));
return val;
}
template<> inline tl::expected<int16_t, std::string> fromString<int16_t>(std::string_view str)
template<> inline std::expected<int16_t, std::string> fromString<int16_t>(std::string_view str)
{
int16_t val;
if (std::sscanf(str.data(), "%" SCNi16, &val) != 1)
return tl::make_unexpected(fmt::format("invalid int16_t {}", str));
return std::unexpected(fmt::format("invalid int16_t {}", str));
return val;
}
template<> inline tl::expected<uint16_t, std::string> fromString<uint16_t>(std::string_view str)
template<> inline std::expected<uint16_t, std::string> fromString<uint16_t>(std::string_view str)
{
uint16_t val;
if (std::sscanf(str.data(), "%" SCNu16, &val) != 1)
return tl::make_unexpected(fmt::format("invalid uint16_t {}", str));
return std::unexpected(fmt::format("invalid uint16_t {}", str));
return val;
}
template<> inline tl::expected<int32_t, std::string> fromString<int32_t>(std::string_view str)
template<> inline std::expected<int32_t, std::string> fromString<int32_t>(std::string_view str)
{
int32_t val;
if (std::sscanf(str.data(), "%" SCNi32, &val) != 1)
return tl::make_unexpected(fmt::format("invalid int32_t {}", str));
return std::unexpected(fmt::format("invalid int32_t {}", str));
return val;
}
template<> inline tl::expected<uint32_t, std::string> fromString<uint32_t>(std::string_view str)
template<> inline std::expected<uint32_t, std::string> fromString<uint32_t>(std::string_view str)
{
uint32_t val;
if (std::sscanf(str.data(), "%" SCNu32, &val) != 1)
return tl::make_unexpected(fmt::format("invalid uint32_t {}", str));
return std::unexpected(fmt::format("invalid uint32_t {}", str));
return val;
}
#ifdef ESP_PLATFORM
template<> inline tl::expected<int, std::string> fromString<int>(std::string_view str)
template<> inline std::expected<int, std::string> fromString<int>(std::string_view str)
{
int val;
if (std::sscanf(str.data(), "%" SCNi32, &val) != 1)
return tl::make_unexpected(fmt::format("invalid int {}", str));
if (std::sscanf(str.data(), "%i", &val) != 1)
return std::unexpected(fmt::format("invalid int {}", str));
return val;
}
template<> inline tl::expected<unsigned int, std::string> fromString<unsigned int>(std::string_view str)
template<> inline std::expected<unsigned int, std::string> fromString<unsigned int>(std::string_view str)
{
unsigned int val;
if (std::sscanf(str.data(), "%" SCNu32, &val) != 1)
return tl::make_unexpected(fmt::format("invalid unsigned int {}", str));
if (std::sscanf(str.data(), "%u", &val) != 1)
return std::unexpected(fmt::format("invalid unsigned int {}", str));
return val;
}
#endif
template<> inline tl::expected<int64_t, std::string> fromString<int64_t>(std::string_view str)
template<> inline std::expected<int64_t, std::string> fromString<int64_t>(std::string_view str)
{
int64_t val;
if (std::sscanf(str.data(), "%" SCNi64, &val) != 1)
return tl::make_unexpected(fmt::format("invalid int64_t {}", str));
return std::unexpected(fmt::format("invalid int64_t {}", str));
return val;
}
template<> inline tl::expected<uint64_t, std::string> fromString<uint64_t>(std::string_view str)
template<> inline std::expected<uint64_t, std::string> fromString<uint64_t>(std::string_view str)
{
uint64_t val;
if (std::sscanf(str.data(), "%" SCNu64, &val) != 1)
return tl::make_unexpected(fmt::format("invalid uint64_t {}", str));
return std::unexpected(fmt::format("invalid uint64_t {}", str));
return val;
}
} // namespace cpputils
+59 -7
View File
@@ -9,7 +9,7 @@
namespace cpputils {
namespace {
tl::expected<char *, std::string> sodium_bin2hex(char * const hex, const size_t hex_maxlen,
std::expected<char *, std::string> sodium_bin2hex(char * const hex, const size_t hex_maxlen,
const unsigned char * const bin, const size_t bin_len)
__attribute__ ((nonnull(1)));
} // namespace
@@ -32,10 +32,10 @@ std::string toHexString(std::basic_string_view<unsigned char> buf)
return hex;
}
tl::expected<std::basic_string<unsigned char>, std::string> fromHexString(std::string_view hex)
std::expected<std::basic_string<unsigned char>, std::string> fromHexString(std::string_view hex)
{
if (hex.size() % 2 != 0)
return tl::make_unexpected("hex length not even");
return std::unexpected("hex length not even");
std::basic_string<unsigned char> result;
result.reserve(hex.size() / 2);
@@ -56,7 +56,7 @@ tl::expected<std::basic_string<unsigned char>, std::string> fromHexString(std::s
case 'A'...'F': nibbles.nibble0 = c - 'A' + 10; break;
case 'a'...'f': nibbles.nibble0 = c - 'a' + 10; break;
default:
return tl::make_unexpected(fmt::format("invalid character {} at pos {}", c, std::distance(std::begin(hex), iter)));
return std::unexpected(fmt::format("invalid character {} at pos {}", c, std::distance(std::begin(hex), iter)));
}
iter++;
@@ -67,7 +67,7 @@ tl::expected<std::basic_string<unsigned char>, std::string> fromHexString(std::s
case 'A'...'F': nibbles.nibble1 = c - 'A' + 10; break;
case 'a'...'f': nibbles.nibble1 = c - 'a' + 10; break;
default:
return tl::make_unexpected(fmt::format("invalid character {} at pos {}", c, std::distance(std::begin(hex), iter)));
return std::unexpected(fmt::format("invalid character {} at pos {}", c, std::distance(std::begin(hex), iter)));
}
iter++;
@@ -107,6 +107,58 @@ std::string toBase64String(std::basic_string_view<unsigned char> buf)
return out;
}
namespace {
constexpr std::string_view base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
bool is_base64(char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
}
std::string base64_decode(std::string const& encoded_string)
{
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret.push_back(char_array_3[i]);
i = 0;
}
}
if (i) {
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++) ret.push_back(char_array_3[j]);
}
return ret;
}
bool stringEqualsIgnoreCase(std::string_view a, std::string_view b)
{
if (a.size() != b.size())
@@ -223,7 +275,7 @@ std::optional<std::string_view> getStringBetween(std::string_view search, std::s
namespace {
/* Derived from original code by CodesInChaos */
tl::expected<char *, std::string>
std::expected<char *, std::string>
sodium_bin2hex(char *const hex, const size_t hex_maxlen,
const unsigned char *const bin, const size_t bin_len)
{
@@ -233,7 +285,7 @@ sodium_bin2hex(char *const hex, const size_t hex_maxlen,
int c;
if (bin_len >= SIZE_MAX / 2 || hex_maxlen <= bin_len * 2U) {
return tl::make_unexpected("misuse because bin_len >= SIZE_MAX / 2 || hex_maxlen <= bin_len * 2U");
return std::unexpected("misuse because bin_len >= SIZE_MAX / 2 || hex_maxlen <= bin_len * 2U");
}
while (i < bin_len) {
c = bin[i] & 0xf;
+12 -4
View File
@@ -6,9 +6,7 @@
#include <string_view>
#include <random>
#include <algorithm>
// 3rdparty lib includes
#include <tl/expected.hpp>
#include <expected>
namespace cpputils {
inline std::string toString(bool val) { return val ? "true" : "false"; }
@@ -18,6 +16,10 @@ inline std::string toString(int16_t val) { return std::to_string(val); }
inline std::string toString(uint16_t val) { return std::to_string(val); }
inline std::string toString(int32_t val) { return std::to_string(val); }
inline std::string toString(uint32_t val) { return std::to_string(val); }
#ifdef IDF_VER
inline std::string toString(int val) { return std::to_string(val); }
inline std::string toString(unsigned int val) { return std::to_string(val); }
#endif
inline std::string toString(int64_t val) { return std::to_string(val); }
inline std::string toString(uint64_t val) { return std::to_string(val); }
inline std::string toString(float val) { return std::to_string(val); }
@@ -30,6 +32,10 @@ inline std::string toString(std::optional<int16_t> val) { if (val) return toStri
inline std::string toString(std::optional<uint16_t> val) { if (val) return toString(*val); else return {}; }
inline std::string toString(std::optional<int32_t> val) { if (val) return toString(*val); else return {}; }
inline std::string toString(std::optional<uint32_t> val) { if (val) return toString(*val); else return {}; }
#ifdef IDF_VER
inline std::string toString(std::optional<int> val) { if (val) return toString(*val); else return {}; }
inline std::string toString(std::optional<unsigned int> val) { if (val) return toString(*val); else return {}; }
#endif
inline std::string toString(std::optional<int64_t> val) { if (val) return toString(*val); else return {}; }
inline std::string toString(std::optional<uint64_t> val) { if (val) return toString(*val); else return {}; }
inline std::string toString(std::optional<float> val) { if (val) return toString(*val); else return {}; }
@@ -42,7 +48,7 @@ inline std::string toHexString(std::string_view str)
return toHexString(std::basic_string_view<unsigned char>{reinterpret_cast<const unsigned char *>(str.data()), str.size()});
}
tl::expected<std::basic_string<unsigned char>, std::string> fromHexString(std::string_view hex);
std::expected<std::basic_string<unsigned char>, std::string> fromHexString(std::string_view hex);
std::string toBase64String(std::basic_string_view<unsigned char> buf);
inline std::string toBase64String(std::string_view str)
@@ -50,6 +56,8 @@ inline std::string toBase64String(std::string_view str)
return toBase64String(std::basic_string_view<unsigned char>{reinterpret_cast<const unsigned char *>(str.data()), str.size()});
}
std::string base64_decode(std::string const& encoded_string);
bool stringEqualsIgnoreCase(std::string_view a, std::string_view b);
//void stringReplaceAll(char search, char replace, std::string &subject);
+2 -18
View File
@@ -3,7 +3,8 @@ TEMPLATE = app
QT += core testlib
QT -= gui widgets
CONFIG += c++17 qt console warn_on depend_includepath testcase
CONFIG += c++23 qt console warn_on depend_includepath testcase
QMAKE_CXXFLAGS += -std=c++2b
CONFIG -= app_bundle
SOURCES += \
@@ -16,23 +17,6 @@ include($$CPPUTILS_DIR/cpputils_src.pri)
include($$CPPUTILS_DIR/test/cpputilstestutils.pri)
include($$CPPUTILS_DIR/test/cpputilstestutils_src.pri)
equals(CLONE_EXPECTED, 1) {
EXPECTED_DIR = $$PWD/expected
message("Checking out expected...")
exists($$EXPECTED_DIR/.git): {
system("git -C $$EXPECTED_DIR pull")
} else {
system("git clone https://github.com/0xFEEDC0DE64/expected.git $$EXPECTED_DIR")
}
} else: exists($$PWD/../../expected/include) {
EXPECTED_DIR = $$PWD/../../expected
} else {
error("expected not found, please run git submodule update --init")
}
include($$EXPECTED_DIR/expected.pri)
equals(CLONE_FMT, 1) {
FMT_DIR = $$PWD/fmt