| 1 | package Data::Unixish::randstr; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use syntax 'each_on_array'; # to support perl < 5.12 | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | #use Log::Any '$log'; | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Data::Unixish::Util qw(%common_args); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 12 | 1 | our $DATE = '2025-02-24'; # DATE | |||||||||||||||||||||||||||
| 13 | 1 | our $DIST = 'Data-Unixish'; # DIST | |||||||||||||||||||||||||||
| 14 | 1 | our $VERSION = '1.574'; # VERSION | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | our %SPEC; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | my $def_charset = 'AZaz09'; | |||||||||||||||||||||||||||
| 19 | 1 | my %charsets = ( | |||||||||||||||||||||||||||
| 20 | '09' => '0123456789', | ||||||||||||||||||||||||||||
| 21 | 'AZ' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', | ||||||||||||||||||||||||||||
| 22 | 'az' => 'abcdefghijklmnopqrstuvwxyz', | ||||||||||||||||||||||||||||
| 23 | 'AZaz' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', | ||||||||||||||||||||||||||||
| 24 | 'AZaz09' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | 1 | my $charsets_desc = <<'MARKDOWN'; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | `az` is basic Latin lowercase letters. `AZ` uppercase letters. `AZaz` lowercase | ||||||||||||||||||||||||||||
| 29 | and uppercase letters. `AZaz09` lowercase + uppercase letters + Arabic numbers. | ||||||||||||||||||||||||||||
| 30 | `09` numbers. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | MARKDOWN | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | $SPEC{randstr} = { | ||||||||||||||||||||||||||||
| 35 | v => 1.1, | ||||||||||||||||||||||||||||
| 36 | summary => 'Generate a stream of random strings', | ||||||||||||||||||||||||||||
| 37 | args => { | ||||||||||||||||||||||||||||
| 38 | %common_args, | ||||||||||||||||||||||||||||
| 39 | min_len => { | ||||||||||||||||||||||||||||
| 40 | summary => 'Minimum possible length (inclusive)', | ||||||||||||||||||||||||||||
| 41 | schema => ['int*', min=>0, default=>16], | ||||||||||||||||||||||||||||
| 42 | cmdline_aliases => { a=>{} }, | ||||||||||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||||||||||
| 44 | max_len => { | ||||||||||||||||||||||||||||
| 45 | summary => 'Maximum possible length (inclusive)', | ||||||||||||||||||||||||||||
| 46 | schema => ['int*', min=>0, default=>16], | ||||||||||||||||||||||||||||
| 47 | cmdline_aliases => { | ||||||||||||||||||||||||||||
| 48 | b => {}, | ||||||||||||||||||||||||||||
| 49 | c => { | ||||||||||||||||||||||||||||
| 50 | summary => 'Set length (min_len and max_len)', | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 52 | 0 | my ($args, $val) = @_; | |||||||||||||||||||||||||||
| 53 | 0 | $args->{min_len} = $val; | |||||||||||||||||||||||||||
| 54 | 0 | $args->{max_len} = $val; | |||||||||||||||||||||||||||
| 55 | }, | ||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||
| 57 | }, | ||||||||||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||||||||||
| 59 | 1 | charset => { | |||||||||||||||||||||||||||
| 60 | summary => 'Character set to use', | ||||||||||||||||||||||||||||
| 61 | description => $charsets_desc, | ||||||||||||||||||||||||||||
| 62 | schema => ['str*', default=>$def_charset, | ||||||||||||||||||||||||||||
| 63 | in=>[sort keys %charsets]], | ||||||||||||||||||||||||||||
| 64 | }, | ||||||||||||||||||||||||||||
| 65 | num => { | ||||||||||||||||||||||||||||
| 66 | summary => 'Number of strings to generate, -1 means infinite', | ||||||||||||||||||||||||||||
| 67 | schema => ['int*', default=>1], | ||||||||||||||||||||||||||||
| 68 | cmdline_aliases => { n=>{} }, | ||||||||||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||||||||||
| 70 | }, | ||||||||||||||||||||||||||||
| 71 | tags => [qw/text gen-data/], | ||||||||||||||||||||||||||||
| 72 | 'x.dux.is_stream_output' => 1, # for duxapp < 1.41, will be removed later | ||||||||||||||||||||||||||||
| 73 | 'x.app.dux.is_stream_output' => 1, | ||||||||||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub randstr { | ||||||||||||||||||||||||||
|
Subroutine
randstr: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 77 | 0 | my ($in, $out) = ($args{in}, $args{out}); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # XXX schema | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my $min_len = $args{min_len} // 16; $min_len = 0 if $min_len < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$args{"min_len"} // 16
MC/DC0%0/1
$args{"min_len"} // 16
$args{"min_len"}
Truth table0%0/2
A
A= $args{"min_len"}
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | my $max_len = $args{max_len} // 16; $max_len = 0 if $max_len < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$args{"max_len"} // 16
MC/DC0%0/1
$args{"max_len"} // 16
$args{"max_len"}
Truth table0%0/2
A
A= $args{"max_len"}
| |||||||||||||||||||||||||||||
| 82 | 0 | my $charset = $args{charset}; | |||||||||||||||||||||||||||
| 83 | 0 | my $chars = $charsets{$charset}; | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | return [400, "Unknown charset"] unless defined($chars); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | my $len_chars = length($chars); | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | my $num = $args{num} // 1; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{"num"} // 1
MC/DC0%0/1
$args{"num"} // 1
$args{"num"}
Truth table0%0/2
A
A= $args{"num"}
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 89 | 0 | while (1) { | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | last if $num >= 0 && ++$i > $num; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$num >= 0 and ++$i > $num
MC/DC0%0/2
$num >= 0 and ++$i > $num
$num >= 0
++$i > $num
Truth table0%0/3
A and B
A= $num >= 0B= ++$i > $num
| |||||||||||||||||||||||||||||
| 91 | 0 | my $len = $min_len + int(rand()*($max_len-$min_len+1)); | |||||||||||||||||||||||||||
| 92 | 0 | my $rand = join "", map {substr($chars, $len_chars*rand(), 1)} 1..$len; | |||||||||||||||||||||||||||
| 93 | 0 | push @$out, $rand; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | [200, "OK"]; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | 1; | |||||||||||||||||||||||||||
| 100 | # ABSTRACT: Generate a stream of random strings | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | __END__ | ||||||||||||||||||||||||||||