Skip to content

Commit d39a2c4

Browse files
committed
String: Provide sensible default constructor
- Provide a sensible constructor to create a blank String, without copying a zero-length string. - Also provide a simple constructor that accepts a single char.
1 parent 491a12b commit d39a2c4

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

cores/esp32/WString.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
/* Constructors */
3131
/*********************************************/
3232

33+
String::String() {
34+
init();
35+
reserve(0); // This will create a valid zero-length string with SSO enabled.
36+
}
37+
3338
String::String(const char *cstr) {
3439
init();
3540
if (cstr) {
@@ -63,8 +68,7 @@ String::String(StringSumHelper &&rval) {
6368

6469
String::String(char c) {
6570
init();
66-
char buf[] = {c, '\0'};
67-
*this = buf;
71+
copy(&c, 1);
6872
}
6973

7074
String::String(unsigned char value, unsigned char base) {

cores/esp32/WString.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class String {
5454
// if the initial value is null or invalid, or if memory allocation
5555
// fails, the string will be marked as invalid (i.e. "if (s)" will
5656
// be false).
57-
String(const char *cstr = "");
57+
String();
58+
String(const char *cstr);
5859
String(const char *cstr, unsigned int length);
5960
#ifdef __GXX_EXPERIMENTAL_CXX0X__
6061
String(const uint8_t *cstr, unsigned int length) : String(reinterpret_cast<const char *>(cstr), length) {}

0 commit comments

Comments
 (0)