Add the "any" built-in function.

This commit is contained in:
Dianne Skoll
2021-12-07 09:22:02 -05:00
parent 4fa956aa27
commit 6295048b3e
4 changed files with 69 additions and 1 deletions

View File

@@ -2358,6 +2358,11 @@ $TimeSep and $DateTimeSep when formatting its output. For example:
.PP
.RE
.TP
.B any(arg1 [,arg2, ..., argN]);
Returns 1 if the first argument \fIarg1\fR is equal to any of the
subsequent arguments \fIarg2\fR through \fIargN\fR; returns 0 otherwise.
Also returns 0 if called with only one argument.
.TP
.B args(s_fname)
Returns the number of arguments expected by the user-defined function
\fIfname\fR, or \-1 if no such user-defined function exists. Note that

View File

@@ -58,6 +58,7 @@ static int FADusk (func_info *);
static int FAbs (func_info *);
static int FAccess (func_info *);
static int FAmpm (func_info *);
static int FAny (func_info *);
static int FArgs (func_info *);
static int FAsc (func_info *);
static int FBaseyr (func_info *);
@@ -209,6 +210,7 @@ BuiltinFunc Func[] = {
{ "adawn", 0, 1, 0, FADawn},
{ "adusk", 0, 1, 0, FADusk},
{ "ampm", 1, 3, 1, FAmpm },
{ "any", 1, NO_MAX, 1, FAny },
{ "args", 1, 1, 0, FArgs },
{ "asc", 1, 1, 1, FAsc },
{ "baseyr", 0, 0, 1, FBaseyr },
@@ -1033,6 +1035,36 @@ static int FPlural(func_info *info)
}
}
/***************************************************************/
/* */
/* FAny */
/* Return 1 if the first arg equals any subsequent arg, 0 */
/* otherwise. */
/* */
/***************************************************************/
static int FAny(func_info *info)
{
int i;
RetVal.type = INT_TYPE;
RETVAL = 0;
for (i=1; i<Nargs; i++) {
if (ARG(0).type == ARG(i).type) {
if (ARG(0).type == STR_TYPE) {
if (!strcmp(ARGSTR(0), ARGSTR(i))) {
RETVAL = 1;
return OK;
}
} else {
if (ARGV(0) == ARGV(i)) {
RETVAL = 1;
return OK;
}
}
}
}
return OK;
}
/***************************************************************/
/* */
/* FChoose */

View File

@@ -3268,12 +3268,32 @@ $IntMin => -2147483648
abs(-2147483648) => Number too high
../tests/test.rem(594): Number too high
# The "any" function
set a any(1)
any(1) => 0
set a any("foo")
any("foo") => 0
set a any(1:00)
any(01:00) => 0
set a any(1, 2)
any(1, 2) => 0
set a any("foo", 2)
any("foo", 2) => 0
set a any(1:00, 2)
any(01:00, 2) => 0
set a any(1, 2, 1, 3)
any(1, 2, 1, 3) => 1
set a any("foo", 2, 3, "foo")
any("foo", 2, 3, "foo") => 1
set a any(1:00, 2, "foo", 2:00, 1:00, 9:00)
any(01:00, 2, "foo", 02:00, 01:00, 09:00) => 1
# Shellescape
set a shellescape(" !\"#$%%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")
shellescape(" !"#$%%&'()*+,-./0123456789:;<=>?@ABCDEF"...) => "\ \!\"\#\$\%\%\&\'\(\)\*+,-./0123456789\"...
msg [a]
../tests/test.rem(599): Trig = Saturday, 16 February, 1991
../tests/test.rem(610): Trig = Saturday, 16 February, 1991
a => "\ \!\"\#\$\%\%\&\'\(\)\*+,-./0123456789\"...
\ \!\"\#\$\\\&\'\(\)\*+,-./0123456789\:\;\<=\>\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\\\]\^_\`abcdefghijklmnopqrstuvwxyz\{\|\}\~

View File

@@ -593,6 +593,17 @@ set a $IntMin * (-1)
set a (-1) * $IntMin
set a abs($IntMin)
# The "any" function
set a any(1)
set a any("foo")
set a any(1:00)
set a any(1, 2)
set a any("foo", 2)
set a any(1:00, 2)
set a any(1, 2, 1, 3)
set a any("foo", 2, 3, "foo")
set a any(1:00, 2, "foo", 2:00, 1:00, 9:00)
# Shellescape
set a shellescape(" !\"#$%%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")