WString.cpp

What's the difference between Arduino(original) and ESP32 WString source,
and which is a better source for ARM based Arduinos

You can compare the sources :wink: I doubt there is a significant difference.

If you add an ARM based Arduino in the IDE, everything should be taken care of for you.

I just did a diff and there are some significant differences. For example, here's Arduino's changeBuffer:

unsigned char String::changeBuffer(unsigned int maxStrLen)
{
	char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
	if (newbuffer) {
		buffer = newbuffer;
		capacity = maxStrLen;
		return 1;
	}
	return 0;
}

Here's ESP32:

unsigned char String::changeBuffer(unsigned int maxStrLen) {
    // Can we use SSO here to avoid allocation?
    if (maxStrLen < sizeof(sso_buf)) {
        if (sso() || !buffer()) {
            // Already using SSO, nothing to do
            setSSO(true);
            return 1;
        } else { // if bufptr && !sso()
            // Using bufptr, need to shrink into sso_buff
            char temp[sizeof(sso_buf)];
            memcpy(temp, buffer(), maxStrLen);
            free(wbuffer());
            setSSO(true);
            memcpy(wbuffer(), temp, maxStrLen);
            return 1;
        }
    }
    // Fallthrough to normal allocator
    size_t newSize = (maxStrLen + 16) & (~0xf);
    // Make sure we can fit newsize in the buffer
    if (newSize > CAPACITY_MAX) {
        return false;
    }
    uint16_t oldLen = len();
    char *newbuffer = (char *) realloc(sso() ? nullptr : wbuffer(), newSize);
    if(newbuffer) {
        size_t oldSize = capacity() + 1; // include NULL.
        if (sso()) {
            // Copy the SSO buffer into allocated space
            memcpy(newbuffer, sso_buf, sizeof(sso_buf));
        }
        if (newSize > oldSize)
        {
            memset(newbuffer + oldSize, 0, newSize - oldSize);
        }
        setSSO(false);
        setCapacity(newSize - 1);
        setLen(oldLen); // Needed in case of SSO where len() never existed
        setBuffer(newbuffer);
        return 1;
    }
    return 0;
}

I see Arduino put WString.cpp in ArduinoCore-API (renamed as String.cpp):

The files in ArduinoCore-API are the non-architecture specific parts of the core library and should be able to be used for any hardware core, whether Arduino's or 3rd party. I wonder if it was a mistake to put that code in ArduinoCore-API?

some libraries (as Azure SAS or Google IoT core - MQTT password) use:

return /String/ "URL" + urlEncode(url) + "expired=" + String(exp);

and I have problems with String concat and returning Strings ... and looking for solutions or where is the problem

EDIT: 3rd party (my port, ARM based) and I use original Arduino WString source...

"URL" is not a string but a const char*. Const char* does not have concat method.

Try:
String("URL") + ...

The function return result as String

This concat make problem (return empty string)

for malloc and free I make stress-test - not have problems

My advice is to write simplified programs until you are able to create one that reproduces the problem with the minimum amount of code. For example:

void setup() {
  Serial.begin(9600);
  while (!Serial) {}
  Serial.println("foo" + String("bar"));
}

void loop() {}

problem is elsewhere ... WString work fine
The Azure library use VLA array and and maybe need settings fo C99 - will test

I make "simple" Arduino simulator for "debug"
wow - I even like it ... I will add libraries and release it to GitHub

simple "demo": :slight_smile: video