diff --git a/strings.scad b/strings.scad index 3682779..cef528e 100644 --- a/strings.scad +++ b/strings.scad @@ -13,7 +13,7 @@ // Function: substr() // Usage: -// substr(str, [pos], [len]) +// newstr = substr(str, [pos], [len]); // Description: // Returns a substring from a string start at position `pos` with length `len`, or // if `len` isn't given, the rest of the string. @@ -39,7 +39,7 @@ function _substr(str,pos,len,substr="") = // Function: suffix() // Usage: -// suffix(str,len) +// newstr = suffix(str,len); // Description: // Returns the last `len` characters from the input string `str`. // If `len` is longer than the length of `str`, then the entirety of `str` is returned. @@ -56,7 +56,7 @@ function suffix(str,len) = // Function: str_find() // Usage: -// str_find(str,pattern,[last=],[all=],[start=]) +// ind = str_find(str,pattern,[last=],[all=],[start=]); // Description: // Searches input string `str` for the string `pattern` and returns the index or indices of the matches in `str`. // By default `str_find()` returns the index of the first match in `str`. If `last` is true then it returns the index of the last match. @@ -94,37 +94,56 @@ function str_find(str,pattern,start=undef,last=false,all=false) = _str_find_first(str,pattern,len(str)-len(pattern),start); function _str_find_first(str,pattern,max_sindex,sindex) = - sindex<=max_sindex && !_str_cmp(str,sindex, pattern)? + sindex<=max_sindex && !substr_match(str,sindex, pattern)? _str_find_first(str,pattern,max_sindex,sindex+1) : (sindex <= max_sindex ? sindex : undef); function _str_find_last(str,pattern,sindex) = - sindex>=0 && !_str_cmp(str,sindex, pattern)? + sindex>=0 && !substr_match(str,sindex, pattern)? _str_find_last(str,pattern,sindex-1) : (sindex >=0 ? sindex : undef); function _str_find_all(str,pattern) = pattern == "" ? count(len(str)) : - [for(i=[0:1:len(str)-len(pattern)]) if (_str_cmp(str,i,pattern)) i]; + [for(i=[0:1:len(str)-len(pattern)]) if (substr_match(str,i,pattern)) i]; + +// Function: substr_match() +// Usage +// bool = substr_match(str,start,pattern); +// Description: +// Returns true if the string `pattern` matches the string `str` starting +// at `str[start]`. If the string is too short for the pattern, or +// `start` is out of bounds---either negative or beyond the end of the +// string---then substr_match returns false. +// Arguments: +// str = String to search +// start = Starting index for search in str +// pattern = String pattern to search for +// Examples: +// substr_match("abcde",2,"cd"); // Returns true +// substr_match("abcde",2,"cx"); // Returns false +// substr_match("abcde",2,"cdef"); // Returns false +// substr_match("abcde",-2,"cd"); // Returns false +// substr_match("abcde",19,"cd"); // Returns false +// substr_match("abc",1,""); // Returns true -// _str_cmp(str,sindex,pattern) -// returns true if the string pattern matches the string -// starting at index position sindex in the string. // // This is carefully optimized for speed. Precomputing the length // cuts run time in half when the string is long. Two other string // comparison methods were slower. -function _str_cmp(str,sindex,pattern) = - len(str)-sindex