Skip to content

Commit

Permalink
Add string constructor and concat routines taking explicit length args (
Browse files Browse the repository at this point in the history
espressif#5586)

## Summary
Applies the upstream changes here: arduino/ArduinoCore-API@3b88acac8^...0d83f1a

## Impact
Adds new String convenience methods that are now available in the mainline Arduino implementation, simplifying interoperability with C code that uses pointer+length strings rather than 0-termination. Also includes a change to avoid mutating the source string when taking a substring.
  • Loading branch information
rcombs authored Aug 31, 2021
1 parent 4a55ff9 commit 24b76cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 7 additions & 4 deletions cores/esp32/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ String::String(const char *cstr) {
copy(cstr, strlen(cstr));
}

String::String(const char *cstr, unsigned int length) {
init();
if (cstr)
copy(cstr, length);
}

String::String(const String &value) {
init();
*this = value;
Expand Down Expand Up @@ -705,10 +711,7 @@ String String::substring(unsigned int left, unsigned int right) const {
return out;
if(right > len())
right = len();
char temp = buffer()[right]; // save the replaced character
wbuffer()[right] = '\0';
out = wbuffer() + left; // pointer arithmetic
wbuffer()[right] = temp; //restore character
out.copy(buffer() + left, right - left);
return out;
}

Expand Down
7 changes: 6 additions & 1 deletion cores/esp32/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class String {
// fails, the string will be marked as invalid (i.e. "if (s)" will
// be false).
String(const char *cstr = "");
String(const char *cstr, unsigned int length);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
String(const uint8_t *cstr, unsigned int length) : String((const char*)cstr, length) {}
#endif
String(const String &str);
String(const __FlashStringHelper *str);
#ifdef __GXX_EXPERIMENTAL_CXX0X__
Expand Down Expand Up @@ -108,6 +112,8 @@ class String {
// concatenation is considered unsuccessful.
unsigned char concat(const String &str);
unsigned char concat(const char *cstr);
unsigned char concat(const char *cstr, unsigned int length);
unsigned char concat(const uint8_t *cstr, unsigned int length) {return concat((const char*)cstr, length);}
unsigned char concat(char c);
unsigned char concat(unsigned char c);
unsigned char concat(int num);
Expand Down Expand Up @@ -326,7 +332,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

0 comments on commit 24b76cb

Please sign in to comment.