| 1 | package Text::VCardFast; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Encode qw(decode_utf8 encode_utf8); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use MIME::Base64 qw(decode_base64 encode_base64); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 5 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | # Items to export into callers namespace by default. Note: do not export | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # Do not simply export all your public functions/methods/constants. | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | # This allows declaration use Text::VCardFast ':all'; | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||||||||||||||||||||||||||||||||||||||
| 19 | # will save memory. | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 5 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||||||||||||||||||||||||||||||||
| 21 | vcard2hash | ||||||||||||||||||||||||||||||||||||||||||
| 22 | hash2vcard | ||||||||||||||||||||||||||||||||||||||||||
| 23 | ) ] ); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 5 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 5 | our @EXPORT = qw( | |||||||||||||||||||||||||||||||||||||||||
| 28 | vcard2hash | ||||||||||||||||||||||||||||||||||||||||||
| 29 | hash2vcard | ||||||||||||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 5 | our $VERSION = '0.12'; | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 5 | require XSLoader; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 5 | XSLoader::load('Text::VCardFast', $VERSION); | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | # public API | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 5 | ▶ | sub vcard2hash { &vcard2hash_c } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
vcard2hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 15 | ▶ | sub hash2vcard { &hash2vcard_pp } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hash2vcard: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | # Implementation | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 31 | ▶ | sub vcard2hash_c { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
vcard2hash_c: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 31 | my $vcard = shift; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 31 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 31 | ▶ | if (utf8::is_utf8($vcard)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 24 | utf8::encode($vcard); | |||||||||||||||||||||||||||||||||||||||||
| 49 | 24 | $params{is_utf8} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 31 | ▶ | unless ($vcard =~ m/\n\S/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | # cruddy card with \r as line separator? | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | $vcard =~ tr/\r/\n/; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 31 | my $hash = Text::VCardFast::_vcard2hash($vcard, \%params); | |||||||||||||||||||||||||||||||||||||||||
| 56 | 25 | return $hash; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | # pureperl version | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | # VCard parsing and formatting {{{ | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 5 | my %RFC6868Map = ("n" => "\n", "^" => "^", "'" => "\""); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 5 | my %RFC6868RevMap = reverse %RFC6868Map; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 5 | my %UnescapeMap = ("n" => "\n", "N" => "\n"); | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 5 | my $Pos = 1; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 5 | my @PropOutputOrder = qw(version fn n nickname lang gender org title role bday anniversary email tel adr url impp); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 5 | my %PropOutputOrder = map { $_ => $Pos++ } @PropOutputOrder; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 5 | my @ParamOutputOrder = qw(type pref); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 5 | my %ParamOutputOrder = map { $_ => $Pos++ } @ParamOutputOrder; | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 12 | ▶ | sub vcard2hash_pp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
vcard2hash_pp: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 12 | my $vcard = shift; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 12 | ▶ | unless ($vcard =~ m/\n\S/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | # cruddy card with \r as line separator? | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | $vcard =~ tr/\r/\n/; | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 12 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 12 | return vcardlines2hash_pp(\%params, (split /\r?\n/, $vcard)); | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 12 | ▶ | sub vcardlines2hash_pp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
vcardlines2hash_pp: called CC=28 SCAR=34.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 12 | my $args = shift; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 12 | local $_; | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 12 | my %MultiFieldMap; | |||||||||||||||||||||||||||||||||||||||||
| 88 | my %MultiParamMap; | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 12 | ▶ | if ($args->{multival}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 12 | %MultiFieldMap = map { $_ => 1 } @{$args->{multival}}; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 12 | ▶ | if ($args->{multiparam}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 12 | %MultiParamMap = map { $_ => 1 } @{$args->{multiparam}}; | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # rfc2425, rfc2426, rfc6350, rfc6868 | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 12 | my @Path; | |||||||||||||||||||||||||||||||||||||||||
| 99 | my $Current; | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 12 | ▶ | while ($_ = shift @_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | # Strip EOL | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 190 | s/\r?\n$//; | |||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | # 5.8.1 - Unfold lines if next line starts with space or tab | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 190 | ▶ | if (@_ && $_[0] =~ s/^[ \t]//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ and $_[0] =~ s/^[ \t]//
MC/DC100%2/2
@_ and $_[0] =~ s/^[ \t]//
@_
$_[0] =~ s/^[ \t]//
Truth table100%3/3
A and B
A= @_B= $_[0] =~ s/^[ \t]//
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 21 | $_ .= shift @_; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 21 | redo; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | # Ignore empty lines | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 169 | ▶ | next if /^\s*$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 169 | ▶ | if (/^BEGIN:(.*)/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 13 | push @Path, $Current; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 13 | $Current = { type => lc $1 }; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 13 | push @{ $Path[-1]{objects} }, $Current; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 13 | next; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 156 | ▶ | if (/^END:(.*)/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | die "END $1 in $Current->{type}" | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 13 | ▶ | unless $Current->{type} eq lc $1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 13 | $Current = pop @Path; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 13 | ▶ | return $Current if ($args->{only_one} and not @Path); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args->{"only_one"} and not @Path
MC/DC0%0/2
$args->{"only_one"} and not @Path
$args->{"only_one"}
not @Path
Truth table33%1/3
A and not(B)
A= $args->{"only_one"}B= not @Path
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 13 | next; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | # 5.8.2 - Parse '[group "."] name *(";" param) ":" value' | ||||||||||||||||||||||||||||||||||||||||||
| 128 | # In v2.1, params may not have "=value" part | ||||||||||||||||||||||||||||||||||||||||||
| 129 | # In v4, "," is allowed in non-quoted param value | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 143 | my ($Name) = /^([^;:]*)/gc; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 143 | my @Params = /\G;(?:([\w\-]+)=)?("[^"]*"|[^";:=]*)/gc; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 143 | my ($Value) = /\G:(.*)$/g; | |||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | # 5.8.2 - Type names and parameter names are case insensitive | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 143 | my $LName = lc $Name; | |||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 143 | my %Props; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | # Remove group from each property name and add as attribute | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # (in v4, group names are case insensitive as well) | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 143 | ▶ | if ($LName =~ s/^(.+)\.(.*?)$/$2/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 18 | $Props{group} = $1; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 143 | $Props{name} = $LName; | |||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | # Parse out parameters | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 143 | my %Params; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 143 | ▶ | while (@Params) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | # Parsed into param => param-value pairs | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 100 | my ($PName, $PValue) = splice @Params, 0, 2; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 100 | ▶ | if (not defined $PName) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 7 | ▶ | if ($args->{barekeys}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | $PName = $PValue; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | $PValue = undef; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 7 | $PName = 'type'; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | # 5.8.2 - parameter names are case insensitive | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 100 | my $LPName = lc $PName; | |||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 100 | my @PValue = (undef); | |||||||||||||||||||||||||||||||||||||||||
| 166 | 100 | ▶ | if (defined $PValue) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 100 | $PValue =~ s/^"(.*)"$/$1/; | |||||||||||||||||||||||||||||||||||||||||
| 168 | # \n needed for label, but assume any \; is meant to be ; as well | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 100 | ▶ | $PValue =~ s#\\(.)#$UnescapeMap{$1} // $1#ge; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$UnescapeMap{$1} // $1
MC/DC0%0/2
$UnescapeMap{$1} // $1
$UnescapeMap{$1}
$1
Truth table33%1/3
A // B
A= $UnescapeMap{$1}B= $1
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | # And RFC6868 recoding | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 100 | $PValue =~ s/\^([n^'])/$RFC6868Map{$1}/g; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 100 | ▶ | if ($MultiParamMap{$LPName}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 70 | @PValue = split /,/, $PValue; | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 30 | @PValue = ($PValue); | |||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 100 | ▶ | if (exists $Params{$LPName}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 15 | push @{$Params{$LPName}}, @PValue; | |||||||||||||||||||||||||||||||||||||||||
| 182 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 85 | $Params{$LPName} = \@PValue; | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 143 | ▶ | $Props{params} = \%Params if keys %Params; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 143 | my $Encoding = $Params{encoding}; | |||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 143 | ▶ | if ($MultiFieldMap{$LName}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | # use negative 'limit' to force trailing fields | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 26 | $Value = [ split /(?<!\\);/, $Value, -1 ]; | |||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | s#\\(.)#$UnescapeMap{$1} // $1#ge for @$Value; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$UnescapeMap{$1} // $1
MC/DC0%0/2
$UnescapeMap{$1} // $1
$UnescapeMap{$1}
$1
Truth table0%0/3
A // B
A= $UnescapeMap{$1}B= $1
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 26 | $Props{values} = $Value; | |||||||||||||||||||||||||||||||||||||||||
| 195 | ▶ | } elsif ($Encoding && lc $Encoding eq 'b') { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$Encoding and lc $Encoding eq "b"
MC/DC0%0/2
$Encoding and lc $Encoding eq "b"
$Encoding
lc $Encoding eq "b"
Truth table66%2/3
A and B
A= $EncodingB= lc $Encoding eq "b"
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | # Don't bother unescaping base64 value | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | $Props{value} = $Value; | |||||||||||||||||||||||||||||||||||||||||
| 199 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 117 | ▶ | $Value =~ s#\\(.)#$UnescapeMap{$1} // $1#ge; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$UnescapeMap{$1} // $1
MC/DC0%0/2
$UnescapeMap{$1} // $1
$UnescapeMap{$1}
$1
Truth table66%2/3
A // B
A= $UnescapeMap{$1}B= $1
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 117 | $Props{value} = $Value; | |||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 143 | push @{$Current->{properties}->{$LName}}, \%Props; | |||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | # something did a BEGIN but no END - TODO, unwind this nicely as | ||||||||||||||||||||||||||||||||||||||||||
| 208 | # it may be more than one level | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 12 | ▶ | die "BEGIN $Current->{type} without matching END" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | if @Path; | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | 12 | return $Current; | |||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | 15 | ▶ | sub hash2vcard_pp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hash2vcard_pp: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 15 | ▶ | return join "", map { $_ . ($_[1] // "\n") } hash2vcardlines_pp($_[0]); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_[1] // "\n"
MC/DC0%0/1
$_[1] // "\n"
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 31 | ▶ | sub hash2vcardlines_pp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hash2vcardlines_pp: called CC=34 SCAR=37.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 31 | ▶ | my $Objects = shift->{objects} // []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
+(shift())->{"objects"} // []
MC/DC100%1/1
+(shift())->{"objects"} // []
+(shift())->{"objects"}
Truth table100%2/2
A
A= +(shift())->{"objects"}
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 31 | my @Lines; | |||||||||||||||||||||||||||||||||||||||||
| 223 | 31 | for my $Card (@$Objects) { | |||||||||||||||||||||||||||||||||||||||||
| 224 | # We group properties in the same group together, track if we've | ||||||||||||||||||||||||||||||||||||||||||
| 225 | # already output a property | ||||||||||||||||||||||||||||||||||||||||||
| 226 | 16 | my %DoneProps; | |||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 16 | my $Props = $Card->{properties}; | |||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | # Order the properties | ||||||||||||||||||||||||||||||||||||||||||
| 231 | my @PropKeys = sort { | ||||||||||||||||||||||||||||||||||||||||||
| 232 | 16 | ▶ | ($PropOutputOrder{$a} // 1000) <=> ($PropOutputOrder{$b} // 1000) | ||||||||||||||||||||||||||||||||||||||||
|
Condition85%6/7
($PropOutputOrder{$a} // 1000) <=> ($PropOutputOrder{$b} // 1000) || $a cmp $b
$PropOutputOrder{$a} // 1000
$PropOutputOrder{$b} // 1000
MC/DC0%0/2
($PropOutputOrder{$a} // 1000) <=> ($PropOutputOrder{$b} // 1000) || $a cmp $b
$PropOutputOrder{$a}
$a cmp $b
MC/DC100%1/1
$PropOutputOrder{$b} // 1000
$PropOutputOrder{$b}
Truth table66%2/3
A || B
A= $PropOutputOrder{$a}B= $a cmp $b
Truth table100%2/2
A
A= $PropOutputOrder{$b}
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | || $a cmp $b | ||||||||||||||||||||||||||||||||||||||||||
| 234 | } keys %$Props; | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | # Make sure items in the same group are output together | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 16 | ▶ | my $Groups = $Card->{groups} || do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$Card->{'groups'} || do {
my %Groups;
foreach $_ (map {@$_;} values %$Props) {
push @{$Groups{$_->{'group'}};}, $_ if $_->{'group'};
};
\%Groups
}
MC/DC0%0/2
$Card->{'groups'} || do {
my %Groups;
foreach $_ (map {@$_;} values %$Props) {
push @{$Groups{$_->{'group'}};}, $_ if $_->{'group'};
};
\%Groups
}
$Card->{'groups'}
do {
my %Groups;
foreach $_ (map {@$_;} values %$Props) {
push @{$Groups{$_->{'group'}};}, $_ if $_->{'group'};
};
\%Groups
}
Truth table33%1/3
A || B
A= $Card->{'groups'}B= do {
my %Groups;
foreach $_ (map {@$_;} values %$Props) {
push @{$Groups{$_->{'group'}};}, $_ if $_->{'group'};
};
\%Groups
}
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 16 | my %Groups; | |||||||||||||||||||||||||||||||||||||||||
| 239 | 16 | for (map { @$_ } values %$Props) { | |||||||||||||||||||||||||||||||||||||||||
| 240 | 146 | ▶ | push @{$Groups{$_->{group}}}, $_ if $_->{group}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 16 | \%Groups; | |||||||||||||||||||||||||||||||||||||||||
| 243 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | # Generate output list | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 16 | my @OutputProps; | |||||||||||||||||||||||||||||||||||||||||
| 247 | 16 | for my $PropKey (@PropKeys) { | |||||||||||||||||||||||||||||||||||||||||
| 248 | 125 | my @PropVals = @{$Props->{$PropKey}}; | |||||||||||||||||||||||||||||||||||||||||
| 249 | 125 | for my $PropVal (@PropVals) { | |||||||||||||||||||||||||||||||||||||||||
| 250 | 146 | ▶ | next if $DoneProps{"$PropVal"}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | 137 | push @OutputProps, $PropVal; | |||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | # If it has a group, output all values in that group together | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 137 | ▶ | if (my $Group = $PropVal->{group}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 9 | push @OutputProps, grep { !$DoneProps{"$_"}++ } @{$Groups->{$Group}}; | |||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | 16 | my $Type = uc $Card->{type}; | |||||||||||||||||||||||||||||||||||||||||
| 262 | 16 | push @Lines, ("BEGIN:" . $Type); | |||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 16 | for (@OutputProps) { | |||||||||||||||||||||||||||||||||||||||||
| 265 | # Skip deleted or synthetic properties | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 146 | ▶ | next if $_->{deleted} || $_->{name} eq 'online'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_->{"deleted"} or $_->{"name"} eq "online"
MC/DC0%0/2
$_->{"deleted"} or $_->{"name"} eq "online"
$_->{"deleted"}
$_->{"name"} eq "online"
Truth table33%1/3
A or B
A= $_->{"deleted"}B= $_->{"name"} eq "online"
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | 146 | my $Binary = $_->{binary}; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 146 | ▶ | if ($Binary) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | my $Encoding = ($_->{params}->{encoding} //= []); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$_->{"params"}{"encoding"} //= []
MC/DC0%0/1
$_->{"params"}{"encoding"} //= []
$_->{"params"}{"encoding"}
Truth table0%0/2
A
A= $_->{"params"}{"encoding"}
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | push @$Encoding, "b" if !@$Encoding; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | 146 | my $LName = $_->{name}; | |||||||||||||||||||||||||||||||||||||||||
| 275 | 146 | my $Group = $_->{group}; | |||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | # rfc6350 3.3 - it is RECOMMENDED that property and parameter names be upper-case on output. | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 146 | ▶ | my $Line = ($Group ? (uc $Group . ".") : "") . uc $LName; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | 146 | ▶ | while (my ($Param, $ParamVals) = each %{$_->{params} // {}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$_->{"params"} // {}
MC/DC100%1/1
$_->{"params"} // {}
$_->{"params"}
Truth table100%2/2
A
A= $_->{"params"}
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 85 | ▶ | if (!defined $ParamVals) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | $Line .= ";" . uc($Param); | |||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 85 | ▶ | for (ref($ParamVals) ? @$ParamVals : $ParamVals) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 114 | ▶ | my $PV = $_ // next; # Modify copy | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_ // (next)
MC/DC0%0/1
$_ // (next)
$_
Truth table50%1/2
A
A= $_
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 114 | ▶ | $PV =~ s/\n/\\N/g if $Param eq 'label'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 114 | $PV =~ s/([\n^"])/'^' . $RFC6868RevMap{$1}/ge; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 114 | ▶ | $PV = '"' . $PV . '"' if $PV =~ /\W/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 114 | $Line .= ";" . uc($Param) . "=" . $PV; | |||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 146 | $Line .= ":"; | |||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | 146 | ▶ | my $Value = $_->{values} || $_->{value}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$_->{'values'} || $_->{'value'}
MC/DC100%2/2
$_->{'values'} || $_->{'value'}
$_->{'values'}
$_->{'value'}
Truth table100%3/3
A || B
A= $_->{'values'}B= $_->{'value'}
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | 146 | ▶ | if ($_->{binary}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | $Value = encode_base64($Value, ''); | |||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 300 | my @Values = map { | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 146 | ▶ | my $V = ref($_) ? $$_ : $_; # Modify copy | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 253 | ▶ | $V //= ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$V //= ""
MC/DC0%0/1
$V //= ""
$V
Truth table50%1/2
A
A= $V
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | # rfc6350 3.4 (v4, assume clarifies many v3 semantics) | ||||||||||||||||||||||||||||||||||||||||||
| 304 | # - a SEMICOLON in a field of such a "compound" property MUST be | ||||||||||||||||||||||||||||||||||||||||||
| 305 | # escaped with a BACKSLASH character | ||||||||||||||||||||||||||||||||||||||||||
| 306 | # - a COMMA character in one of a field's values MUST be escaped | ||||||||||||||||||||||||||||||||||||||||||
| 307 | # with a BACKSLASH character | ||||||||||||||||||||||||||||||||||||||||||
| 308 | # - BACKSLASH characters in values MUST be escaped with a BACKSLASH | ||||||||||||||||||||||||||||||||||||||||||
| 309 | # character. | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 253 | $V =~ s/([\,\;\\])/\\$1/g; | |||||||||||||||||||||||||||||||||||||||||
| 311 | # - NEWLINE (U+000A) characters in values MUST be encoded | ||||||||||||||||||||||||||||||||||||||||||
| 312 | # by two characters: a BACKSLASH followed by either an 'n' (U+006E) | ||||||||||||||||||||||||||||||||||||||||||
| 313 | # or an 'N' (U+004E). | ||||||||||||||||||||||||||||||||||||||||||
| 314 | 253 | $V =~ s/\n/\\n/g; | |||||||||||||||||||||||||||||||||||||||||
| 315 | 253 | ▶ | $V; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | } ref $Value ? @$Value : $Value; | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | 146 | $Value = join ";", @Values; | |||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | # Stripped v4 proto prefix, add it back | ||||||||||||||||||||||||||||||||||||||||||
| 321 | 146 | ▶ | if (my $ProtoStrip = $_->{proto_strip}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | $Value = $ProtoStrip . $Value; | |||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | # If it's a perl unicode string, make it utf-8 bytes | ||||||||||||||||||||||||||||||||||||||||||
| 326 | #if (utf8::is_utf8($Value)) { | ||||||||||||||||||||||||||||||||||||||||||
| 327 | #$Value = encode_utf8($Value); | ||||||||||||||||||||||||||||||||||||||||||
| 328 | #} | ||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | 146 | $Line .= $Value; | |||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | 146 | push @Lines, foldline($Line); | |||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | 16 | push @Lines, hash2vcardlines_pp($Card); | |||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | 16 | push @Lines, "END:" . $Type; | |||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | 31 | return @Lines; | |||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | 146 | ▶ | sub foldline { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
foldline: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 345 | 146 | local $_ = shift; | |||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | # Fold at every \n, regardless of position | ||||||||||||||||||||||||||||||||||||||||||
| 348 | # Try folding on at whitespace boundaries after 60 chars first | ||||||||||||||||||||||||||||||||||||||||||
| 349 | # Otherwise fold to 75 chars, but don't split utf-8 unicode char or end with a \ | ||||||||||||||||||||||||||||||||||||||||||
| 350 | 146 | my @Out; | |||||||||||||||||||||||||||||||||||||||||
| 351 | 146 | ▶ | while (/\G(.{0,75}?\\n)/gc || /\G(.{60,75})(?<=[^\n\t ])(?=[\n\t ])/gc || /\G(.{0,74}[^\\])(?![\x80-\xbf])/gc) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
/\G(.{0,75}?\\n)/cg or /\G(.{60,75})(?<=[^\n\t ])(?=[\n\t ])/cg or /\G(.{0,74}[^\\])(?![\x80-\xbf])/cg
/\G(.{0,75}?\\n)/cg or /\G(.{60,75})(?<=[^\n\t ])(?=[\n\t ])/cg
MC/DC100%3/3
/\G(.{0,75}?\\n)/cg or /\G(.{60,75})(?<=[^\n\t ])(?=[\n\t ])/cg or /\G(.{0,74}[^\\])(?![\x80-\xbf])/cg
/\G(.{0,75}?\\n)/cg
/\G(.{60,75})(?<=[^\n\t ])(?=[\n\t ])/cg
/\G(.{0,74}[^\\])(?![\x80-\xbf])/cg
Truth table100%4/4
A or B or C
A= /\G(.{0,75}?\\n)/cgB= /\G(.{60,75})(?<=[^\n\t ])(?=[\n\t ])/cgC= /\G(.{0,74}[^\\])(?![\x80-\xbf])/cg
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | 191 | ▶ | push @Out, (@Out ? " " . $1 : $1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | 146 | ▶ | push @Out, " " . substr($_, pos($_)) if pos $_ != length $_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | 146 | return @Out; | |||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | # }}} | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 364 | __END__ | ||||||||||||||||||||||||||||||||||||||||||