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 <werwolv98@gmail.com>
This commit is contained in:
Martín Montané
2024-11-17 13:54:03 +01:00
committed by GitHub
parent bf94cb7243
commit c8ebb3eb8a

View File

@@ -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;
};