Skip to content

Commit 09896d5

Browse files
earlephilhowerd-a-v
authored andcommitted
Add String::concat(char*, len) to allow non null-term strings (#6754)
* Add comcat(char*, len) to Sting Fixes #5061 Adds a concat(const char *data, int len) method which allows arbitrary sequences of data (including ones w/embedded \0s) to be appended to a String. May be useful for certain MQTT operations. Adds sanity test for the feature to host suite * Review comment cleanups
1 parent d2d0ee3 commit 09896d5

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

cores/esp8266/WString.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ unsigned char String::concat(const char *cstr, unsigned int length) {
331331
return 0;
332332
memmove_P(wbuffer() + len(), cstr, length + 1);
333333
setLen(newlen);
334+
wbuffer()[newlen] = 0;
334335
return 1;
335336
}
336337

cores/esp8266/WString.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class String {
116116
unsigned char concat(float num);
117117
unsigned char concat(double num);
118118
unsigned char concat(const __FlashStringHelper * str);
119+
unsigned char concat(const char *cstr, unsigned int length);
119120

120121
// if there's not enough memory for the concatenated value, the string
121122
// will be left unchanged (but this isn't signalled in any way)
@@ -311,7 +312,6 @@ class String {
311312
void init(void);
312313
void invalidate(void);
313314
unsigned char changeBuffer(unsigned int maxStrLen);
314-
unsigned char concat(const char *cstr, unsigned int length);
315315

316316
// copy and move
317317
String & copy(const char *cstr, unsigned int length);

tests/host/core/test_string.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ TEST_CASE("String concantenation", "[core][String]")
131131
REQUIRE(str == "-100");
132132
str = String((long)-100, 10);
133133
REQUIRE(str == "-100");
134+
// Non-zero-terminated array concatenation
135+
const char buff[] = "abcdefg";
136+
String n;
137+
n = "1234567890"; // Make it a SSO string, fill with non-0 data
138+
n = "1"; // Overwrite [1] with 0, but leave old junk in SSO space still
139+
n.concat(buff, 3);
140+
REQUIRE(n == "1abc"); // Ensure the trailing 0 is always present even w/this funky concat
141+
for (int i=0; i<20; i++)
142+
n.concat(buff, 1); // Add 20 'a's to go from SSO to normal string
143+
REQUIRE(n == "1abcaaaaaaaaaaaaaaaaaaaa");
144+
n = "";
145+
for (int i=0; i<=5; i++)
146+
n.concat(buff, i);
147+
REQUIRE(n == "aababcabcdabcde");
148+
n.concat(buff, 0); // And check no add'n
149+
REQUIRE(n == "aababcabcdabcde");
134150
}
135151

136152
TEST_CASE("String comparison", "[core][String]")

0 commit comments

Comments
 (0)