blib/lib/String/Random/Regexp/regxstring.pm

filter: Statementstmt 94.7% 36 / 38 Branchbran 87.5% 7 / 8 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 91.5% 54 / 59 CC 7 SCAR 15.2
CC
7
Cov
92%
generate_random_strings
7.1
BEGIN
1.0
BEGIN
1.0
CRAP 4.6
↑ Summary regxstring.cpp »
1 package String::Random::Regexp::regxstring;
2  
3 # we rely on having T_AVREF_REFCOUNT_FIXED
4 6 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 our $VERSION = '1.04';
10  
11 6 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 # export our only sub by default:
13 6 our @ISA = qw(Exporter);
14 6 our @EXPORT = ('generate_random_strings');
15 # load XS functions
16 6 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 XSLoader::load(__PACKAGE__, $VERSION);
18  
19 # It generates $N random strings given a regular expression.
20 # $regx is a string containing a regular expression
21 # or a Regexp object constructed, e.g., via qr/.../
22 # $N is optional, it is the number of random strings to generate, default is 1.
23 # $debug is optional, it can be 0 or 1 denoting name-like behaviour.
24 # The sub returns undef on failure (e.g. bad parameters)
25 # On success, it returns an array of $N random strings
26 184 ▶sub generate_random_strings {
Subroutine generate_random_strings: called CC=7 SCAR=19.6
Pod documented
27 184 my ($regx, $N, $debug) = @_;
28  
29 184 ▶ if( ! defined $regx ){ print STDERR "generate_random_strings() : error, at least an input regular expression (either as a string or as a Regexp object) is expected.\n"; return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $regx) 1 183 184
30  
31 183 my $regx_str;
32 183 ▶ if( ref($regx) eq '' ){ $regx_str = $regx }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $regx eq '') { } 95 88 183
elsif (ref $regx eq 'Regexp') { } 88 0 88
33 elsif( ref($regx) eq 'Regexp' ){
34 # stringifying a regexp object results in the string regexp
35 88 $regx_str = "".$regx;
36 # perl -e 'print qr/^(abc)/' prints (?^:^(abc)) and we dont like it
37 # sometimes (?^u:...)
38 # TODO: do this properly!!!! without subst hack
39 88 $regx_str =~ s/^\(\?\^.*?\://;
40 88 $regx_str =~ s/\)$//;
41 0 } else { print STDERR "generate_random_strings() : error, input regular expression must either be a string or a Regexp object (created via qr/.../) and not '".ref($regx)."'.\n"; return undef }
42  
43 # default values if optional params are not specified:
44 183 ▶ $N //= 1;
Condition50%1/2
$N //= 1
l!l
1830
MC/DC0%0/1
$N //= 1
$N
Truth table50%1/2
A
A= $N
Aresult
11
00
45 183 ▶ $debug //= 0;
Condition100%2/2
$debug //= 0
l!l
1785
MC/DC100%1/1
$debug //= 0
$debug
Truth table100%2/2
A
A= $debug
Aresult
11
00
46  
47 183 my $ret = generate_random_strings_xs(
48 $regx_str,
49 $N,
50 $debug
51 );
52 183 ▶ if( ! defined $ret ){ print STDERR "generate_random_strings() : error, call to XS code 'generate_random_strings_xs()' has failed.\n"; return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret) 2 181 183
53 181 return $ret
54 }
55  
56  
57 =pod
58  
59 =head1 NAME
60  
61 String::Random::Regexp::regxstring - Generate random strings from a regular expression
62  
63 =head1 VERSION
64  
65 Version 1.04
66  
67 =head1 SYNOPSIS
68  
69 This module provides functionality for generating random strings from a
70 regular expression by bridging
71 to the L<regxstring C++ library by daidodo|https://github.com/daidodo/regxstring>
72 via XS.
73  
74 use String::Random::Regexp::regxstring;
75  
76 my $strings = generate_random_strings(
77 '^([A-Z]|[0-9]){10}\d{5}xxx(\d{3})?',
78 3
79 );
80 # generates 3 random strings based on the regexp
81 # 3F3YR2W22947580xxx
82 # N5HHM8LW0K59719xxx957
83 # G2DQL6JF1E91086xxx
84  
85 # or provide it with a Regexp object
86 my $strings = generate_random_strings(
87 qr/^([A-Z]|[0-9]){10}\d{5}xxx(\d{3})?/,
88 3
89 );
90  
91 # or enable debug
92 my $strings = generate_random_strings(
93 qr/^([A-Z]|[0-9]){10}\d{5}xxx(\d{3})?/,
94 3,
95 1
96 );
97  
98 =head1 EXPORT
99  
100 =over 4
101  
102 =item * C<generate_random_strings> : generates random strings. This sub is exported by default.
103  
104 =back
105  
106 =head1 SUBROUTINES
107  
108  
109 =head2 C<generate_random_strings>
110  
111 my $strings = generate_random_strings($regexp [, $N, $debug])
112  
113  
114 Arguments:
115  
116 =over 4
117  
118 =item * C<$regexp> : a regular expression either as a string
119 or as a Regexp object created via e.g. C<qr/.../>
120  
121 =item * C<$N> : optionally specify the number of random strings to generate. Default is 1.
122  
123 =item * C<$debug> : optionally enable debug, if set to 1. By default it is turned off.
124  
125 =back
126  
127 Given a regular expression, this subroutine will generate
128 C<$N> random strings which are guaranteed to be matched by
129 the specified regular expression.
130  
131 The generated random strings will be returned back as an ARRAY ref.
132  
133 C<undef> is returned on error, e.g. when no regular expression was
134 specified or when the number of random strings to generate is not positive.
135  
136 =head1 THE C++ LIBRARY regxstring by daidodo
137  
138 This is a L<regxstring C++ library by daidodo|https://github.com/daidodo/regxstring>
139 which produces random strings from a regular expresssion. According to the
140 author, "I<... most Perl 5 supported regular expressions are also supported by regxstring, as showing bellow:>"
141  
142 Meta-character(s) Description
143 --------------------------------
144 \ Quote the next meta-character
145 ^ Match the beginning of the line
146 $ Match the end of the line (or before newline at the end)
147 ? Match 1 or 0 times
148 + Match 1 or more times
149 * Match 0 or more times
150 {n} Match exactly n times
151 {n,} Match at least n times
152 {n,m} Match at least n but not more than m times
153 . Match any character (except newline)
154 (pattern) Grouping
155 (?:pattern) This is for clustering, not capturing; it groups sub-expressions like "()", but doesn't make back-references as "()" does
156 (?=pattern) A zero-width positive look-ahead assertion, e.g., \w+(?=\t) matches a word followed by a tab, without including the tab
157 (?!pattern) A zero-width negative look-ahead assertion, e.g., foo(?!bar) matches any occurrence of "foo" that isn't followed by "bar"
158 | Alternation
159 [xyz] Matches a single character that is contained within the brackets
160 [^xyz] Matches a single character that is not contained within the brackets
161 [a-z] Matches a single character that is in a given range
162 [^a-z] Matches a single character that is not in a given range
163 \f Form feed
164 \n Newline
165 \r Return
166 \t Tab
167 \v Vertical white space
168 \d Digits, [0-9]
169 \D Non-digits, [^0-9]
170 \s Space and tab, [ \t\r\n\f]
171 \S Non-white space characters, [^ \t\r\n\f]
172 \w Alphanumeric characters plus '_', [0-9a-zA-Z_]
173 \W Non-word characters, [^0-9a-zA-Z_]
174 \N Matches what the Nth marked sub-expression matched, where N is a digit from 1 to 9
175  
176 The library provides an executable which may be run from the command line.
177 It takes a regular expression
178 from the standard input
179 and dumps the random strings.
180  
181 =head1 ALTERNATIVES
182  
183 There are at least two alternative modules at CPAN which I have tested.
184  
185 L<String::Random> and L<Regexp::Genex>. Both fail with rudimentary
186 regular expressions.
187  
188 The former does not support groups and therefore
189 all parentheses have to be removed from the regular expression first.
190 But this is not a trivial task. For example:
191  
192 use String::Random qw/random_regex/;
193 print random_regex('[A-HN-SW]\d{7}[A-J]ES[A-HN-SW]\d{7}[A-J](?:xx)?');
194 # '(' not implemented. treating literally.
195  
196 The latter fails randomly on large regular expressions, e.g. C<[A-HN-SW]\d{7}[A-J]xxx>
197 but succeeds with the shorter C<[A-HN-SW]\d{7}[A-J]>
198  
199  
200 =head1 AUTHOR
201  
202 Andreas Hadjiprocopis, C<< <bliako at cpan.org> >>
203  
204 =head1 DEDICATIONS
205  
206 !Almaz!
207  
208 =head1 CAVEATS
209  
210 The XS function for generating random strings accepts
211 the input regular expression as a string. This means
212 that if a Regexp object was supplied to L<generate_random_strings>,
213 the regular expression as a string must be extracted. And this
214 is done by stringifying the Regexp object, e.g. C<my $str = "".qr/abc/>
215 However, the stringification encloses the regular expression within
216 a C<(?^:> and C<)>. For example:
217  
218 print "".qr/^(abc)/
219 # prints (?^:^(abc))
220  
221 Currently, the subroutine will remove this "enclosure".
222 It remains to be seen whether this is 100% successful.
223  
224 I have not tested the statistical distribution of the results in
225 regular expressions like C<a|b|c|d>. They must appear equally often.
226  
227  
228 =head1 BUGS
229  
230 Please report any bugs or feature requests to C<bug-string-random-regexp-regxstring at rt.cpan.org>, or through
231 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=String-Random-Regexp-regxstring>. I will be notified, and then you'll
232 automatically be notified of progress on your bug as I make changes.
233  
234 =head1 SEE ALSO
235  
236 L<Mock::Data::Regex> which is implemented in Pure-Perl.
237  
238  
239 =head1 SUPPORT
240  
241 You can find documentation for this module with the perldoc command.
242  
243 perldoc String::Random::Regexp::regxstring
244  
245  
246 You can also look for information at:
247  
248 =over 4
249  
250 =item * RT: CPAN's request tracker (report bugs here)
251  
252 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=String-Random-Regexp-regxstring>
253  
254 =item * Review this module at PerlMonks
255  
256 L<https://perlmonks.org/?node_id=11160309>
257  
258 =item * Search CPAN
259  
260 L<https://metacpan.org/release/String-Random-Regexp-regxstring>
261  
262 =back
263  
264  
265 =head1 ACKNOWLEDGEMENTS
266  
267 The core functionality to this module is provided by the C++
268 library for generating random strings from regular expressions
269 located at L<https://github.com/daidodo|https://github.com/daidodo>.
270 The author is DoZerg / daidodo. The Licence is Apache v2.0.
271  
272 The source code of this library is included in the current module.
273  
274 I have provided C++ harness code, the XS interface and the Perl module.
275  
276  
277 =head1 LICENSE AND COPYRIGHT
278  
279 This software (except the C++ files) is Copyright (c) 2024 by Andreas Hadjiprocopis.
280  
281 This is free software, licensed under:
282  
283 The Artistic License 2.0 (GPL Compatible)
284  
285 The C++ files are Copyright (c) by L<daidodo|https://github.com/daidodo> and are L<licensed under Apache v2.0|https://github.com/daidodo/regxstring/blob/master/LICENSE>.
286  
287  
288 =cut
289  
290 6 1; # End of String::Random::Regexp::regxstring
↑ Summary regxstring.cpp »