diff options
author | Cristian Maglie <c.maglie@bug.st> | 2014-09-12 11:56:12 +0200 |
---|---|---|
committer | Cristian Maglie <c.maglie@bug.st> | 2014-09-12 11:56:12 +0200 |
commit | 3155eeeae92ca78ec0ddda5db4a1da93b9002a86 (patch) | |
tree | 0456222aa0c3d19eb61cff9b0cd5ad4adc40ba25 /cores/arduino | |
parent | 0b094167de87edc14dedf775ed532a25f3d5d992 (diff) | |
parent | fde95cf5b5ace06f497842f9329d5207c4834898 (diff) |
Merge pull request #1937 from matthijskooijman/stringindex
String index fixes and cleanups
Diffstat (limited to 'cores/arduino')
-rw-r--r-- | cores/arduino/WString.cpp | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index ed880ce..dcd469d 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -619,7 +619,7 @@ String String::substring(unsigned int left, unsigned int right) const left = temp; } String out; - if (left > len) return out; + if (left >= len) return out; if (right > len) right = len; char temp = buffer[right]; // save the replaced character buffer[right] = '\0'; @@ -684,15 +684,16 @@ void String::replace(const String& find, const String& replace) } void String::remove(unsigned int index){ - if (index >= len) { return; } - int count = len - index; - remove(index, count); + // Pass the biggest integer as the count. The remove method + // below will take care of truncating it at the end of the + // string. + remove(index, (unsigned int)-1); } void String::remove(unsigned int index, unsigned int count){ if (index >= len) { return; } if (count <= 0) { return; } - if (index + count > len) { count = len - index; } + if (count > len - index) { count = len - index; } char *writeTo = buffer + index; len = len - count; strncpy(writeTo, buffer + index + count,len - index); |