Fixed structs.scad docs formatting. Fixed bug in is_struct().

This commit is contained in:
Revar Desmera 2020-05-19 17:28:02 -07:00
parent 892e98e773
commit d20caf7d59
2 changed files with 73 additions and 59 deletions

View file

@ -18,93 +18,107 @@
// Function: struct_set() // Function: struct_set()
// Usage: // Usage:
// struct_set(struct, keyword, value, [grow]) // struct_set(struct, keyword, value, [grow])
// struct_set(struct, [keyword1, value1, keyword2, value2, ...], [grow]) // struct_set(struct, [keyword1, value1, keyword2, value2, ...], [grow])
// Description: // Description:
// Sets the keyword(s) in the structure to the specified value(s), returning a new updated structure. If a keyword // Sets the keyword(s) in the structure to the specified value(s), returning a new updated structure. If a keyword
// exists its value is changed, otherwise the keyword is added to the structure. If grow is set to false then // exists its value is changed, otherwise the keyword is added to the structure. If grow is set to false then
// it is an error to set a keyword not already defined in the structure. If you specify the same keyword twice // it is an error to set a keyword not already defined in the structure. If you specify the same keyword twice
// that is also an error. If speed matters, use the first form with scalars rather than the list form: this is // that is also an error. If speed matters, use the first form with scalars rather than the list form: this is
// about thirty times faster. // about thirty times faster.
// Arguments: // Arguments:
// struct = input structure // struct = Input structure.
// keyword = keyword to set // keyword = Keyword to set.
// value = value to set the keyword to // value = Value to set the keyword to.
// grow = Set to true to allow structure to grow, or false for new keywords to generate an error. Default: true // grow = Set to true to allow structure to grow, or false for new keywords to generate an error. Default: true
function struct_set(struct, keyword, value=undef, grow=true) = function struct_set(struct, keyword, value=undef, grow=true) =
!is_list(keyword) ? !is_list(keyword)? (
let(ind=search([keyword],struct,1,0)[0]) let( ind=search([keyword],struct,1,0)[0] )
(ind==[] ? assert(grow,str("Unknown keyword \"",keyword)) ind==[]? (
concat(struct, [[keyword,value]]) : assert(grow,str("Unknown keyword \"",keyword))
list_set(struct, [ind], [[keyword,value]])) : concat(struct, [[keyword,value]])
_parse_pairs(struct,keyword,grow); ) : list_set(struct, [ind], [[keyword,value]])
) : _parse_pairs(struct,keyword,grow);
function _parse_pairs(spec, input, grow=true, index=0, result=undef) = function _parse_pairs(spec, input, grow=true, index=0, result=undef) =
assert(len(input)%2==0,"Odd number of entries in [keyword,value] pair list") assert(len(input)%2==0,"Odd number of entries in [keyword,value] pair list")
let( result = result==undef ? spec : result) let( result = result==undef ? spec : result)
index == len(input) ? result : index == len(input) ? result :
_parse_pairs(spec,input,grow,index+2,struct_set(result, input[index], input[index+1],grow)); _parse_pairs(spec,input,grow,index+2,struct_set(result, input[index], input[index+1],grow));
// Function: struct_remove() // Function: struct_remove()
// Usage: // Usage:
// struct_remove(struct, keyword) // struct_remove(struct, keyword)
// Description: // Description:
// Remove keyword or keyword list from a structure // Remove keyword or keyword list from a structure
// Arguments: // Arguments:
// struct = input structure // struct = input structure
// keyword = a single string (keyword) or list of strings (keywords) to remove // keyword = a single string (keyword) or list of strings (keywords) to remove
function struct_remove(struct, keyword) = function struct_remove(struct, keyword) =
is_string(keyword)? struct_remove(struct, [keyword]) : is_string(keyword)? struct_remove(struct, [keyword]) :
let(ind = search(keyword, struct)) let(ind = search(keyword, struct))
list_remove(struct, ind); list_remove(struct, ind);
// Function: struct_val() // Function: struct_val()
// Usage: // Usage:
// struct_val(struct,keyword) // struct_val(struct,keyword)
// Description: // Description:
// Returns the value for the specified keyword in the structure, or undef if the keyword is not present // Returns the value for the specified keyword in the structure, or undef if the keyword is not present
// Arguments: // Arguments:
// struct = input structure // struct = input structure
// keyword = keyword whose value to return // keyword = keyword whose value to return
function struct_val(struct,keyword) = function struct_val(struct,keyword) =
assert(is_def(keyword),"keyword is missing") assert(is_def(keyword),"keyword is missing")
let(ind = search([keyword],struct)[0]) let(ind = search([keyword],struct)[0])
ind == [] ? undef : struct[ind][1]; ind == [] ? undef : struct[ind][1];
// Function: struct_keys() // Function: struct_keys()
// Usage: // Usage:
// struct_keys(struct) // keys = struct_keys(struct)
// Description: // Description:
// Returns a list of the keys in a structure // Returns a list of the keys in a structure
// Arguments: // Arguments:
// struct = input structure // struct = input structure
function struct_keys(struct) = function struct_keys(struct) =
[for(entry=struct) entry[0]]; [for(entry=struct) entry[0]];
// Function&Module struct_echo()
// Function&Module: struct_echo()
// Usage: // Usage:
// struct_echo(struct, [name]) // struct_echo(struct, [name])
// Description: // Description:
// Displays a list of structure keywords and values, one pair per line, for easier reading. // Displays a list of structure keywords and values, one pair per line, for easier reading.
// Arguments: // Arguments:
// struct = input structure // struct = input structure
// name = optional structure name to list at the top of the output. Default: "" // name = optional structure name to list at the top of the output. Default: ""
function struct_echo(struct,name="") = function struct_echo(struct,name="") =
let( keylist = [for(entry=struct) str("  ",entry[0],": ",entry[1],"\n")]) let( keylist = [for(entry=struct) str("  ",entry[0],": ",entry[1],"\n")])
echo(str("\nStructure ",name,"\n",str_join(keylist))) echo(str("\nStructure ",name,"\n",str_join(keylist)))
undef; undef;
module struct_echo(struct,name="") { module struct_echo(struct,name="") {
dummy = struct_echo(struct,name); dummy = struct_echo(struct,name);
} }
// Function is_struct()
// Function: is_struct()
// Usage: // Usage:
// is_struct(struct) // is_struct(struct)
// Description: Returns true if the input has the form of a structure, false otherwise. // Description:
function is_struct(struct, ind=0) = // Returns true if the input has the form of a structure, false otherwise.
is_list(struct) && ( function is_struct(x) =
struct == [] || is_list(x) && [
let(dim = array_dim(struct)) for (xx=x) if(
len(dim)==2 && dim[1]==2 !is_list(xx) ||
); len(xx) != 2 ||
!is_string(xx[0])
) 1
] == [];
// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap

View file

@ -8,7 +8,7 @@
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
BOSL_VERSION = [2,0,310]; BOSL_VERSION = [2,0,311];
// Section: BOSL Library Version Functions // Section: BOSL Library Version Functions