Commit | Line | Data |
a0d0e21e |
1 | package Search::Dict; |
2 | require 5.000; |
3 | require Exporter; |
4 | |
b75c8c73 |
5 | use strict; |
6 | |
7 | our $VERSION = '1.00'; |
8 | our @ISA = qw(Exporter); |
9 | our @EXPORT = qw(look); |
a0d0e21e |
10 | |
5be1dfc7 |
11 | =head1 NAME |
a0d0e21e |
12 | |
5be1dfc7 |
13 | Search::Dict, look - search for key in dictionary file |
14 | |
15 | =head1 SYNOPSIS |
16 | |
17 | use Search::Dict; |
18 | look *FILEHANDLE, $key, $dict, $fold; |
19 | |
20 | =head1 DESCRIPTION |
21 | |
22 | Sets file position in FILEHANDLE to be first line greater than or equal |
23 | (stringwise) to I<$key>. Returns the new file position, or -1 if an error |
24 | occurs. |
25 | |
26 | The flags specify dictionary order and case folding: |
27 | |
28 | If I<$dict> is true, search by dictionary order (ignore anything but word |
29 | characters and whitespace). |
30 | |
31 | If I<$fold> is true, ignore case. |
32 | |
33 | =cut |
a0d0e21e |
34 | |
35 | sub look { |
b75c8c73 |
36 | my($fh,$key,$dict,$fold) = @_; |
5be1dfc7 |
37 | local($_); |
b75c8c73 |
38 | my(@stat) = stat($fh) |
5be1dfc7 |
39 | or return -1; |
40 | my($size, $blksize) = @stat[7,11]; |
41 | $blksize ||= 8192; |
a0d0e21e |
42 | $key =~ s/[^\w\s]//g if $dict; |
df76f08a |
43 | $key = lc $key if $fold; |
5be1dfc7 |
44 | my($min, $max, $mid) = (0, int($size / $blksize)); |
a0d0e21e |
45 | while ($max - $min > 1) { |
46 | $mid = int(($max + $min) / 2); |
b75c8c73 |
47 | seek($fh, $mid * $blksize, 0) |
5be1dfc7 |
48 | or return -1; |
b75c8c73 |
49 | <$fh> if $mid; # probably a partial line |
50 | $_ = <$fh>; |
a0d0e21e |
51 | chop; |
52 | s/[^\w\s]//g if $dict; |
df76f08a |
53 | $_ = lc $_ if $fold; |
5be1dfc7 |
54 | if (defined($_) && $_ lt $key) { |
a0d0e21e |
55 | $min = $mid; |
56 | } |
57 | else { |
58 | $max = $mid; |
59 | } |
60 | } |
61 | $min *= $blksize; |
b75c8c73 |
62 | seek($fh,$min,0) |
5be1dfc7 |
63 | or return -1; |
b75c8c73 |
64 | <$fh> if $min; |
5be1dfc7 |
65 | for (;;) { |
b75c8c73 |
66 | $min = tell($fh); |
67 | defined($_ = <$fh>) |
5be1dfc7 |
68 | or last; |
a0d0e21e |
69 | chop; |
70 | s/[^\w\s]//g if $dict; |
df76f08a |
71 | $_ = lc $_ if $fold; |
a0d0e21e |
72 | last if $_ ge $key; |
a0d0e21e |
73 | } |
b75c8c73 |
74 | seek($fh,$min,0); |
a0d0e21e |
75 | $min; |
76 | } |
77 | |
78 | 1; |