| 1 | package DBIO::PostgreSQL::PostGIS::Codec::WKT::Parser; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: WKT/EWKT parser for PostGIS geometry values | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 13 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | # Collections nest at most this deep; the outermost counts as level 1. | ||||||||||||||||||||||||||||||||||||||||||
| 13 | 13 | my $MAX_DEPTH = 32; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # A number as WKT writes it: optional minus, digits with an optional | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # fraction or a fraction alone, optional exponent. | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 13 | my $NUMBER_RE = qr/\A-?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?\z/; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 13 | my $TYPE_RE = qr/geometrycollection|multilinestring|multipolygon|multipoint|linestring|polygon|point/i; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | # Thrown by the helpers for text that is no geometry; parse turns it into | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # undef and rethrows anything else. | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 13 | my $UNREADABLE = "DBIO::PostgreSQL::PostGIS::Codec::WKT::Parser: unreadable\n"; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 30 | ▶ | sub _unreadable { die $UNREADABLE } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unreadable: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 370 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 370 | my ($class, $wkt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 370 | ▶ | return undef unless defined $wkt; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 370 | my $text = "$wkt"; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 370 | my $result = eval { | |||||||||||||||||||||||||||||||||||||||||
| 31 | 370 | my $g = _geometry(\$text, 0); | |||||||||||||||||||||||||||||||||||||||||
| 32 | 332 | ▶ | $text =~ /\G\s*\z/ or _unreadable(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 332 | $g; | |||||||||||||||||||||||||||||||||||||||||
| 34 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 370 | ▶ | if (my $err = $@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 38 | ▶ | return undef if !ref $err && $err eq $UNREADABLE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $err and $err eq $UNREADABLE
MC/DC50%1/2
not ref $err and $err eq $UNREADABLE
not ref $err
$err eq $UNREADABLE
Truth table66%2/3
not(A) and B
A= not ref $errB= $err eq $UNREADABLE
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 8 | die $err; | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 332 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | # The parser reads the one string through pos() and \G: every helper takes a | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # reference to it, continues where the previous one stopped and leaves pos() | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # behind its own token. Nothing is copied but the text of a coordinate list. | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | # One geometry at the position. $depth is the number of GEOMETRYCOLLECTIONs | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # around it. | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 238235 | ▶ | sub _geometry { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_geometry: called CC=42 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 238235 | my ($s, $depth) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | # The marker is glued to the type (POINTM) or a word of its own, never both. | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 238235 | ▶ | $$s =~ /\G\s*($TYPE_RE)(?:(ZM|Z|M)|\s+(ZM|Z|M))?\s*(\(|EMPTY)/gci | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | or _unreadable(); | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 238231 | ▶ | my ($type, $mark, $open) = (lc $1, uc($2 // $3 // ''), $4); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%5/5
($2 // $3) // ""
$2 // $3
MC/DC100%2/2
($2 // $3) // ""
$2
$3
Truth table100%3/3
A // B
A= $2B= $3
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 238231 | ▶ | my $marked = length $mark ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 238231 | ▶ | my $has_z = $mark =~ /Z/ ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 238231 | ▶ | my $has_m = $mark =~ /M/ ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 238231 | ▶ | DBIO::Exception->throw("WKT nesting too deep") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "geometrycollection" and ++$depth > $MAX_DEPTH
MC/DC100%2/2
$type eq "geometrycollection" and ++$depth > $MAX_DEPTH
$type eq "geometrycollection"
++$depth > $MAX_DEPTH
Truth table100%3/3
A and B
A= $type eq "geometrycollection"B= ++$depth > $MAX_DEPTH
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | if $type eq 'geometrycollection' && ++$depth > $MAX_DEPTH; | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 238223 | ▶ | if ($open ne '(') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 68 | return { type => $type, coords => [], has_z => $has_z, has_m => $has_m, is_empty => 1 }; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 238155 | my ($coords, $is_empty); | |||||||||||||||||||||||||||||||||||||||||
| 67 | 238155 | ▶ | if ($type eq 'point') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 237622 | my $leaf = _leaf($s); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 237621 | ▶ | _unreadable() unless $leaf =~ /\S/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 237621 | $coords = _parse_point_str($leaf); | |||||||||||||||||||||||||||||||||||||||||
| 71 | } elsif ($type eq 'linestring') { | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 48 | my $leaf = _leaf($s); | |||||||||||||||||||||||||||||||||||||||||
| 73 | 48 | ▶ | _unreadable() unless $leaf =~ /\S/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 48 | $coords = _parse_ring($leaf); | |||||||||||||||||||||||||||||||||||||||||
| 75 | } elsif ($type eq 'multipoint') { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | # Both forms: "1 2,3 4" and "(1 2),(3 4)". A point holds no comma, so | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # the commas separate the points either way. An EMPTY member has no | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # ordinates and keeps its place as []. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 67 | my @points; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 67 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 475125 | ▶ | if (_empty($s)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 22 | push @points, []; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } elsif ($$s =~ /\G\(/gc) { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 475042 | push @points, _parse_point_str(_leaf($s)); | |||||||||||||||||||||||||||||||||||||||||
| 85 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 61 | $$s =~ /\G([^(),]*)/gc; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 61 | my $bare = $1; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 61 | ▶ | _unreadable() unless $bare =~ /\S/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 61 | push @points, _parse_point_str($bare); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | } while (_separator($s)); | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 65 | $coords = \@points; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } elsif ($type eq 'polygon') { | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 29 | $coords = _rings($s); | |||||||||||||||||||||||||||||||||||||||||
| 95 | } elsif ($type eq 'multilinestring') { | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 19 | my @lines; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 19 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 29 | ▶ | push @lines, _empty($s) ? [] : _parse_ring(_leaf(_open($s))); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | } while (_separator($s)); | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 18 | $coords = \@lines; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } elsif ($type eq 'multipolygon') { | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 23 | my @polygons; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 23 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 33 | ▶ | push @polygons, _empty($s) ? [] : _rings(_open($s)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | } while (_separator($s)); | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 22 | $coords = \@polygons; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 347 | my (@members, $z, $m); | |||||||||||||||||||||||||||||||||||||||||
| 109 | 347 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 237865 | my $member = _geometry($s, $depth); | |||||||||||||||||||||||||||||||||||||||||
| 111 | 237607 | ▶ | $z ||= $member->{has_z}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$z ||= $member->{"has_z"}
MC/DC100%2/2
$z ||= $member->{"has_z"}
$z
$member->{"has_z"}
Truth table100%3/3
A ||= B
A= $zB= $member->{"has_z"}
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 237607 | ▶ | $m ||= $member->{has_m}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$m ||= $member->{"has_m"}
MC/DC100%2/2
$m ||= $member->{"has_m"}
$m
$member->{"has_m"}
Truth table100%3/3
A ||= B
A= $mB= $member->{"has_m"}
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | push @members, { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | type => $member->{type}, coords => $member->{coords}, is_empty => $member->{is_empty}, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 237607 | }; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } while (_separator($s)); | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 88 | $coords = \@members; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 88 | ▶ | ($has_z, $has_m) = ($z ? 1 : 0, $m ? 1 : 0) unless $marked; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 88 | ▶ | $is_empty = (grep { !$_->{is_empty} } @members) ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | # No marker: the ordinates say it, three are Z and four are ZM, as PostGIS | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # reads them. | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 237871 | ▶ | if (!$marked && $type ne 'geometrycollection') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $marked and $type ne "geometrycollection"
MC/DC100%2/2
not $marked and $type ne "geometrycollection"
not $marked
$type ne "geometrycollection"
Truth table100%3/3
not(A) and B
A= not $markedB= $type ne "geometrycollection"
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 237665 | my $n = _first_point_size($coords); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 237665 | ▶ | ($has_z, $has_m) = ($n >= 3 ? 1 : 0, $n >= 4 ? 1 : 0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | $is_empty //= ($type eq 'point' || $type eq 'linestring' || $type eq 'polygon') | ||||||||||||||||||||||||||||||||||||||||||
| 130 | ? 0 | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 237871 | ▶ | : ((grep { @$_ } @$coords) ? 0 : 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%8/8
$is_empty //= $type eq "point" || $type eq "linestring" || $type eq "polygon" ? 0 : (grep({@$_;} @$coords) ? 0 : 1)
$type eq "point" or $type eq "linestring" or $type eq "polygon"
$type eq "point" or $type eq "linestring"
MC/DC100%1/1
$is_empty //= $type eq "point" || $type eq "linestring" || $type eq "polygon" ? 0 : (grep({@$_;} @$coords) ? 0 : 1)
$is_empty
Truth table100%2/2
A
A= $is_empty
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 237871 | return { type => $type, coords => $coords, has_z => $has_z, has_m => $has_m, is_empty => $is_empty }; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | # The text up to the closing parenthesis, which is consumed with it. A | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # coordinate list holds no parenthesis. | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 712795 | ▶ | sub _leaf { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_leaf: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 712795 | my ($s) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 712795 | ▶ | $$s =~ /\G([^()]*)\)/gc or _unreadable(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 712794 | return $1; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | # After a member: true at a comma (another member follows), false at the | ||||||||||||||||||||||||||||||||||||||||||
| 145 | # closing parenthesis of the list, both consumed. | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 712848 | ▶ | sub _separator { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_separator: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 712848 | my ($s) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 712848 | ▶ | $$s =~ /\G\s*(?:(,)|\))/gc or _unreadable(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 712847 | ▶ | return defined $1 ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | # True at EMPTY (consumed); a MULTI* member that is empty keeps its place as []. | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 475187 | ▶ | sub _empty { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_empty: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 475187 | my ($s) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 475187 | $$s =~ /\G\s*/gc; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 475187 | ▶ | return $$s =~ /\GEMPTY/gci ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | # The opening parenthesis of a member. | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 48 | ▶ | sub _open { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_open: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 48 | my ($s) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 48 | ▶ | $$s =~ /\G\(/gc or _unreadable(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 48 | return $s; | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | # The rings of a polygon, behind its opening parenthesis: "(x y,x y),(...)". | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 54 | ▶ | sub _rings { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_rings: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 54 | my ($s) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 54 | my @rings; | |||||||||||||||||||||||||||||||||||||||||
| 170 | 54 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 60 | ▶ | $$s =~ /\G\s*\(/gc or _unreadable(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 60 | push @rings, _parse_ring(_leaf($s)); | |||||||||||||||||||||||||||||||||||||||||
| 173 | } while (_separator($s)); | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 52 | return \@rings; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | # Number of ordinates of the first point there is, whatever the nesting; 0 | ||||||||||||||||||||||||||||||||||||||||||
| 178 | # for a geometry without a point. | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 237842 | ▶ | sub _first_point_size { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_first_point_size: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 237842 | my ($c) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 181 | 237842 | ▶ | return 0 unless ref $c eq 'ARRAY' && @$c; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $c eq "ARRAY" and @$c
MC/DC50%1/2
ref $c eq "ARRAY" and @$c
ref $c eq "ARRAY"
@$c
Truth table66%2/3
A and B
A= ref $c eq "ARRAY"B= @$c
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 237820 | ▶ | return scalar @$c unless ref $c->[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 164 | for (@$c) { | |||||||||||||||||||||||||||||||||||||||||
| 184 | 177 | my $n = _first_point_size($_); | |||||||||||||||||||||||||||||||||||||||||
| 185 | 177 | ▶ | return $n if $n; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 9 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | # split ' ' skips leading whitespace and splits at whitespace runs, both in | ||||||||||||||||||||||||||||||||||||||||||
| 191 | # one pass; a regex trim (\s+\z) would try every start of a run. | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 713123 | ▶ | sub _parse_point_str { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_point_str: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 713123 | my ($str) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 194 | 713123 | my @ord = split ' ', $str; | |||||||||||||||||||||||||||||||||||||||||
| 195 | 713123 | ▶ | _unreadable() if @ord < 2 || @ord > 4; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@ord < 2 or @ord > 4
MC/DC100%2/2
@ord < 2 or @ord > 4
@ord < 2
@ord > 4
Truth table100%3/3
A or B
A= @ord < 2B= @ord > 4
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 713114 | ▶ | _unreadable() if grep { !/$NUMBER_RE/ } @ord; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 713101 | my @num = map { 0 + $_ } @ord; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 713101 | ▶ | _unreadable() if grep { $_ != $_ || $_ == 9**9**9 || $_ == -9**9**9 } @num; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$_ != $_ || $_ == 9**9**9 || $_ == -9**9**9
$_ != $_ or $_ == "Inf"
MC/DC66%2/3
$_ != $_ || $_ == 9**9**9 || $_ == -9**9**9
$_ != $_
$_ == "Inf"
$_ == -9**9**9
Truth table75%3/4
A or B || C
A= $_ != $_B= $_ == "Inf"C= $_ == -9**9**9
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 713099 | \@num; | |||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | # The points of a list, split at the commas, empty entries kept (an empty one | ||||||||||||||||||||||||||||||||||||||||||
| 203 | # is unreadable); each point trims itself. | ||||||||||||||||||||||||||||||||||||||||||
| 204 | 131 | ▶ | sub _parse_ring { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_ring: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 131 | my ($str) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 131 | ▶ | return [] unless $str =~ /\S/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 131 | [ map { _parse_point_str($_) } split /,/, $str, -1 ]; | |||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | 13 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | __END__ | ||||||||||||||||||||||||||||||||||||||||||