blib/lib/Search/demo.pl untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 69 CC 5 SCAR 23.0
CC
5
Cov
0%
off2lnr
20.0
lnr2off
6.0
new
2.0
CRAP 10.0
1 #! /usr/bin/perl -w
2 #
3 # (c) 2007, jw
4 # Distributable under the well known terms of GPLv2.
5  
6 use ExtUtils::testlib;
7 use Search::WuManber;
8 use Data::Dumper;
9 $Data::Dumper::Indent = 0;
10  
11 my $text = qq(
12 Linux kernel management style
13  
14 This is a short document describing the preferred (or made up, depending
15 on who you ask) management style for the linux kernel. It's meant to
16 mirror the CodingStyle document to some degree, and mainly written to
17 avoid answering (*) the same (or similar) questions over and over again.
18  
19 Management style is very personal and much harder to quantify than
20 simple coding style rules, so this document may or may not have anything
21 to do with reality. It started as a lark, but that doesn't mean that it
22 might not actually be true. You'll have to decide for yourself.
23  
24 Btw, when talking about "kernel manager", it's all about the technical
25 lead persons, not the people who do traditional management inside
26 companies. If you sign purchase orders or you have any clue about the
27 budget of your group, you're almost certainly not a kernel manager.
28 These suggestions may or may not apply to you.
29 );
30  
31 # if (open IN, "<", "/usr/src/linux/Documentation/ManagementStyle")
32 # {
33 # $text = join '', <IN>;
34 # close IN;
35 # }
36  
37 my $tln = Text::LineNumber->new($text);
38  
39 my @list = qw(Management DISTRIBUTIVE Algorithm equation Persons emEnt somebody suddenly dangerous preemptive decision person style);
40  
41 # @list = qw(1032104 2010321041 2032104 032104 22412430);
42 # $text = qq(100221201032104213044210);
43  
44 my $search = Search::WuManber->new(\@list, { case_sensitive => 0 });
45 $search->{return_string}++;
46  
47 warn Dumper $search->all($text);
48 # delete $search->{wm}; warn Dumper $search;
49  
50 while (defined (my $match = $search->next($text)))
51 {
52 push @$match, $list[$match->[1]], $tln->off2lnr($match->[0]);
53 print Dumper $match;
54 print "\n";
55 }
56  
57  
58 ## this demo.pl contains a builtin copy of Text::LineNumber module.
59 ## so that it is independant.
60 package Text::LineNumber;
61  
62 sub new
63 {
64 my ($self, $text) = @_;
65 my $class = ref($self) || $self;
66 my $lnr_off = [ 0 ];
67 while ($text =~ m{(\r\n|\n|\r)}gs)
68 {
69 # pos() returns the offset of the next character
70 # after the match -- exactly what we need here.
71 push @$lnr_off, pos $text;
72 }
73 return bless $lnr_off, $self;
74 }
75  
76 ## the first byte has offset 0
77 sub lnr2off
78 {
79 my ($self, $lnr) = @_;
80 my $l = 0;
81 return 0 if $lnr < 0;
82 return $self->[$lnr] || $self->[-1];
83 }
84  
85  
86 ## the first line has lnr 1,
87 ## the first byte in a line has column 1.
88 sub off2lnr
89 {
90 my ($self, $offset) = @_;
91 my $l = 0;
92 my $h = $#$self;
93 while ($h - $l > 1)
94 {
95 my $n = ($l + $h) >> 1;
96 if ($self->[$n] <= $offset)
97 {
98 $l = $n;
99 $h = $n if $self->[$l] == $offset;
100 }
101 else
102 {
103 $h = $n;
104 }
105 }
106
107 return $h unless wantarray;
108 return ($h, $offset - $self->[$l] + 1);
109 }
110  
111 # end package Text::LineNumber