Skip to content
Prev Previous commit
Next Next commit
Don't mess with the original in String::substring
Before, substring would (temporarily) add a \0 in the original string at
the end of the substring, so the substring could be copied into a new
String object. However, now that the String::copy() method can work with
non-nul-terminated strings (by passing an explicit length), this trick
is not needed and we can just call the copy method instead.
  • Loading branch information
matthijskooijman committed Jan 25, 2021
commit dd236bfd2c62ed083aaa1b1229d71e85fd92f7a9
5 changes: 1 addition & 4 deletions api/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,10 +632,7 @@ String String::substring(unsigned int left, unsigned int right) const
String out;
if (left >= len) return out;
if (right > len) right = len;
char temp = buffer[right]; // save the replaced character
buffer[right] = '\0';
out = buffer + left; // pointer arithmetic
buffer[right] = temp; //restore character
out.copy(buffer + left, right - left);
return out;
}

Expand Down