Skip to content

Commit 3d222cc

Browse files
committed
Merge pull request #1937 from matthijskooijman/stringindex
String index fixes and cleanups
2 parents 6f96742 + 04dba1e commit 3d222cc

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ String String::substring(unsigned int left, unsigned int right) const
619619
left = temp;
620620
}
621621
String out;
622-
if (left > len) return out;
622+
if (left >= len) return out;
623623
if (right > len) right = len;
624624
char temp = buffer[right]; // save the replaced character
625625
buffer[right] = '\0';
@@ -684,15 +684,16 @@ void String::replace(const String& find, const String& replace)
684684
}
685685

686686
void String::remove(unsigned int index){
687-
if (index >= len) { return; }
688-
int count = len - index;
689-
remove(index, count);
687+
// Pass the biggest integer as the count. The remove method
688+
// below will take care of truncating it at the end of the
689+
// string.
690+
remove(index, (unsigned int)-1);
690691
}
691692

692693
void String::remove(unsigned int index, unsigned int count){
693694
if (index >= len) { return; }
694695
if (count <= 0) { return; }
695-
if (index + count > len) { count = len - index; }
696+
if (count > len - index) { count = len - index; }
696697
char *writeTo = buffer + index;
697698
len = len - count;
698699
strncpy(writeTo, buffer + index + count,len - index);

0 commit comments

Comments
 (0)