Skip to content

request to modify String::concat #5061

Closed
@darrendavidhumphrey

Description

@darrendavidhumphrey

unsigned char String::concat(const char *cstr, unsigned int length) {
unsigned int newlen = len + length;
if(!cstr)
return 0;
if(length == 0)
return 1;
if(!reserve(newlen))
return 0;
strcpy(buffer + len, cstr);
len = newlen;
return 1;
}

Basic Infos

  • This issue complies with the issue POLICY doc.
  • I have read the documentation at readthedocs and the issue is not addressed there.
  • I have tested that the issue is present in current master branch (aka latest git).
  • I have searched the issue tracker for a similar issue.
  • If there is a stack dump, I have decoded it.
  • I have filled out all fields below.

Platform

  • Hardware: [All ESPxx]
  • Core Version: [latest]
  • Development Env: [All]
  • Operating System: [All]

Problem Description

The String (Wstring.cpp) class has no method for concat() from a non-null terminated string. This is annoying, and inffecient, when processing payloads from MQTT messages, which are not-null terminated.

This existing Concat(cstr,length) method would work with minor modifications, but it is protected. I am proposing modifying it as shown below and making it public.

unsigned char String::concat(const char *cstr, unsigned int length) {
unsigned int newlen = len + length;
if(!cstr)
return 0;
if(length == 0)
return 1;
if(!reserve(newlen))
return 0;

-- strcpy(buffer + len, cstr); // Would fail if cstr is not NULL terminated
++ memcpy(buffer + len, cstr,length);
++ buffer[newlen] = 0; // insert NULL terminator

len = newlen;
return 1;
}

MCVE Sketch

#include <Arduino.h>
#include "WString.h"

void setup() {
   char bigBuff[1000];

   int messageSize= ReadMQTT_Payload(bigBuff);
   
    String someString;

// How you have to do it now.  concat called for each character
   string.reserve(messageSize)
   for (int i=0; i < messageSize; i++)
      someString.concat(bigBuff[i];

// How you could do it with proposal.  Single call to concat
   someString.concat(bigBuff,messageSize);
}

void loop() {

}

Debug Messages

Debug messages go here

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions