Skip to content

Commit c11b4c3

Browse files
matthijskooijmanaentinger
authored andcommitted
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 53df991 commit c11b4c3

File tree

1 file changed

+1
-4
lines changed

1 file changed

+1
-4
lines changed

Diff for: api/String.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -618,10 +618,7 @@ String String::substring(unsigned int left, unsigned int right) const
618618
String out;
619619
if (left >= len) return out;
620620
if (right > len) right = len;
621-
char temp = buffer[right]; // save the replaced character
622-
buffer[right] = '\0';
623-
out = buffer + left; // pointer arithmetic
624-
buffer[right] = temp; //restore character
621+
out.copy(buffer + left, right - left);
625622
return out;
626623
}
627624

0 commit comments

Comments
 (0)