Skip to content

Commit 4dd4ae5

Browse files
committed
fixup lastIndexOfImpl checking ptr instead of offset
similar to replace() use, we already know there is nothing past previously found needle ptr
1 parent 70222c2 commit 4dd4ae5

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

cores/esp8266/WString.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -840,22 +840,23 @@ int String::lastIndexOf(char ch, unsigned int fromIndex) const {
840840
}
841841

842842
int String::lastIndexOfImpl(internal_memmem_t impl, const char *str, unsigned int length, unsigned int fromIndex) const {
843-
const auto this_len = len();
843+
const char *buf = buffer();
844+
const char *const bufEnd = buf + len();
845+
846+
const auto this_len = bufEnd - buf;
844847
if (!this_len || !length || length > this_len)
845848
return -1;
846849
if (fromIndex >= this_len)
847850
fromIndex = this_len - 1;
851+
848852
int found = -1;
849-
const char *buf = buffer();
850-
unsigned int left = len();
851-
for (const char *p = buf; p <= buf + fromIndex; p++) {
852-
p = static_cast<const char *>(impl(p, left, str, length));
853+
for (const char *p = buf + fromIndex; p && p < bufEnd; p += length) {
854+
p = static_cast<const char *>(impl(p, bufEnd - p, str, length));
853855
if (!p)
854856
break;
855-
left = static_cast<unsigned int>(p - buf);
856-
if (left <= fromIndex)
857-
found = p - buf;
857+
found = p - buf;
858858
}
859+
859860
return found;
860861
}
861862

0 commit comments

Comments
 (0)