Forum Rule: Always post complete source code & details to reproduce any issue!
Page 2 of 2 FirstFirst 1 2
Results 26 to 32 of 32

Thread: Something wrong with Adding a String to itself!

  1. #26
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,685
    Used www_GitHub to edit a fork and did a pull request:: .../PaulStoffregen/cores/pull/165

  2. #27
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,416
    Sounds great! I took the rest of the day off yesterday.

  3. #28
    Senior Member
    Join Date
    Jan 2013
    Posts
    843
    Quote Originally Posted by defragster View Post
    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;

  4. #29
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,685
    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.

  5. #30
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,416
    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;
    }

  6. #31
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,685
    Quote Originally Posted by KurtE View Post
    ... 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 . . .

  7. #32
    Senior Member
    Join Date
    Jan 2013
    Posts
    843
    Quote Originally Posted by defragster View Post
    ... 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;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •