Skip to content

Add String::concat(char*, len) to allow non null-term strings #6754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cores/esp8266/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -311,7 +312,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);
Expand Down
16 changes: 16 additions & 0 deletions tests/host/core/test_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ 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 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
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]")
Expand Down