Skip to content

Strings without null-termination #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 25, 2021
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