Skip to content
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

String Class - Concat #78

Merged
merged 2 commits into from
Jan 25, 2024
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
27 changes: 27 additions & 0 deletions lib/ArduinoNative/src/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
* Compile Switches
*****************************************************************************/

#ifndef ARDUINOJSON_ENABLE_ARDUINO_STRING
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
#endif

/******************************************************************************
* Includes
*****************************************************************************/
Expand Down Expand Up @@ -648,6 +652,29 @@ class String
}
}

bool concat(const String& s)
{
(void)this->operator+=(s);
return true;
}

bool concat(const char* cstr)
{
if ((nullptr == cstr) || (0U == strlen(cstr)))
{
return false;
}

(void)this->operator+=(cstr);
return true;
}

bool concat(char c)
{
(void)this->operator+=(c);
return true;
}

private:
size_t m_size; /**< String buffer size */
char* m_buffer; /**< String buffer */
Expand Down
35 changes: 30 additions & 5 deletions test/test_WString/test_WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

static void testWStringReplacement(void);
static void testWStringAppend(void);
static void testWStringConcat(void);

/******************************************************************************
* Local Variables
Expand Down Expand Up @@ -88,6 +89,7 @@ extern int main(int argc, char** argv)

RUN_TEST(testWStringReplacement);
RUN_TEST(testWStringAppend);
RUN_TEST(testWStringConcat);

return UNITY_END();
}
Expand Down Expand Up @@ -154,11 +156,11 @@ static void testWStringReplacement(void)
static void testWStringAppend(void)
{
String original = String("Im short");
char classic[4];
classic[0] = 'H';
classic[1] = 'I';
classic[2] = '!';
classic[3] = '\0';
char classic[4];
classic[0] = 'H';
classic[1] = 'I';
classic[2] = '!';
classic[3] = '\0';

original += String("- first amendment -");
original += classic;
Expand All @@ -170,3 +172,26 @@ static void testWStringAppend(void)
tester += "Im new";
TEST_ASSERT_EQUAL_STRING("Im new", tester.c_str());
}

/**
* Test WString += implementation.
*/
static void testWStringConcat(void)
{
String original = String("Im short");
char classic[4];
classic[0] = 'H';
classic[1] = 'I';
classic[2] = '!';
classic[3] = '\0';

original.concat(String("- first amendment -"));
original.concat(classic);
original.concat('-');

TEST_ASSERT_EQUAL_STRING("Im short- first amendment -HI!-", original.c_str());

String tester = String("");
tester.concat("Im new");
TEST_ASSERT_EQUAL_STRING("Im new", tester.c_str());
}
Loading