Browse Source

When concat is same string to string, use memcpy, allows for re-alloc to move string on growth

https://forum.pjrc.com/threads/35688-Something-wrong-with-Adding-a-String-to-itself!?p=111109#post111109
teensy4-core
Defragster 8 years ago
parent
commit
00438a66ed
1 changed files with 12 additions and 1 deletions
  1. +12
    -1
      teensy3/WString.cpp

+ 12
- 1
teensy3/WString.cpp View File

@@ -248,8 +248,19 @@ String & String::append(const String &s)
String & String::append(const char *cstr, unsigned int length)
{
unsigned int newlen = len + length;
bool self = false;
unsigned int buffer_offset;
if ( (cstr >= buffer) && (cstr < (buffer+len) ) ) {
self = true;
buffer_offset = (unsigned int)(cstr-buffer);
}
if (length == 0 || !reserve(newlen)) return *this;
strcpy(buffer + len, cstr);
if ( self ) {
memcpy(buffer + len, buffer+buffer_offset, length);
buffer[newlen] = 0;
}
else
strcpy(buffer + len, cstr);
len = newlen;
return *this;
}

Loading…
Cancel
Save