Skip to content

Commit 6bea238

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 803e662 commit 6bea238

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
@@ -632,10 +632,7 @@ String String::substring(unsigned int left, unsigned int right) const
632632
String out;
633633
if (left >= len) return out;
634634
if (right > len) right = len;
635-
char temp = buffer[right]; // save the replaced character
636-
buffer[right] = '\0';
637-
out = buffer + left; // pointer arithmetic
638-
buffer[right] = temp; //restore character
635+
out.copy(buffer + left, right - left);
639636
return out;
640637
}
641638

0 commit comments

Comments
 (0)