Skip to content

Commit

Permalink
Add is_choice_of function
Browse files Browse the repository at this point in the history
  • Loading branch information
stdweird committed Feb 8, 2016
1 parent 9333651 commit 853d091
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
57 changes: 56 additions & 1 deletion pan/functions.pan
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,63 @@ function domain_from_object = {
}
function full_hostname_from_object = {
# Check cardinality; leave detailed error checking to called functions.
if (ARGC != 1) error("usage: full_hostname_from_object(default_domain) requires default_domian as argument");
if (ARGC != 1) error("usage: full_hostname_from_object(default_domain) requires default_domain as argument");

host_domain = host_domain_from_object(ARGV[0]);
format("%s.%s", host_domain[0], host_domain[1]);
};

@documentation{
Test if (all elements of) the first argument are
any of the remaining arguments.
(If the first argument is a list, all its elements are tested).
Example:
is_choice_of(1, 1, 2, 3) returns true
is_choice_of(1, 2, 3) returns false
is_choice_of(list("b", "c"), "a", "b", "c") returns true
is_choice_of(list("a", "d"), "a", "b", "c") returns false
The types are not checked and are assumed to be comparable.
is_choice_of("1", 1, 2, 3) will give a panc error (string vs long),
but is_choice_of(1.0, 1, 2, 3) is true (float vs long)
}
function is_choice_of = {
if (ARGC < 2) {
error("usage: is_choice_of() requires at least 2 arguments");
};

# TODO: is it faster to make a list of ARGV and use index function?
if (is_list(ARGV[0])) {
foreach(idx; el; ARGV[0]) {
match = false;
i = 1;
while (i<ARGC) {
if (el == ARGV[i]) {
match = true;
# no need to check others
i = ARGC;
} else {
i = i + 1;
};
};

if (! match) {
# Current element does not match
# No reason to continue
return(false);
};
};
return(true);
} else {
i = 1;
while (i<ARGC) {
if (ARGV[0] == ARGV[i]) {
return(true);
};
i = i + 1;
};
};
false;
};
18 changes: 18 additions & 0 deletions unittests/compile/pan/functions/is_choice_of.pan
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
object template is_choice_of;

include 'pan/functions';

"/a" = is_choice_of(1, 2 ,3);
"/b" = is_choice_of(1, 2 ,3, 1);
"/c" = is_choice_of(1.0, 2 ,3, 1);

"/la" = is_choice_of(list(1), 2 ,3);
"/lb" = is_choice_of(list(1), 2 ,3, 1);
"/lc" = is_choice_of(list(1.0), 2 ,3, 1);

"/ld" = is_choice_of(list(2, 1), 2 ,3, 1);
"/le" = is_choice_of(list(1, 3, 2), 2 ,3, 1);

# list examples
"/lf" = is_choice_of(list("b", "c"), "a", "b", "c");
"/lg" = is_choice_of(list("a", "d"), "a", "b", "c");
12 changes: 12 additions & 0 deletions unittests/compile_result/pan/functions/is_choice_of.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?><nlist format="pan" name="profile">
<boolean name="a">false</boolean>
<boolean name="b">true</boolean>
<boolean name="c">true</boolean>
<boolean name="la">false</boolean>
<boolean name="lb">true</boolean>
<boolean name="lc">true</boolean>
<boolean name="ld">true</boolean>
<boolean name="le">true</boolean>
<boolean name="lf">true</boolean>
<boolean name="lg">false</boolean>
</nlist>

0 comments on commit 853d091

Please sign in to comment.