From 8ccf5d0bbedd2ffc91084f4758b6b6a31c9b13ca Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Mon, 11 Nov 2019 07:54:22 -0800 Subject: [PATCH 1/2] 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 --- cores/esp8266/WString.cpp | 1 + cores/esp8266/WString.h | 2 +- tests/host/core/test_string.cpp | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index 5e31b27704..bab537737e 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -331,6 +331,7 @@ unsigned char String::concat(const char *cstr, unsigned int length) { return 0; memmove_P(wbuffer() + len(), cstr, length + 1); setLen(newlen); + wbuffer()[newlen] = 0; return 1; } diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h index 8c970abf14..ec90be1a8e 100644 --- a/cores/esp8266/WString.h +++ b/cores/esp8266/WString.h @@ -116,6 +116,7 @@ class String { unsigned char concat(float num); unsigned char concat(double num); unsigned char concat(const __FlashStringHelper * str); + unsigned char concat(const char *cstr, unsigned int length); // if there's not enough memory for the concatenated value, the string // will be left unchanged (but this isn't signalled in any way) @@ -284,7 +285,6 @@ class String { void init(void); void invalidate(void); unsigned char changeBuffer(unsigned int maxStrLen); - unsigned char concat(const char *cstr, unsigned int length); // copy and move String & copy(const char *cstr, unsigned int length); diff --git a/tests/host/core/test_string.cpp b/tests/host/core/test_string.cpp index 93dc85e460..358451c109 100644 --- a/tests/host/core/test_string.cpp +++ b/tests/host/core/test_string.cpp @@ -131,6 +131,20 @@ TEST_CASE("String concantenation", "[core][String]") REQUIRE(str == "-100"); str = String((long)-100, 10); REQUIRE(str == "-100"); + // Non-zero-terminated array concatenation + const char buff[] = "abcdefg"; + String n; + n = "1234567890"; // Make it a SSO string, fill with non-0 data + n = "1"; // Overwrite [1] with 0, but leave old jump in SSO space still + n.concat(buff, 3); + REQUIRE(n == "1abc"); // Ensure the trailing 0 is always present even w/this funky concat + for (int i=0; i<20; i++) n.concat(buff, 1); // Add 20 'a's to go from SSO to normal string + REQUIRE(n == "1abcaaaaaaaaaaaaaaaaaaaa"); + n = ""; + for (int i=0; i<=5; i++) n.concat(buff, i); + REQUIRE(n == "aababcabcdabcde"); + n.concat(buff, 0); // And check no add'n + REQUIRE(n == "aababcabcdabcde"); } TEST_CASE("String comparison", "[core][String]") From 15e799aad89335c980d5c1abcb6309d62568265a Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Mon, 11 Nov 2019 18:14:43 +0100 Subject: [PATCH 2/2] Review comment cleanups --- tests/host/core/test_string.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/host/core/test_string.cpp b/tests/host/core/test_string.cpp index 358451c109..2d8ced73fc 100644 --- a/tests/host/core/test_string.cpp +++ b/tests/host/core/test_string.cpp @@ -135,13 +135,15 @@ TEST_CASE("String concantenation", "[core][String]") const char buff[] = "abcdefg"; String n; n = "1234567890"; // Make it a SSO string, fill with non-0 data - n = "1"; // Overwrite [1] with 0, but leave old jump in SSO space still + n = "1"; // Overwrite [1] with 0, but leave old junk in SSO space still n.concat(buff, 3); REQUIRE(n == "1abc"); // Ensure the trailing 0 is always present even w/this funky concat - for (int i=0; i<20; i++) n.concat(buff, 1); // Add 20 'a's to go from SSO to normal string + for (int i=0; i<20; i++) + n.concat(buff, 1); // Add 20 'a's to go from SSO to normal string REQUIRE(n == "1abcaaaaaaaaaaaaaaaaaaaa"); n = ""; - for (int i=0; i<=5; i++) n.concat(buff, i); + for (int i=0; i<=5; i++) + n.concat(buff, i); REQUIRE(n == "aababcabcdabcde"); n.concat(buff, 0); // And check no add'n REQUIRE(n == "aababcabcdabcde");