Closed as not planned
Description
Description
replacing multiple occurrences of a string with a smaller string only works for an odd number of occurrences
Environment
Boards platform name
"Raspberry Pi Pico/RP2040" boards platform from Earle F. Philhower, III
Boards platform version
Original report
Not specified
Last verified with
4.0.3
Code demonstrating problem
void setup() {
Serial.begin(9600);
while (!Serial) {}
// this code should remove all occurrences of "narf" from the string
String s = "narf";
Serial.println();
Serial.print("input: (");
Serial.print(s);
Serial.println(")");
s.replace("narf", "");
Serial.print("result: (");
Serial.print(s);
Serial.println(")");
s = "narf narf";
Serial.println();
Serial.print("input: (");
Serial.print(s);
Serial.println(")");
s.replace("narf", "");
Serial.print("result: (");
Serial.print(s);
Serial.println(")");
}
void loop() {}
Serial monitor output:
input: (narf)
result: ()
input: (narf narf)
result: (narf narf)
Additional information
In this code:
while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
readFrom = foundAt + find.len;
diff = 0 - diff;
size -= diff;
}
line 669 (diff = 0 - diff;
) should be above the while
loop.