From d33f966b05e8409bebe297ea141afefcd617fae8 Mon Sep 17 00:00:00 2001 From: albertexye Date: Mon, 1 Apr 2024 14:36:18 -0400 Subject: [PATCH] when a negative start index is passed to str.find, 0 is used instead --- src/str.cpp | 2 +- tests/04_str.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/str.cpp b/src/str.cpp index b6ca04d2..9965e6eb 100644 --- a/src/str.cpp +++ b/src/str.cpp @@ -266,7 +266,7 @@ int utf8len(unsigned char c, bool suppress){ } int Str::index(const Str& sub, int start) const { - if (start < 0 || start >= this->u8_length()) return -1; + if (start < 0) start = 0; auto p = std::search(data + start, data + size, sub.data, sub.data + sub.size); if(p == data + size) return -1; return p - data; diff --git a/tests/04_str.py b/tests/04_str.py index 4c3d5ef8..4801d117 100644 --- a/tests/04_str.py +++ b/tests/04_str.py @@ -242,4 +242,5 @@ except ValueError: assert a.find('1') == 0 assert a.find('1', 1) == -1 +assert a.find('1', -1000000) == 0