blib/lib/String/Examine.pm

filter: Statementstmt 29.5% 18 / 61 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 4 / 12 Podpod 100% 8 / 8 totaltotal 29.7% 30 / 101 CC 13 SCAR 22.0
CC
13
Cov
23%
vector_compare
20.0
new
12.0
str_pos
12.0
CRAP 9.0
↑ Summary
1 package String::Examine;
2  
3 =head1 NAME
4  
5 String::Examine - String comparisions and offset checking
6  
7 =head1 SYNOPSIS
8
9 use String::Examine;
10  
11 my $str = String::Examine::new('string1' => 'foo', $string2 => 'bar');
12 my $match = $str->compare();
13 my $match2 = $str->nocase_compare();
14 my $match3 = $str->regex_compare();
15 my $match4 = $str->vector_compare();
16  
17 if($match || $match2 || $match3 || $match4)
18 {
19 my $offset = $str->str_pos();
20 }
21  
22 $str->string2("new_string");
23 $str->string1("new_string_also");
24  
25 =head1 DESCRIPTION
26  
27 This module does basic string comparision, string1 is always the primary that string2 is matched against
28 there is 4 builtin comparisions, 'compare()' which just does a basic eq/ne compare, 'nocase_compare()'
29 which converts to lowercase and does eq/ne match, 'regex_compare()' which is simply string matching using m//,
30 'vector_compare()' a slightly more unusual comparision, converts each character into vector and concatenates
31 the converted vectors and does a '==' there is instances on some strings where this has found a failure that
32 other compares failed to find, however, due to the nature it is the most expensive compare.
33  
34 The final function is 'str_pos()' in the event that a string doesn't match this function can be called to find
35 the position in the string2 where it failed, it is offset from 0, ie:
36  
37 string1 = 'aaaaba';
38 string2 = 'aaaaaa';
39  
40 str_pos() would return '4'.
41  
42 =cut
43  
44 1 ▶use 5.008007;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 require Exporter;
50  
51 1 @ISA = qw(Exporter);
52 1 @EXPORT_OK = qw/new string1 string2 compare nocase_compare regex_compare vector_compare str_pos/;
53 1 @EXPORT = qw//;
54 1 %EXPORT_TAGS = (all => [@EXPORT_OK]);
55 1 $VERSION = '0.01';
56  
57  
58 =head1 FUNCTIONS
59  
60 =head2 new
61  
62 my $str = String::Examine::new();
63  
64 You can pass the optional parameters 'string1', and 'string2' here.
65  
66 =cut
67  
68 sub new
69 0 ▶{
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
70 0 my $class = "String::Examine";
71 0 my %params = @_;
72 0 my $self = {};
73 0 ▶ $self->{'_string1'} = $params{'string1'} || undef;
Condition0%0/2
$params{'string1'} || undef
l!l
00
MC/DC0%0/1
$params{'string1'} || undef
$params{'string1'}
Truth table0%0/2
A
A= $params{'string1'}
Aresult
11
00
74 0 ▶ $self->{'_string2'} = $params{'string2'} || undef;
Condition0%0/2
$params{'string2'} || undef
l!l
00
MC/DC0%0/1
$params{'string2'} || undef
$params{'string2'}
Truth table0%0/2
A
A= $params{'string2'}
Aresult
11
00
75 0 bless $self, $class;
76 0 return $self;
77 }
78  
79 =head2 string1
80  
81 $str->string1("string");
82  
83 If a value is pass string1 will be set to the value if not it will just return the
84 currently set value.
85  
86 =cut
87  
88 sub string1
89 0 ▶{
Subroutine string1: not called CC=2 SCAR=17.9
Pod documented
90 0 my ($self, $new_val) = @_;
91 0 ▶ $self->{'_string1'} = $new_val if $new_val;
Branch: 1 branch
BranchTrueFalseTotal
if $new_val 0 0 0
92 0 return $self->{'_string1'};
93 }
94  
95 =head2 string2
96  
97 $str->string2("string");
98  
99 If a value is pass string2 will be set to the value if not it will just return the
100 currently set value.
101  
102 =cut
103  
104 sub string2
105 0 ▶{
Subroutine string2: not called CC=2 SCAR=17.9
Pod documented
106 0 my ($self, $new_val) = @_;
107 0 ▶ $self->{'_string2'} = $new_val if $new_val;
Branch: 1 branch
BranchTrueFalseTotal
if $new_val 0 0 0
108 0 return $self->{'_string2'};
109 }
110  
111 =head2 compare
112  
113 $str->compare();
114  
115 Performs a basic eq compare on string1 and string2, string1 is always primary,
116 returns 0 strings are equal or 1 if not
117  
118 =cut
119  
120 sub compare
121 0 ▶{
Subroutine compare: not called CC=2 SCAR=17.9
Pod documented
122 0 my $self = shift;
123
124 0 ▶ if($self->{'_string1'} eq $self->{'_string2'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_string1'} eq $self->{'_string2'}) { } 0 0 0
125 {
126 0 return 0;
127 }
128 else
129 {
130 0 return 1;
131 }
132 }
133  
134 =head2 nocase_compare
135  
136 $str->nocase_compare();
137  
138 Exactly as compare() except it performs lc() first.
139  
140 =cut
141  
142 sub nocase_compare
143 0 ▶{
Subroutine nocase_compare: not called CC=2 SCAR=17.9
Pod documented
144 0 my $self = shift;
145  
146 0 ▶ if(lc($self->{'_string1'}) eq lc($self->{'_string2'}))
Branch: 1 branch
BranchTrueFalseTotal
if (lc $self->{'_string1'} eq lc $self->{'_string2'}) { } 0 0 0
147 {
148 0 return 0;
149 }
150 else
151 {
152 0 return 1;
153 }
154 }
155  
156 =head2 regex_compare
157  
158 $str->regex_compare();
159  
160 Performs the compare based on a regular expression using string1 =~ m/string2/
161 returns 0 if they are equal or 1 if not.
162  
163 =cut
164  
165 sub regex_compare
166 0 ▶{
Subroutine regex_compare: not called CC=2 SCAR=17.9
Pod documented
167 0 my $self = shift;
168  
169 0 ▶ if($self->{'_string1'} =~ m/$self->{'_string2'}/)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_string1'} =~ /$self->{'_string2'}/) { } 0 0 0
170 {
171 0 return 0;
172 }
173 else
174 {
175 0 return 1;
176 }
177 }
178  
179 =head2 vector_compare
180  
181 $str->vector_compare();
182  
183 Converts the strings into 4bit vectors and performs a compare based on the concatenated output.
184  
185 =cut
186  
187 sub vector_compare
188 0 ▶{
Subroutine vector_compare: not called CC=4 SCAR=30.0
Pod documented
189 0 my $self = shift;
190 0 my ($vec1, $vec2);
191  
192 0 foreach my $c (split //, $self->{'_string1'})
193 {
194 0 $vec1 .= vec($c, 0, 4);
195 }
196
197 0 foreach my $c (split //, $self->{'_string2'})
198 {
199 0 $vec2 .= vec($c, 0, 4);
200 }
201  
202 0 ▶ if($vec1 == $vec2)
Branch: 1 branch
BranchTrueFalseTotal
if ($vec1 == $vec2) { } 0 0 0
203 {
204 0 return 0;
205 }
206 else
207 {
208 0 return 1;
209 }
210 }
211  
212 =head2 str_pos
213  
214 $str->str_pos();
215  
216 Returns the offset number of string2 at the point at which the 2 strings do not match, counts from 0.
217  
218 =cut
219  
220 sub str_pos
221 0 ▶{
Subroutine str_pos: not called CC=3 SCAR=24.8
Pod documented
222 0 my $self = shift;
223 0 my $strcount = 0;
224 0 my @str1 = split(//, $self->{'_string1'});
225 0 my @str2 = split(//, $self->{'_string2'});
226  
227 0 foreach my $char (@str1)
228 {
229 0 ▶ if($char ne $str2[$strcount])
Branch: 1 branch
BranchTrueFalseTotal
if ($char ne $str2[$strcount]) 0 0 0
230 {
231 0 return $strcount;
232 }
233 0 $strcount++;
234 }
235  
236 0 return -1;
237 }
238  
239 1 1;
240 __END__
↑ Summary