From af88e5a6df0b1374b15ef0f4931c77c61c763e12 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Mon, 16 Aug 2021 09:23:00 +0200 Subject: [PATCH] :bug: fix string.rfind --- src/lib/string.c | 4 ++-- tests/string/find.gw | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/lib/string.c b/src/lib/string.c index 05c56d9a..1ca973e2 100644 --- a/src/lib/string.c +++ b/src/lib/string.c @@ -337,8 +337,8 @@ static MFUN(string_rfindStrStart) { const m_str base = STRING(o); const size_t sz = strlen(base); const char pos = *(m_int *)MEM(SZ_INT * 2); - const M_Object obj = *(M_Object *)MEM(SZ_INT * 2); - if (sz) { + const M_Object obj = *(M_Object *)MEM(SZ_INT); + if (sz && pos < sz) { const m_str arg = STRING(obj); m_str tmp = base + pos, str = NULL; while ((tmp = strstr(tmp, arg))) str = tmp++; diff --git a/tests/string/find.gw b/tests/string/find.gw index 6c42ccad..3b923f3c 100644 --- a/tests/string/find.gw +++ b/tests/string/find.gw @@ -2,25 +2,25 @@ <<< "test".find('a') >>>; <<< "".find('a') >>>; -<<< "test".find(-1, 'e') >>>; -<<< "test".find(0, 'e') >>>; -<<< "test".find(1, 'a') >>>; -<<< "".find(1, 'a') >>>; +<<< "test".find('e', -1) >>>; +<<< "test".find('e', 0) >>>; +<<< "test".find('a', 1) >>>; +<<< "".find('a', 0) >>>; <<< "test".find("es") >>>; <<< "test".find("a") >>>; <<< "".find("") >>>; -<<< "test".find(-1, "es") >>>; -<<< "test".find(0, "es") >>>; -<<< "test".find(1, "a") >>>; -<<< "".find(1, "") >>>; +<<< "test".find("es", -1) >>>; +<<< "test".find("es", 0) >>>; +<<< "test".find("a", 1) >>>; +<<< "".find("", 1) >>>; <<< "test".rfind("es") >>>; <<< "test".rfind("a") >>>; <<< "".rfind("") >>>; -<<< "test".rfind(-1, "es") >>>; -<<< "test".rfind(0, "e") >>>; -<<< "test".rfind(1, "a") >>>; -<<< "".rfind(1, "") >>>; +<<< "test".rfind("es", -1) >>>; +<<< "test".rfind("e", 0) >>>; +<<< "test".rfind("a", 1) >>>; +<<< "".rfind("", 1) >>>; -- 2.43.0