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

@ -27,17 +27,19 @@
// 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.
// Arguments:
// struct = input structure
// keyword = keyword to set
// value = value to set the keyword to
// struct = Input structure.
// keyword = Keyword to set.
// 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
function struct_set(struct, keyword, value=undef, grow=true) =
!is_list(keyword) ?
!is_list(keyword)? (
let( ind=search([keyword],struct,1,0)[0] )
(ind==[] ? assert(grow,str("Unknown keyword \"",keyword))
concat(struct, [[keyword,value]]) :
list_set(struct, [ind], [[keyword,value]])) :
_parse_pairs(struct,keyword,grow);
ind==[]? (
assert(grow,str("Unknown keyword \"",keyword))
concat(struct, [[keyword,value]])
) : list_set(struct, [ind], [[keyword,value]])
) : _parse_pairs(struct,keyword,grow);
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")
@ -45,6 +47,7 @@ function _parse_pairs(spec, input, grow=true, index=0, result=undef) =
index == len(input) ? result :
_parse_pairs(spec,input,grow,index+2,struct_set(result, input[index], input[index+1],grow));
// Function: struct_remove()
// Usage:
// struct_remove(struct, keyword)
@ -58,6 +61,7 @@ function struct_remove(struct, keyword) =
let(ind = search(keyword, struct))
list_remove(struct, ind);
// Function: struct_val()
// Usage:
// struct_val(struct,keyword)
@ -71,9 +75,10 @@ function struct_val(struct,keyword) =
let(ind = search([keyword],struct)[0])
ind == [] ? undef : struct[ind][1];
// Function: struct_keys()
// Usage:
// struct_keys(struct)
// keys = struct_keys(struct)
// Description:
// Returns a list of the keys in a structure
// Arguments:
@ -81,7 +86,8 @@ function struct_val(struct,keyword) =
function struct_keys(struct) =
[for(entry=struct) entry[0]];
// Function&Module struct_echo()
// Function&Module: struct_echo()
// Usage:
// struct_echo(struct, [name])
// Description:
@ -98,13 +104,21 @@ module struct_echo(struct,name="") {
dummy = struct_echo(struct,name);
}
// Function is_struct()
// Function: is_struct()
// Usage:
// is_struct(struct)
// Description: Returns true if the input has the form of a structure, false otherwise.
function is_struct(struct, ind=0) =
is_list(struct) && (
struct == [] ||
let(dim = array_dim(struct))
len(dim)==2 && dim[1]==2
);
// Description:
// Returns true if the input has the form of a structure, false otherwise.
function is_struct(x) =
is_list(x) && [
for (xx=x) if(
!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