Skip to content

Commit 4b1f89c

Browse files
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.
1 parent 7f2eca2 commit 4b1f89c

File tree

2 files changed

+2
-8
lines changed

2 files changed

+2
-8
lines changed

Diff for: hardware/arduino/avr/cores/arduino/WString.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,7 @@ String String::substring(unsigned int left, unsigned int right) const
624624
String out;
625625
if (left >= len) return out;
626626
if (right > len) right = len;
627-
char temp = buffer[right]; // save the replaced character
628-
buffer[right] = '\0';
629-
out = buffer + left; // pointer arithmetic
630-
buffer[right] = temp; //restore character
627+
out.copy(buffer + left, right - left);
631628
return out;
632629
}
633630

Diff for: hardware/arduino/sam/cores/arduino/WString.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,7 @@ String String::substring(unsigned int left, unsigned int right) const
626626
String out;
627627
if (left >= len) return out;
628628
if (right > len) right = len;
629-
char temp = buffer[right]; // save the replaced character
630-
buffer[right] = '\0';
631-
out = buffer + left; // pointer arithmetic
632-
buffer[right] = temp; //restore character
629+
out.copy(buffer + left, right - left);
633630
return out;
634631
}
635632

0 commit comments

Comments
 (0)