aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2024-07-29 12:06:52 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2024-07-29 12:06:52 +0200
commite122c6ff4a7f21026332ce9211ad095bd44e71ea (patch)
treeb05ba5261fdb1089c94760f834f88ef7dcfa0a3a
parentc3212dbda325bdf6eaff6a7652c996a28e8ba688 (diff)
Fix bug in $string.{contains,ends_with,replace}() (GH issue #405)
These functions use the common rfind() helper which contains the bug.
-rw-r--r--libbuild2/functions-string.cxx4
-rw-r--r--tests/function/string/testscript2
2 files changed, 4 insertions, 2 deletions
diff --git a/libbuild2/functions-string.cxx b/libbuild2/functions-string.cxx
index eccc6c7..58c17d7 100644
--- a/libbuild2/functions-string.cxx
+++ b/libbuild2/functions-string.cxx
@@ -38,14 +38,14 @@ namespace build2
{
n -= sn; // Don't consider characters out of range.
- for (size_t p (n);; )
+ for (size_t p (n);; --p)
{
if ((ic
? icasecmp (ss, s.c_str () + p, sn)
: s.compare (p, sn, ss)) == 0)
return p;
- if (--p == 0)
+ if (p == 0)
break;
}
}
diff --git a/tests/function/string/testscript b/tests/function/string/testscript
index 8eb5760..57f30e2 100644
--- a/tests/function/string/testscript
+++ b/tests/function/string/testscript
@@ -47,6 +47,7 @@
{
$* <'print $string.contains(abcdabcd, da, once)' >'true' : true
$* <'print $string.contains(abcdabcd, bc, once)' >'false' : false
+ $* <'print $string.contains(abcdefgh, ab, once)' >'true' : true-begin
}
}
@@ -76,6 +77,7 @@
{
$* <'print $string.ends_with( abcd, cd)' >'true' : true
$* <'print $string.ends_with( abcd, bc)' >'false' : false
+ $* <'print $string.ends_with( abcd, xxxx)' >'false' : false-equal-size
$* <'print $ends_with([string] abcd, abcd)' >'true' : typed
}