From c8ebb3eb8a6310de9eca7144330152428c565141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Montan=C3=A9?= <14808539+MartorSkull@users.noreply.github.com> Date: Sun, 17 Nov 2024 13:54:03 +0100 Subject: [PATCH] includes/std: Added checks to ends_with and starts_with to prevent failure (#295) Fixing cases where part is longer than string, this cases should both return false instead of failing. Co-authored-by: Nik --- includes/std/string.pat | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/includes/std/string.pat b/includes/std/string.pat index 0ce755df..3b3cb6a4 100644 --- a/includes/std/string.pat +++ b/includes/std/string.pat @@ -125,6 +125,8 @@ namespace auto std::string { @return True if the string starts with the substring, false otherwise. */ fn starts_with(str string, str part) { + if (std::string::length(string) < std::string::length(part)) + return false; return std::string::substr(string, 0, std::string::length(part)) == part; }; @@ -135,6 +137,8 @@ namespace auto std::string { @return True if the string ends with the substring, false otherwise. */ fn ends_with(str string, str part) { + if (std::string::length(string) < std::string::length(part)) + return false; return std::string::substr(string, std::string::length(string) - std::string::length(part), std::string::length(part)) == part; };