Something wrong with Adding a String to itself!

Code:
  else if ( twostring.length() < 10000 ) {
    Serial.print( twostring );     Serial.println( "<<< SS0");
    twostring += twostring.substring( 0, 5 );
    Serial.print( twostring );     Serial.println( "<<< SS1");
    twostring += twostring.substring( 5, 9 );
    Serial.print( twostring );     Serial.println( "<<< SS2");
    twostring += twostring.substring( 10, twostring.length() );
    Serial.print( twostring );     Serial.println( "<<< SS3");
  }
This code doesn't actually test the overlapping substring case. "twostring.substring()" creates a temporary string with a new buffer.

To actually test that case you need to do something like:
Code:
    twostring.append(twostring.c_str(), 5);
    twostring.append(twostring.c_str() + 5, 4);
    twostring += twostring.c_str() + 10;
 
Merge Complete! - thanks Paul and those above . . .

I haven't done anything with the p#28 note. But found: "error: 'String& String::append(const char*, unsigned int)' is protected"

Indeed of course substring munges the current copy (hacks in a NULL) then makes a temp String and returns that, so it would not be affected mem move. There may not be a way for a user to hit a substring - given how it is stored.
 
Congrats!
Yes I found that the one was protected as well, with lots of things calling it. so you could do things like:
twostring.append(twostring.c_str() + 5);

Which is implemented by:
Code:
String & String::append(const char *cstr)
{
	if (cstr) append(cstr, strlen(cstr));
	return *this;
}
 
... twostring.append(twostring.c_str() + 5);

That was in the tni p_28 and it what gave me the error on compile.

Given that the szCstr is the underlying storage - it can't be publicly exposed easily. The substring works hard to make a temp String . . .
 
... twostring.append(twostring.c_str() + 5);
That was in the tni p_28 and it what gave me the error on compile.
This should compile. As should:
Code:
twostring += twostring.c_str() + 10;

Given that the szCstr is the underlying storage - it can't be publicly exposed easily.
You can get at the internal buffer via 2 public methods, c_str() and operator[] (e.g. char* ptr = &twostring[2]).

To test the protected method:
Code:
class StringTest: public String {
public:
    using String::append;
};

void test() {
    StringTest twostring;
    twostring.append(twostring.c_str(), 5);
    twostring.append(twostring.c_str() + 5, 4);
    twostring += twostring.c_str() + 10;
}
 
Back
Top