Mostly port project config and helpers, clean up some stuff

This commit is contained in:
Lorow
2024-09-20 00:13:49 +02:00
parent 427f586ae8
commit e6e884b54c
10 changed files with 795 additions and 18 deletions

View File

@@ -0,0 +1,3 @@
idf_component_register(SRCS "Helpers/helpers.cpp"
INCLUDE_DIRS "helpers"
)

View File

@@ -0,0 +1,80 @@
#include "helpers.hpp"
char *Helpers::itoa(int value, char *result, int base)
{
// check that the base if valid
if (base < 2 || base > 36)
{
*result = '\0';
return result;
}
char *ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do
{
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)];
} while (value);
// Apply negative sign
if (tmp_value < 0)
*ptr++ = '-';
*ptr-- = '\0';
while (ptr1 < ptr)
{
tmp_char = *ptr;
*ptr-- = *ptr1;
*ptr1++ = tmp_char;
}
return result;
}
void split(const std::string &str, const std::string &splitBy, std::vector<std::string> &tokens)
{
/* Store the original string in the array, so we can loop the rest
* of the algorithm. */
tokens.emplace_back(str);
// Store the split index in a 'size_t' (unsigned integer) type.
size_t splitAt;
// Store the size of what we're splicing out.
size_t splitLen = splitBy.size();
// Create a string for temporarily storing the fragment we're processing.
std::string frag;
// Loop infinitely - break is internal.
while (true)
{
/* Store the last string in the vector, which is the only logical
* candidate for processing. */
frag = tokens.back();
/* The index where the split is. */
splitAt = frag.find(splitBy);
// If we didn't find a new split point...
if (splitAt == std::string::npos)
{
// Break the loop and (implicitly) return.
break;
}
/* Put everything from the left side of the split where the string
* being processed used to be. */
tokens.back() = frag.substr(0, splitAt);
/* Push everything from the right side of the split to the next empty
* index in the vector. */
tokens.emplace_back(frag.substr(splitAt + splitLen, frag.size() - (splitAt + splitLen)));
}
}
std::vector<std::string> Helpers::split(const std::string &s, char delimiter)
{
std::vector<std::string> parts;
std::string part;
std::istringstream tokenStream(s);
while (std::getline(tokenStream, part, delimiter))
{
parts.push_back(part);
}
return parts;
}

View File

@@ -0,0 +1,36 @@
#ifndef HELPERS_HPP
#define HELPERS_HPP
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include <memory>
namespace Helpers
{
char *itoa(int value, char *result, int base);
void split(std::string str, std::string splitBy, std::vector<std::string> &tokens);
std::vector<std::string> split(const std::string &s, char delimiter);
/// @brief
/// @tparam ...Args
/// @param format
/// @param ...args
/// @return
template <typename... Args>
std::string format_string(const std::string &format, Args... args)
{
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
if (size_s <= 0)
{
std::cout << "Error during formatting.";
return "";
}
auto size = static_cast<size_t>(size_s);
std::unique_ptr<char[]> buf(new char[size]);
std::snprintf(buf.get(), size, format.c_str(), args...);
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
}
}
#endif // HELPERS_HPP