mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2024-12-29 16:29:40 +00:00
Combined str_matches and str_match and added start parameter.
This commit is contained in:
parent
60be226e85
commit
e3be9a3b5b
1 changed files with 31 additions and 20 deletions
51
strings.scad
51
strings.scad
|
@ -219,35 +219,53 @@ function _str_cmp_recurse(str,sindex,pattern,plen,pindex=0,) =
|
||||||
|
|
||||||
// Function: str_match()
|
// Function: str_match()
|
||||||
// Usage:
|
// Usage:
|
||||||
// str_match(str,pattern)
|
// str_match(str,pattern,[last],[all],[start])
|
||||||
// Description:
|
// Description:
|
||||||
// Searches input string `str` for the string `pattern` and returns the index of the first or last match in `str`.
|
// Searches input string `str` for the string `pattern` and returns the index or indices of the matches in `str`.
|
||||||
// If `pattern` is empty then it returns 0. If `pattern` doesn't match it returns undef.
|
// By default str_match() returns the index of the first match in `str`. If `last` is true then it returns the index of the last match.
|
||||||
|
// If the pattern is the empty string the first match is at zero and the last match is the last character of the `str`.
|
||||||
|
// If `start` is set then the search begins at index start, working either forward and backward from that position. If you set `start`
|
||||||
|
// and `last` is true then the search will find the pattern if it begins at index `start`. If no match exists, returns undef.
|
||||||
|
// If you set `all` to true then all str_match() returns all of the matches in a list, or an empty list if there are no matches.
|
||||||
// Arguments:
|
// Arguments:
|
||||||
// str = string to search
|
// str = string to search
|
||||||
// pattern = string pattern to search for
|
// pattern = string pattern to search for
|
||||||
// last = set to true to return the last match. Default: false
|
// last = set to true to return the last match. Default: false
|
||||||
|
// all = set to true to return all matches as a list. Overrides last. Default: false
|
||||||
|
// start = index where the search starts
|
||||||
// Example:
|
// Example:
|
||||||
// str_match("abc123def123abc","123"); // Returns 3
|
// str_match("abc123def123abc","123"); // Returns 3
|
||||||
// str_match("abc123def123abc","b"); // Returns 1
|
// str_match("abc123def123abc","b"); // Returns 1
|
||||||
// str_match("abc123def123abc","1234"); // Returns undef
|
// str_match("abc123def123abc","1234"); // Returns undef
|
||||||
// str_match("abc",""); // Returns 0
|
// str_match("abc",""); // Returns 0
|
||||||
// str_match("abc123def123abc","123",last=true); // Returns 9
|
// str_match("abc123def123", "123", start=4); // Returns 9
|
||||||
// str_match("abc123def123abc","b",last=true); // Returns 13
|
// str_match("abc123def123abc","123",last=true); // Returns 9
|
||||||
// str_match("abc123def123abc","1234",last=true); // Returns undef
|
// str_match("abc123def123abc","b",last=true); // Returns 13
|
||||||
// str_match("abc","",last=true); // Returns 2
|
// str_match("abc123def123abc","1234",last=true); // Returns undef
|
||||||
function str_match(str,pattern,last=false) =
|
// str_match("abc","",last=true); // Returns 2
|
||||||
pattern=="" ? (last?len(str)-1:0) :
|
// str_match("abc123def123", "123", start=8, last=true)); // Returns 3
|
||||||
last ? _str_match_last(str,pattern,len(str)-len(pattern)) :
|
// str_match("abc123def123abc","123",all=true); // Returns [3,9]
|
||||||
_str_match(str,pattern,len(str)-len(pattern));
|
// str_match("abc123def123abc","b",all=true); // Returns [1,13]
|
||||||
|
// str_match("abc123def123abc","1234",all=true); // Returns []
|
||||||
|
// str_match("abc","",all=true); // Returns [0,1,2]
|
||||||
|
function str_match(str,pattern,start=undef,last=false,all=false) =
|
||||||
|
all ? _str_matches(str,pattern) :
|
||||||
|
let( start = first_defined([start,last?len(str)-len(pattern):0]))
|
||||||
|
pattern=="" ? start :
|
||||||
|
last ? _str_match_last(str,pattern,start) :
|
||||||
|
_str_match(str,pattern,len(str)-len(pattern),start);
|
||||||
|
|
||||||
function _str_match(str,pattern,max_sindex,sindex=0) =
|
function _str_match(str,pattern,max_sindex,sindex) =
|
||||||
sindex<=max_sindex && !_str_cmp(str,sindex, pattern) ? _str_match(str,pattern,max_sindex,sindex+1) :
|
sindex<=max_sindex && !_str_cmp(str,sindex, pattern) ? _str_match(str,pattern,max_sindex,sindex+1) :
|
||||||
(sindex <= max_sindex ? sindex : undef);
|
(sindex <= max_sindex ? sindex : undef);
|
||||||
|
|
||||||
function _str_match_last(str,pattern,sindex) =
|
function _str_match_last(str,pattern,sindex) =
|
||||||
sindex>=0 && !_str_cmp(str,sindex, pattern) ? _str_match_last(str,pattern,sindex-1) :
|
sindex>=0 && !_str_cmp(str,sindex, pattern) ? _str_match_last(str,pattern,sindex-1) :
|
||||||
(sindex >=0 ? sindex : undef);
|
(sindex >=0 ? sindex : undef);
|
||||||
|
function _str_matches(str,pattern) =
|
||||||
|
pattern == "" ? list_range(len(str)) :
|
||||||
|
[for(i=[0:1:len(str)-len(pattern)]) if (_str_cmp(str,i,pattern)) i];
|
||||||
|
|
||||||
|
|
||||||
// Function: str_matches()
|
// Function: str_matches()
|
||||||
// Usage:
|
// Usage:
|
||||||
// str_matches(str,pattern)
|
// str_matches(str,pattern)
|
||||||
|
@ -258,13 +276,6 @@ function _str_match_last(str,pattern,sindex) =
|
||||||
// str = string to search
|
// str = string to search
|
||||||
// pattern = string pattern to search for
|
// pattern = string pattern to search for
|
||||||
// Example:
|
// Example:
|
||||||
// str_matches("abc123def123abc","123"); // Returns [3,9]
|
|
||||||
// str_matches("abc123def123abc","b"); // Returns [1,13]
|
|
||||||
// str_matches("abc123def123abc","1234"); // Returns []
|
|
||||||
// str_matches("abc",""); // Returns [0,1,2]
|
|
||||||
function str_matches(str,pattern) =
|
|
||||||
pattern == "" ? list_range(len(str)) :
|
|
||||||
[for(i=[0:1:len(str)-len(pattern)]) if (_str_cmp(str,i,pattern)) i];
|
|
||||||
|
|
||||||
|
|
||||||
// Function: starts_with()
|
// Function: starts_with()
|
||||||
|
|
Loading…
Reference in a new issue