Merge pull request #289 from henrygab/patch-2

Fix [-Wrestrict] bug
This commit is contained in:
Ha Thach 2021-04-13 12:46:32 +07:00 committed by GitHub
commit ade7cafbae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -693,12 +693,21 @@ void String::remove(unsigned int index){
}
void String::remove(unsigned int index, unsigned int count){
if (index >= len) { return; }
if (count <= 0) { return; }
if (count > len - index) { count = len - index; }
char *writeTo = buffer + index;
// removes characters from the middle of a string.
if (count <= 0) { return; } // exit if nothing to remove
if (index >= len) { return; } // ensure start is within string length; thus, ensures (len-index >= 1)
if (count > len - index) { // ensure characters to remove is no larger than total length remaining
count = len - index;
}
char *writeTo = buffer + index;
char *copyFrom = buffer + index + count;
len = len - count;
strncpy(writeTo, buffer + index + count,len - index);
// strncpy() cannot be used with overlapping buffers, so copy one char at a time
unsigned int charactersToMove = len - index; // yes, uses post-adjusted length
for (unsigned int i = 0; i < charactersToMove; i++, writeTo++, copyFrom++) {
*writeTo = *copyFrom;
}
buffer[len] = 0;
}