Skip to content

Commit

Permalink
includes/std: Added checks to ends_with and starts_with to prevent fa…
Browse files Browse the repository at this point in the history
…ilure (#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>
  • Loading branch information
MartorSkull and WerWolv authored Nov 17, 2024
1 parent bf94cb7 commit c8ebb3e
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions includes/std/string.pat
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

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

Expand Down

0 comments on commit c8ebb3e

Please sign in to comment.