Switching from String to std::string is way harder than I thought !
Posted: Tue Mar 26, 2019 11:16 am
Hello !
I am currently trying to switch from using String to std::string, because even if it's WAY easier to use String, I understand that it plays with realloc() and can cause "heap fragmentation".
I would like some help with moving from String to std::string.
I have a void function that takes as argument a std::string string1 and puts it in a ring buffer but also prints it on the Serial.
And it doesn't work when I try and use the following:
Not even a simple for works properly:
It outputs: inia nia ia a. What is wrong with std::string ?
String is a champion and easy to use. I have to write a lot of additional lines for a std::string to work properly.
The following while:
Outputs:
Connecting to WiFi
onnecting to WiFi
nnecting to WiFi
necting to WiFi
So how am I supposed to send one beautiful string to logOutput so that it can add it to the ring buffer ? I can’t send pieces of it, because they’ll be put in a ring buffer that, later on, will print them on different lines.
If I can’t use + operator to concatenate char to a string how should I do it ?
I am beginning to miss the ability to concatenate String as easily as using + operator.
For reference:
Circular Buffer:
I use this circular buffer to send what ever string I would output with Serial.println(); to a webpage using ESPAsyncWebServer library’s processor(); function:
I am currently trying to switch from using String to std::string, because even if it's WAY easier to use String, I understand that it plays with realloc() and can cause "heap fragmentation".
I would like some help with moving from String to std::string.
I have a void function that takes as argument a std::string string1 and puts it in a ring buffer but also prints it on the Serial.
Code: Select all
void logOutput(std::string string1) {
delay(500);
circle.push(string1);
Serial.println(string1.c_str());
}
Code: Select all
char* name = "name";
logOutput("Something something" + name);
logOutput("Something" + "Something"); //also doesn't work
Code: Select all
for(int i = 1; i<=10;i++){
logOutput("Linia " + i);
}
String is a champion and easy to use. I have to write a lot of additional lines for a std::string to work properly.
The following while:
Code: Select all
while(WiFi.status() != WL_CONNECTED && k<20) {
delay(1000);
k++;
logOutput("Connecting to WiFi");
}
Connecting to WiFi
onnecting to WiFi
nnecting to WiFi
necting to WiFi
So how am I supposed to send one beautiful string to logOutput so that it can add it to the ring buffer ? I can’t send pieces of it, because they’ll be put in a ring buffer that, later on, will print them on different lines.
If I can’t use + operator to concatenate char to a string how should I do it ?
I am beginning to miss the ability to concatenate String as easily as using + operator.
For reference:
Circular Buffer:
Code: Select all
std::string strlog; // global variable
struct ring_buffer
{
ring_buffer(size_t cap) : buffer(cap) {}
bool empty() const { return sz == 0 ; }
bool full() const { return sz == buffer.size() ; }
void push( std::string str )
{
if(last >= buffer.size()) last = 0 ;
buffer[last] = str ;
++last ;
if(full())
first = (first+1) % buffer.size() ;
else ++sz ;
}
void print() const {
strlog = ""; // empty its previous content
if( first < last )
for( size_t i = first ; i < last ; ++i ) {
strlog.append(buffer[i]);
strlog.append("<br>");
}
else
{
for( size_t i = first ; i < buffer.size() ; ++i ) {
strlog.append(buffer[i]);
strlog.append("<br>");
}
for( size_t i = 0 ; i < last ; ++i ) {
strlog.append(buffer[i]);
strlog.append("<br>");
}
}
}
private:
std::vector<std::string> buffer ;
size_t first = 0 ;
size_t last = 0 ;
size_t sz = 0 ;
};
Code: Select all
String processor(const String& var) {
circle.print();
if (var == "PLACEHOLDER_1")
return strlog.c_str();
return String();
}