File Coverage

blib/lib/Parse/RandGen/CharClass.pm
Criterion Covered Total %
statement 18 55 32.7
branch 3 24 12.5
condition 0 3 0.0
subroutine 6 9 66.6
pod 1 3 33.3
total 28 94 29.7


line stmt bran cond sub pod time code
1             # $Revision: #5 $$Date: 2005/08/31 $$Author: jd150722 $
2             ######################################################################
3             #
4             # This program is Copyright 2003-2005 by Jeff Dutton.
5             #
6             # This program is free software; you can redistribute it and/or modify
7             # it under the terms of either the GNU General Public License or the
8             # Perl Artistic License.
9             #
10             # This program is distributed in the hope that it will be useful,
11             # but WITHOUT ANY WARRANTY; without even the implied warranty of
12             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13             # GNU General Public License for more details.
14             #
15             # If you do not have a copy of the GNU General Public License write to
16             # the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
17             # MA 02139, USA.
18             ######################################################################
19              
20             package Parse::RandGen::CharClass;
21              
22             require 5.006_001;
23 4     4   25 use Carp;
  4         11  
  4         338  
24 4     4   21 use Parse::RandGen qw($Debug);
  4         8  
  4         406  
25 4     4   23 use strict;
  4         7  
  4         140  
26 4     4   22 use vars qw(@ISA $Debug);
  4         7  
  4         3762  
27             @ISA = ('Parse::RandGen::Condition');
28              
29             sub _newDerived {
30 3 50   3   15 my $self = shift or confess ("%Error: Cannot call without a valid object!");
31 3         7 my $type = ref($self);
32 3         34 my $elem = $self->element();
33 3 50       31 (ref($elem) eq "Regexp") or confess("%Error: CharClass element is not a regular expression (\"$elem\")! Must be a regular expression!");
34 3 50       920 ($elem =~ m/(\(\?[imsx]*-?[imsx]*\:)+((\[\^?.+\])|\.)/ ) or confess("%Error: CharClass element is malformed (\"$elem\")! Must be a regular expression matching a single character of a character class (e.g. (?-imsx:[^a-f\n]).");
35 0         0 $self->_buildCharset();
36             }
37              
38 3     3 0 14 sub isQuantSupported { return 1; }
39              
40             sub dump {
41 0 0   0 0   my $self = shift or confess ("%Error: Cannot call without a valid object!");
42 0           return ($self->element().$self->quant());
43             }
44              
45             sub pick {
46 0 0   0 1   my $self = shift or confess ("%Error: Cannot call without a valid object!");
47 0           my %args = ( match=>1, # Default is to pick matching data
48             @_ );
49              
50 0           my %result = $self->pickRepetitions(%args);
51 0           my $matchCnt = $result{matchCnt};
52 0           my $badOne = $result{badOne};
53              
54 0           my $min; my $max;
55 0           my $val = "";
56 0           for (my $i=0; $i < $matchCnt; $i++) {
57 0 0 0       if (defined($badOne) && ($i==$badOne)) {
58 0           $min = $self->{_charsetEndOffset};
59 0           $max = 256;
60             } else {
61 0           $min = 0;
62 0           $max = $self->{_charsetEndOffset};
63             }
64 0           my $chrOffset = $min + int(rand($max-$min));
65 0           $val .= substr($self->{_charset}, $chrOffset, 1);
66             }
67 0           my $elem = $self->element();
68 0 0         if ($Debug) {
69 0 0         print("Parse::RandGen::CharClass($elem)::pick(match=>$args{match}, matchCnt=>$matchCnt, badOne=>".(defined($badOne)?$badOne:"undef")
70             ." with value of ".$self->dumpVal($val)."\n");
71             }
72 0           return ($val);
73             }
74              
75             sub _buildCharset {
76 0 0   0     my $self = shift or confess ("%Error: Cannot call without a valid object!");
77 0           my $elem = $self->element();
78 0 0         ($elem =~ /(\[\^?.+\])|\./) or confess("%Error: CharClass element is malformed (\"$elem\")! Must be a regular expression looking character class (e.g. [^a-f\n]).");
79              
80 0           my $reCharSet = qr/$elem/;
81 0           my $strGood = "";
82 0           my $strBad = "";
83 0           foreach my $ord (0..255) {
84 0           my $char = chr($ord);
85 0 0         if ($char =~ $reCharSet) {
86 0           $strGood .= $char;
87             } else {
88 0           $strBad .= $char;
89             }
90             }
91 0           $self->{_charsetEndOffset} = length($strGood);
92 0           $self->{_charset} = ($strGood . $strBad);
93 0           my $len = length($self->{_charset});
94 0 0         ($len == 256) or confess("Charset length is $len (all charsets should be 256 characters)!\n");
95             }
96              
97             ######################################################################
98             #### Package return
99             1;
100             __END__