Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Item12739: Merge TWiki's RANDSTRING() function.
Along with a unit test.
  • Loading branch information
gac410 committed Jan 3, 2015
1 parent 50415fd commit bd2b26f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
14 changes: 11 additions & 3 deletions SpreadSheetPlugin/data/System/SpreadSheetPlugin.txt
Expand Up @@ -414,7 +414,7 @@ This function has two modes of operation.
---+++ LISTRAND( list ) -- get one random element of a list
* Syntax: ==$LISTRAND( list )==
* Example: ==%<nop>CALC{"$LISTRAND(Apple, Orange, Apple, Kiwi)"}%== returns one of the four elements
* Related: =[[#FuncCOUNTITEMS][$COUNTITEMS()]]=, =[[#FuncCOUNTSTR][$COUNTSTR()]]=, =[[#FuncLIST][$LIST()]]=, =[[#FuncLISTIF][$LISTIF()]]=, =[[#FuncLISTITEM][$LISTITEM()]]=, =[[#FuncLISTMAP][$LISTMAP()]]=, =[[#FuncLISTSHUFFLE][$LISTSHUFFLE()]]=, =[[#FuncLISTSIZE][$LISTSIZE()]]=, =[[#FuncLISTSORT][$LISTSORT()]]=, =[[#FuncLISTUNIQUE][$LISTUNIQUE()]]=, =[[#FuncRAND][$RAND()]]=, =[[#FuncSUM][$SUM()]]=
* Related: =[[#FuncCOUNTITEMS][$COUNTITEMS()]]=, =[[#FuncCOUNTSTR][$COUNTSTR()]]=, =[[#FuncLIST][$LIST()]]=, =[[#FuncLISTIF][$LISTIF()]]=, =[[#FuncLISTITEM][$LISTITEM()]]=, =[[#FuncLISTMAP][$LISTMAP()]]=, =[[#FuncLISTSHUFFLE][$LISTSHUFFLE()]]=, =[[#FuncLISTSIZE][$LISTSIZE()]]=, =[[#FuncLISTSORT][$LISTSORT()]]=, =[[#FuncLISTUNIQUE][$LISTUNIQUE()]]=, =[[#FuncRAND][$RAND()]]=, =[[#FuncRAND][$RANDSTRING()]]=, =[[#FuncSUM][$SUM()]]=

#FuncLISTREVERSE
---+++ LISTREVERSE( list ) -- opposite order of a list
Expand All @@ -432,7 +432,7 @@ This function has two modes of operation.
---+++ LISTSHUFFLE( list ) -- shuffle element of a list in random order
* Syntax: ==$LISTSHUFFLE( list )==
* Example: ==%<nop>CALC{"$LISTSHUFFLE(Apple, Orange, Apple, Kiwi)"}%== returns the four elements in random order
* Related: =[[#FuncCOUNTITEMS][$COUNTITEMS()]]=, =[[#FuncCOUNTSTR][$COUNTSTR()]]=, =[[#FuncLIST][$LIST()]]=, =[[#FuncLISTIF][$LISTIF()]]=, =[[#FuncLISTITEM][$LISTITEM()]]=, =[[#FuncLISTMAP][$LISTMAP()]]=, =[[#FuncLISTRAND][$LISTRAND()]]=, =[[#FuncLISTSIZE][$LISTSIZE()]]=, =[[#FuncLISTSORT][$LISTSORT()]]=, =[[#FuncLISTUNIQUE][$LISTUNIQUE()]]=, =[[#FuncRAND][$RAND()]]=, =[[#FuncSUM][$SUM()]]=
* Related: =[[#FuncCOUNTITEMS][$COUNTITEMS()]]=, =[[#FuncCOUNTSTR][$COUNTSTR()]]=, =[[#FuncLIST][$LIST()]]=, =[[#FuncLISTIF][$LISTIF()]]=, =[[#FuncLISTITEM][$LISTITEM()]]=, =[[#FuncLISTMAP][$LISTMAP()]]=, =[[#FuncLISTRAND][$LISTRAND()]]=, =[[#FuncLISTSIZE][$LISTSIZE()]]=, =[[#FuncLISTSORT][$LISTSORT()]]=, =[[#FuncLISTUNIQUE][$LISTUNIQUE()]]=, =[[#FuncRAND][$RAND()]]=, =[[#FuncRAND][$RANDSTRING()]]=, =[[#FuncSUM][$SUM()]]=

#FuncLISTSORT
---+++ LISTSORT( list ) -- sort a list
Expand Down Expand Up @@ -569,6 +569,14 @@ This function has two modes of operation.
* Syntax: ==$RAND( max )==
* Related: =[[#FuncEVAL][$EVAL()]]=, =[[#FuncLISTRAND][$LISTRAND()]]=, =[[#FuncLISTSHUFFLE][$LISTSHUFFLE()]]=

#FuncRANDSTRING
---+++ RANDSTRING( set, format ) -- random string & password generator
* Generate a random string from a ==set== of characters; the set may contain sequences like ==a..z==; default is ==a..zA..Z0..9_==. The ==format== defines the string length or the output format; specify a number to indicate the length of the random string; default is ==8== characters. Alternatively, specify a format string with ==x== as placeholders for random characters, such ==xxxx-xxxx-xxxx-xxxx==.
* Syntax: ==$RANDSTRING( _set_, _format_ )==
* Example: ==%<nop>CALCULATE{$RANDSTRING()}%== returns a random string with 8 characters composed of alphanumeric characters and underscores
* Example: ==%<nop>CALCULATE{$RANDSTRING(A..NP..Z1..9, xxxx-xxxx-xxxx-xxxx)}%== returns four sets of random strings, separated by dashes, where each set has four characters composed of uppercase letters and numbers, excluding letter O and number 0
* Related: =[[#FuncINSERTSTRING][$INSERTSTRING()]]=, =[[#FuncSUBSTRING][$SUBSTRING()]]=, =[[#FuncLISTRAND][$LISTRAND()]]=, =[[#FuncLISTSHUFFLE][$LISTSHUFFLE()]]=, =[[#FuncRAND][$RAND()]]=, =[[#FuncRAND][$RANDSTRING()]]=, =[[#FuncREPEAT][$REPEAT()]]=

#FuncREPEAT
---+++ REPEAT( text, num ) -- repeat text a number of times
* Syntax: ==$REPEAT( text, num )==
Expand Down Expand Up @@ -887,7 +895,7 @@ Note that the =DONTSPACE= global preference overrides the =SPREADSHEETPLUGIN_DON
| Version: | %$VERSION% |
| Release: | %$RELEASE% |
| Change History: | <!-- specify latest version first -->&nbsp; |
| 31 Jan 2014: | Foswikitask:Item12739: fixed use of uninitialized value |
| 02 Jan 2015: | Foswikitask:Item12739: fixed use of uninitialized value, Add triple-quote for escaped strings, Add RANDSTRING() function |
| 05 Nov 2012: | Foswikitask:Item8417: add VarCALCULATE macro to be used within the normal macro evaluation order. (name chosen for comaptibility ) |
| 15 Oct 2012: | Foswikitask:Item12153: Use proper format of %<nop>SCRIPTURL% in CALC demo. |
| 06 Apr 2012: (1.1.6) | Foswikitask:Item11523 - support $comma, $sp in SUBSTITUTE, REPLACE.<br />\
Expand Down
44 changes: 44 additions & 0 deletions SpreadSheetPlugin/lib/Foswiki/Plugins/SpreadSheetPlugin/Calc.pm
Expand Up @@ -300,6 +300,7 @@ my $Function = {
$max = 1 if ( $max <= 0 );
rand($max);
},
RANDSTRING => \&_RANDSTRING,
REPEAT => \&_REPEAT,
REPLACE => \&_REPLACE,
RIGHT => sub {
Expand Down Expand Up @@ -1111,6 +1112,49 @@ sub _BITXOR {
return $result;
}

# =========================
sub _RANDSTRING {
my ($theAttr) = @_;
my ( $chars, $format ) = split( /,\s*/, $theAttr, 2 );
$chars = '' unless defined($chars);
$chars =~ s/(.)\.\.(.)/_expandRange($1, $2)/ge;
my @pool = split( //, $chars );
@pool = ( 'a' .. 'z', 'A' .. 'Z', '0' .. '9', '_' ) unless ( scalar @pool );
my $num = 0;
$format = '' unless defined($format);

if ( $format =~ m/^([0-9]*)$/ ) {
$num = _getNumber($format);
$num = 8 if ( $num < 1 );
$num = 1024 if ( $num > 1024 );
$format = 'x' x $num;
}
else {
$num = length($format);
}
my $result;
foreach my $ch ( split( //, $format ) ) {
if ( $ch eq 'x' ) {
$result .= $pool[ rand @pool ];
}
else {
$result .= $ch;
}
}
return $result;
}

# =========================
sub _expandRange {
my ( $lowCh, $highCh ) = @_;
my $text =
"$1$2"; # in case out of range, return just low char and high char
if ( ord $highCh > ord $lowCh ) {
$text = join( '', ( $lowCh .. $highCh ) );
}
return $text;
}

##########################
# DATE / TIME Functions
# #######################
Expand Down
Expand Up @@ -982,6 +982,20 @@ sub test_RAND {
}
}

sub test_RANDSTRING {
my ($this) = @_;
for ( 1 .. 20 ) {
$this->assert( length( $this->CALC('$RANDSTRING()') ) == 8 );
$this->assert(
$this->CALC('$RANDSTRING()') ne $this->CALC('$RANDSTRING()') );
$this->assert_matches(
qr/^[A-NP-Z1-9]{4},[A-NP-Z1-9]{4},[A-NP-Z1-9]{4},[A-NP-Z1-9]{4}$/,
$this->CALC(
"\$RANDSTRING(A..NP..Z1..9, '''xxxx,xxxx,xxxx,xxxx''')")
);
}
}

sub test_REPEAT {
my ($this) = @_;
$this->assert( $this->CALC('$REPEAT(/\, 5)') eq q{/\\/\\/\\/\\/\\} );
Expand Down

0 comments on commit bd2b26f

Please sign in to comment.