blib/lib/XML/Compile/Util.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 42 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 153 CC 8 SCAR 14.8
CC
8
Cov
0%
pack_type
12.0
unpack_type
6.0
odd_elements
6.0
CRAP 4.4
1 # Copyrights 2006-2018 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution XML-Compile. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package XML::Compile::Util;
10 use vars '$VERSION';
11 $VERSION = '1.61';
12  
13 use base 'Exporter';
14  
15 use warnings;
16 use strict;
17  
18 my @constants = qw/XMLNS SCHEMA1999 SCHEMA2000 SCHEMA2001 SCHEMA2001i/;
19 our @EXPORT = qw/pack_type unpack_type/;
20 our @EXPORT_OK =
21 ( qw/pack_id unpack_id odd_elements even_elements type_of_node
22 escape duration2secs add_duration/
23 , @constants
24 );
25 our %EXPORT_TAGS = (constants => \@constants);
26  
27 use constant
28 { XMLNS => 'http://www.w3.org/XML/1998/namespace'
29 , SCHEMA1999 => 'http://www.w3.org/1999/XMLSchema'
30 , SCHEMA2000 => 'http://www.w3.org/2000/10/XMLSchema'
31 , SCHEMA2001 => 'http://www.w3.org/2001/XMLSchema'
32 , SCHEMA2001i => 'http://www.w3.org/2001/XMLSchema-instance'
33 };
34  
35 use Log::Report 'xml-compile';
36 use POSIX qw/mktime/;
37  
38  
39 sub pack_type($;$)
40 { @_==1 ? $_[0]
41 : !defined $_[0] || !length $_[0] ? $_[1]
42 : "{$_[0]}$_[1]"
43 }
44  
45  
46 sub unpack_type($) { $_[0] =~ m/^\{(.*?)\}(.*)$/ ? ($1, $2) : ('', $_[0]) }
47  
48  
49 sub pack_id($$) { "$_[0]#$_[1]" }
50  
51  
52 sub unpack_id($) { split /\#/, $_[0], 2 }
53  
54  
55 sub odd_elements(@) { my $i = 0; map {$i++ % 2 ? $_ : ()} @_ }
56 sub even_elements(@) { my $i = 0; map {$i++ % 2 ? () : $_} @_ }
57  
58  
59 sub type_of_node($)
60 { my $node = shift or return ();
61 pack_type $node->namespaceURI, $node->localName;
62 }
63  
64  
65 use constant SECOND => 1;
66 use constant MINUTE => 60 * SECOND;
67 use constant HOUR => 60 * MINUTE;
68 use constant DAY => 24 * HOUR;
69 use constant MONTH => 30.4 * DAY;
70 use constant YEAR => 365.256 * DAY;
71  
72 my $duration = qr/
73 ^ (\-?) P (?:([0-9]+)Y)? (?:([0-9]+)M)? (?:([0-9]+)D)?
74 (?:T (?:([0-9]+)H)? (?:([0-9]+)M)? (?:([0-9]+(?:\.[0-9]+)?)S)?
75 )?$/x;
76  
77 sub duration2secs($)
78 { my $stamp = shift or return undef;
79  
80 $stamp =~ $duration
81 or error __x"illegal duration format: {d}", d => $stamp;
82  
83 ($1 eq '-' ? -1 : 1)
84 * ( ($2 // 0) * YEAR
85 + ($3 // 0) * MONTH
86 + ($4 // 0) * DAY
87 + ($5 // 0) * HOUR
88 + ($6 // 0) * MINUTE
89 + ($7 // 0) * SECOND
90 );
91 }
92  
93  
94 sub add_duration($;$)
95 { my $stamp = shift or return;
96 my ($secs, $min, $hour, $mday, $mon, $year) = gmtime(shift // time);
97  
98 $stamp =~ $duration
99 or error __x"illegal duration format: {d}", d => $stamp;
100  
101 my $sign = $1 eq '-' ? -1 : 1;
102 mktime
103 $secs + $sign*($7//0)
104 , $min + $sign*($6//0)
105 , $hour + $sign*($5//0)
106 , $mday + $sign*($4//0)
107 , $mon + $sign*($3//0)
108 , $year + $sign*($2//0)
109 }
110 1;