zero padded not using dyn mem alloc now

This commit is contained in:
Robin Müller 2020-06-04 17:30:09 +02:00
parent 11c64a91a3
commit 1cb241ca0c

View File

@ -1,6 +1,7 @@
#ifndef FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_ #ifndef FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_
#define FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_ #define FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACEBUFFER_H_
#include <framework/returnvalues/HasReturnvaluesIF.h>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
@ -58,21 +59,33 @@ private:
/** /**
* This helper function returns the zero padded string version of a number. * This helper function returns the zero padded string version of a number.
* The type is deduced automatically. * The type is deduced automatically.
* TODO: This uses dynamic memory allocation, so we should provide
* a custom streambuf class to use it (which takes maxSize as argument)
* Then we would propably
* @tparam T * @tparam T
* @param num * @param num
* @param width * @param width
* @return * @return
*/ */
template<typename T> template<typename T>
std::string zero_padded(const T& num, uint8_t width) { ReturnValue_t zeroPadded(std::string stringToFill, const T& num,
std::ostringstream string_to_pad; uint8_t width) {
string_to_pad << std::setw(width) << std::setfill('0') << num; auto numString = std::to_string(num);
std::string result = string_to_pad.str(); uint8_t i = 0;
if (result.length() > width) for(i = 0; i < width; i++) {
{ stringToFill[i] = '0';
result.erase(0, result.length() - width);
} }
return result; numString.copy(stringToFill.data() + i, numString.size());
// std::streambuf streambuf;
// std::ostringstream string_to_pad;
// string_to_pad << std::setw(width) << std::setfill('0') << num;
// std::string result = string_to_pad.str();
// if (result.length() > width)
// {
// result.erase(0, result.length() - width);
// }
// return result;
//std::string dest = std::string( number_of_zeros, '0').append( original_string);
} }
}; };