Skip to content

Add string constructor and concat routines taking explicit length args #5586

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 5 commits into from
Aug 31, 2021
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
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