File Coverage

blib/lib/Locale/TextDomain/OO/Util/ExtractHeader.pm
Criterion Covered Total %
statement 28 28 100.0
branch 8 10 80.0
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 47 49 95.9


line stmt bran cond sub pod time code
1             package Locale::TextDomain::OO::Util::ExtractHeader; ## no critic (TidyCode)
2            
3 2     2   99872 use strict;
  2         15  
  2         62  
4 2     2   12 use warnings;
  2         4  
  2         49  
5 2     2   42 use 5.010;
  2         8  
6            
7 2     2   14 use Carp qw(confess);
  2         4  
  2         130  
8 2     2   1069 use English qw(-no_match_vars $EVAL_ERROR);
  2         5148  
  2         11  
9 2     2   826 use namespace::autoclean;
  2         17959  
  2         11  
10            
11             our $VERSION = '4.001';
12            
13             sub instance {
14 1     1 1 100 return __PACKAGE__;
15             }
16            
17             my $really_compile_formula = sub {
18             my ($formula) = @_;
19            
20             ## no critic (ComplexRegexes EnumeratedClasses EscapedMetacharacters)
21             $formula =~ m{
22             \A \s*+ (?&expr) \z
23             (?(DEFINE)
24             (?
25             (?>
26             (?> ~ | ! (?! = ) | - (?! [\-=] ) | \+ (?! [+=] ) ) \s*+
27             )*+
28            
29             (?>
30             [1-9] [0-9]*+ \b
31             |
32             0 0*+ \b
33             |
34             n \b
35             |
36             \( \s*+ (?&expr) \)
37             )
38             \s*+
39             )
40             (?
41             (?&term)
42             (?>
43             (?>
44             \? \s*+ (?&expr) :
45             |
46             \|\| | &&
47             |
48             == | !=
49             |
50             << (?! = ) | >> (?! = )
51             |
52             <= | >=
53             |
54             < (?! [<=] ) | > (?! [>=] )
55             |
56             - (?! [-=] )
57             |
58             \+ (?! [+=] )
59             |
60             \| (?! [|=] )
61             |
62             & (?! [&=] )
63             |
64             / (?! [/*=] )
65             |
66             [\^*%] (?! = )
67             )
68             \s*+
69             (?&term)
70             )*+
71             )
72             )
73             }xms
74             or confess "Invalid formula: $formula";
75             ## use critic (ComplexRegexes EnumeratedClasses EscapedMetacharacters)
76            
77             $formula =~ s{ \b n \b }{\$n}xmsg;
78 1     1   14 my $sub = eval "sub { my \$n = shift; use integer; 0 + ($formula) }" ## no critic (StringyEval)
  1         2  
  1         8  
79             or confess "Internal error: $formula: $EVAL_ERROR";
80            
81             return $sub;
82             };
83            
84             my %compiled_formula_cache;
85             my $compile_formula = sub {
86             my $formula = shift;
87            
88             return $compiled_formula_cache{$formula} ||= $really_compile_formula->($formula);
89             };
90            
91             sub extract_header_msgstr {
92 4     4 1 125 my ( undef, $header_msgstr ) = @_;
93            
94 4 100       28 defined $header_msgstr
95             or confess 'Header is not defined';
96             ## no critic (ComplexRegexes EnumeratedClasses)
97 3 100       33 my ( $plural_forms, $nplurals, $plural ) = $header_msgstr =~ m{
98             ^
99             Plural-Forms:
100             [ ]*
101             (
102             nplurals [ ]* [=] [ ]* ([0-9]+) [ ]* [;]
103             [ ]*
104             plural [ ]* [=] [ ]* ([^;\n]+) [ ]* [;]?
105             [ ]*
106             )
107             $
108             }xms
109             or confess 'Plural-Forms not found in header';
110             ## use critic (ComplexRegexes EnumeratedClasses)
111 2 100       21 my ( $charset ) = $header_msgstr =~ m{
112             ^
113             Content-Type:
114             [^;]+ [;] [ ]*
115             charset [ ]* = [ ]*
116             ( [^ ]+ )
117             [ ]*
118             $
119             }xms
120             or confess 'Content-Type with charset not found in header';
121 1         6 my ( $lexicon_class ) = $header_msgstr =~ m{
122             ^ X-Lexicon-Class: \s* ( \S* ) \s* $
123             }xms;
124             # ToDo: remove because multiplural was a too complicated idea
125             ## no critic (EnumeratedClasses)
126 1         3 my ( $multiplural_nplurals ) = $header_msgstr =~ m{
127             ^ X-Multiplural-Nplurals: [ ]* ( [0-9]+ ) [ ]* $
128             }xms;
129             ## use critic (EnumeratedClasses)
130            
131             return {(
132 1 50       4 nplurals => 0 + $nplurals,
    50          
133             plural => $plural,
134             plural_code => $compile_formula->($plural),
135             charset => $charset,
136             ! $lexicon_class ? () : (
137             lexicon_class => $lexicon_class,
138             ),
139             # ToDo: remove because multiplural was a too complicated idea
140             ! $multiplural_nplurals ? () : (
141             multiplural_nplurals => $multiplural_nplurals,
142             ),
143             )};
144             }
145            
146             1;
147            
148             __END__