line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Device::KeyStroke::Mobile; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
65291
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
82
|
|
4
|
2
|
|
|
2
|
|
11
|
use vars qw($VERSION @EXPORT $KeyMapping); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
891
|
|
5
|
|
|
|
|
|
|
$VERSION = 0.01; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
*import = \&Exporter::import; |
9
|
|
|
|
|
|
|
@EXPORT = qw(calc_keystroke); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$KeyMapping = { |
12
|
|
|
|
|
|
|
1 => '', |
13
|
|
|
|
|
|
|
2 => 'ABC', |
14
|
|
|
|
|
|
|
3 => 'DEF', |
15
|
|
|
|
|
|
|
4 => 'GHI', |
16
|
|
|
|
|
|
|
5 => 'JKL', |
17
|
|
|
|
|
|
|
6 => 'MNO', |
18
|
|
|
|
|
|
|
7 => 'PQRS', |
19
|
|
|
|
|
|
|
8 => 'TUV', |
20
|
|
|
|
|
|
|
9 => 'WXYZ', |
21
|
|
|
|
|
|
|
'*' => '.-@/', |
22
|
|
|
|
|
|
|
0 => '', |
23
|
|
|
|
|
|
|
'#' => '', |
24
|
|
|
|
|
|
|
}; |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
0
|
|
0
|
sub _croak { require Carp; Carp::croak(@_) } |
|
0
|
|
|
|
|
0
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub calc_keystroke { |
29
|
2
|
|
|
2
|
1
|
1625
|
my $text = uc(shift); |
30
|
2
|
|
|
|
|
11
|
my $lookup = _build_lookup($KeyMapping); # XXX need cache? but |
31
|
|
|
|
|
|
|
# mapping can be modified ... |
32
|
|
|
|
|
|
|
|
33
|
2
|
|
|
|
|
4
|
my $typing_times = 0; |
34
|
2
|
|
|
|
|
5
|
my $prev = ''; |
35
|
2
|
|
|
|
|
7
|
for my $i (0 .. length($text) - 1) { |
36
|
17
|
|
|
|
|
23
|
my $char = substr($text, $i, 1); |
37
|
17
|
50
|
|
|
|
41
|
my $table = $lookup->{$char} |
38
|
|
|
|
|
|
|
or _croak("don't know how to type $char"); |
39
|
17
|
|
|
|
|
18
|
my($time, $keypad) = @{$lookup->{$char}}; |
|
17
|
|
|
|
|
31
|
|
40
|
17
|
|
|
|
|
20
|
$typing_times += $time; |
41
|
17
|
100
|
|
|
|
52
|
$typing_times++ if $prev eq $keypad; # for ">" key |
42
|
17
|
|
|
|
|
29
|
$prev = $keypad; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
2
|
|
|
|
|
58
|
return $typing_times; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub _build_lookup { |
49
|
2
|
|
|
2
|
|
4
|
my $mapping = shift; |
50
|
2
|
|
|
|
|
4
|
my %lookup; |
51
|
2
|
|
|
|
|
11
|
while (my($key, $values) = each %$mapping) { |
52
|
24
|
|
|
|
|
52
|
for my $len (1..length($values)) { |
53
|
60
|
|
|
|
|
88
|
my $char = substr($values, $len - 1, 1); |
54
|
60
|
50
|
|
|
|
119
|
if (exists $lookup{$char}) { |
55
|
0
|
0
|
|
|
|
0
|
next if $len > $lookup{$char}->[0]; # already has shorter one |
56
|
|
|
|
|
|
|
} |
57
|
60
|
|
|
|
|
237
|
$lookup{$char} = [ $len, $key ]; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
2
|
|
|
|
|
9
|
return \%lookup; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
1; |
64
|
|
|
|
|
|
|
__END__ |