Skip to content

Commit

Permalink
relax starts_with and end_with error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianVmariano committed Jan 8, 2025
1 parent 93b9915 commit f9be26f
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions strings.scad
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ function _str_find_all(str,pattern) =
// comparison methods were slower.
function substr_match(str,start,pattern) =
assert(is_str(str))
assert(is_str(pattern))
len(str)-start <len(pattern)? false
: _substr_match_recurse(str,start,pattern,len(pattern));

Expand All @@ -167,15 +168,15 @@ function _substr_match_recurse(str,sindex,pattern,plen,pindex=0,) =
// bool = starts_with(str,pattern);
// Description:
// Returns true if the input string `str` starts with the specified string pattern, `pattern`.
// Otherwise returns false.
// Otherwise returns false. (If the input is not a string, returns false.)
// Arguments:
// str = String to search.
// pattern = String pattern to search for.
// Example:
// starts_with("abcdef","abc"); // Returns true
// starts_with("abcdef","def"); // Returns false
// starts_with("abcdef",""); // Returns true
function starts_with(str,pattern) = substr_match(str,0,pattern);
function starts_with(str,pattern) = is_string(str) && substr_match(str,0,pattern);


// Function: ends_with()
Expand All @@ -186,15 +187,15 @@ function starts_with(str,pattern) = substr_match(str,0,pattern);
// bool = ends_with(str,pattern);
// Description:
// Returns true if the input string `str` ends with the specified string pattern, `pattern`.
// Otherwise returns false.
// Otherwise returns false. (If the input is not a string, returns false.)
// Arguments:
// str = String to search.
// pattern = String pattern to search for.
// Example:
// ends_with("abcdef","def"); // Returns true
// ends_with("abcdef","de"); // Returns false
// ends_with("abcdef",""); // Returns true
function ends_with(str,pattern) = substr_match(str,len(str)-len(pattern),pattern);
function ends_with(str,pattern) = is_str(str) && substr_match(str,len(str)-len(pattern),pattern);



Expand Down

0 comments on commit f9be26f

Please sign in to comment.