Skip to content

Commit cba9193

Browse files
committed
Added concat method to String Class.
Enables serialization of JSON directly to String class
1 parent 484b6f5 commit cba9193

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

lib/ArduinoNative/src/WString.h

+29
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
* Compile Switches
4242
*****************************************************************************/
4343

44+
#ifndef ARDUINOJSON_ENABLE_ARDUINO_STRING
45+
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
46+
#endif
47+
4448
/******************************************************************************
4549
* Includes
4650
*****************************************************************************/
@@ -648,6 +652,31 @@ class String
648652
}
649653
}
650654

655+
bool concat(const String& s)
656+
{
657+
this->operator+=(s);
658+
return true;
659+
}
660+
661+
bool concat(const char* cstr)
662+
{
663+
if ((nullptr == cstr) || (0U == strlen(cstr)))
664+
{
665+
return false;
666+
}
667+
else
668+
{
669+
this->operator+=(cstr);
670+
return true;
671+
}
672+
}
673+
674+
bool concat(char c)
675+
{
676+
this->operator+=(c);
677+
return true;
678+
}
679+
651680
private:
652681
size_t m_size; /**< String buffer size */
653682
char* m_buffer; /**< String buffer */

test/test_WString/test_WString.cpp

+30-5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353

5454
static void testWStringReplacement(void);
5555
static void testWStringAppend(void);
56+
static void testWStringConcat(void);
5657

5758
/******************************************************************************
5859
* Local Variables
@@ -88,6 +89,7 @@ extern int main(int argc, char** argv)
8889

8990
RUN_TEST(testWStringReplacement);
9091
RUN_TEST(testWStringAppend);
92+
RUN_TEST(testWStringConcat);
9193

9294
return UNITY_END();
9395
}
@@ -154,11 +156,11 @@ static void testWStringReplacement(void)
154156
static void testWStringAppend(void)
155157
{
156158
String original = String("Im short");
157-
char classic[4];
158-
classic[0] = 'H';
159-
classic[1] = 'I';
160-
classic[2] = '!';
161-
classic[3] = '\0';
159+
char classic[4];
160+
classic[0] = 'H';
161+
classic[1] = 'I';
162+
classic[2] = '!';
163+
classic[3] = '\0';
162164

163165
original += String("- first amendment -");
164166
original += classic;
@@ -170,3 +172,26 @@ static void testWStringAppend(void)
170172
tester += "Im new";
171173
TEST_ASSERT_EQUAL_STRING("Im new", tester.c_str());
172174
}
175+
176+
/**
177+
* Test WString += implementation.
178+
*/
179+
static void testWStringConcat(void)
180+
{
181+
String original = String("Im short");
182+
char classic[4];
183+
classic[0] = 'H';
184+
classic[1] = 'I';
185+
classic[2] = '!';
186+
classic[3] = '\0';
187+
188+
original.concat(String("- first amendment -"));
189+
original.concat(classic);
190+
original.concat('-');
191+
192+
TEST_ASSERT_EQUAL_STRING("Im short- first amendment -HI!-", original.c_str());
193+
194+
String tester = String("");
195+
tester.concat("Im new");
196+
TEST_ASSERT_EQUAL_STRING("Im new", tester.c_str());
197+
}

0 commit comments

Comments
 (0)