| 1 | package JQ::Lite::Value; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use JSON::PP (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use B (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | # Older B releases do not expose the public SV flag constants. The flag | ||||||||||
| 11 | # values themselves are part of Perl's long-standing SV layout, so retain | ||||||||||
| 12 | # their historical values when running with one of those releases. | ||||||||||
| 13 | 1 | ▶ | my $SVf_IOK = B->can('SVf_IOK') ? B::SVf_IOK() : 0x00000100; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 14 | 1 | ▶ | my $SVf_NOK = B->can('SVf_NOK') ? B::SVf_NOK() : 0x00000200; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 1 | ▶ | my $SVf_POK = B->can('SVf_POK') ? B::SVf_POK() : 0x00000400; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | |||||||||||
| 17 | # This module is deliberately internal. It provides the jq-style value | ||||||||||
| 18 | # semantics needed by the 3.0 evaluator without changing the compatibility | ||||||||||
| 19 | # behaviour of the 2.x filters that still live in JQ::Lite::Util. | ||||||||||
| 20 | |||||||||||
| 21 | 54 | ▶ | sub type_of { | ||||||||
|
Subroutine
type_of: called CC=9 SCAR=22.9
Pod
undocumented
| |||||||||||
| 22 | 54 | my ($value) = @_; | |||||||||
| 23 | |||||||||||
| 24 | 54 | ▶ | return 'null' unless defined $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 53 | ▶ | return 'boolean' if JSON::PP::is_bool($value); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 49 | ▶ | return 'array' if ref($value) eq 'ARRAY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 36 | ▶ | return 'object' if ref($value) eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 29 | ▶ | if (!ref($value)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 29 | my $flags = B::svref_2object(\$value)->FLAGS; | |||||||||
| 30 | # Test the public numeric flags first. Older Perl releases may promote | ||||||||||
| 31 | # the cached PV to SVf_POK during interpolation even though IOK/NOK is | ||||||||||
| 32 | # still present. Numeric identity therefore wins for dual-valued | ||||||||||
| 33 | # scalars regardless of how the bundled JSON::PP treats dualvars. | ||||||||||
| 34 | 29 | ▶ | return 'number' if $flags & ($SVf_IOK | $SVf_NOK); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 8 | ▶ | return 'string' if $flags & $SVf_POK; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 0 | ▶ | return 'number' if looks_like_number($value); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | } | ||||||||||
| 38 | 0 | return 'string'; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 23 | ▶ | sub compare { | ||||||||
|
Subroutine
compare: called CC=14 SCAR=26.4
Pod
undocumented
| |||||||||||
| 42 | 23 | my ($left, $right) = @_; | |||||||||
| 43 | |||||||||||
| 44 | 23 | my %rank = ( | |||||||||
| 45 | null => 0, | ||||||||||
| 46 | boolean => 1, | ||||||||||
| 47 | number => 2, | ||||||||||
| 48 | string => 3, | ||||||||||
| 49 | array => 4, | ||||||||||
| 50 | object => 5, | ||||||||||
| 51 | ); | ||||||||||
| 52 | |||||||||||
| 53 | 23 | my $left_type = type_of($left); | |||||||||
| 54 | 23 | my $right_type = type_of($right); | |||||||||
| 55 | 23 | ▶ | return $rank{$left_type} <=> $rank{$right_type} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | if $left_type ne $right_type; | ||||||||||
| 57 | |||||||||||
| 58 | 21 | ▶ | return 0 if $left_type eq 'null'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 21 | ▶ | return (!!$left) <=> (!!$right) if $left_type eq 'boolean'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 20 | ▶ | return $left <=> $right if $left_type eq 'number'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 12 | ▶ | return $left cmp $right if $left_type eq 'string'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | |||||||||||
| 63 | 9 | ▶ | if ($left_type eq 'array') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 6 | ▶ | my $limit = @{$left} < @{$right} ? @{$left} : @{$right}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 6 | ▶ | for my $index ($limit ? (0 .. $limit - 1) : ()) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 9 | my $ordering = compare($left->[$index], $right->[$index]); | |||||||||
| 67 | 9 | ▶ | return $ordering if $ordering; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | } | ||||||||||
| 69 | 4 | return @{$left} <=> @{$right}; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 3 | my @left_keys = sort keys %{$left}; | |||||||||
| 73 | 3 | my @right_keys = sort keys %{$right}; | |||||||||
| 74 | 3 | my $key_ordering = compare(\@left_keys, \@right_keys); | |||||||||
| 75 | 3 | ▶ | return $key_ordering if $key_ordering; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 2 | for my $key (@left_keys) { | |||||||||
| 77 | 2 | my $ordering = compare($left->{$key}, $right->{$key}); | |||||||||
| 78 | 2 | ▶ | return $ordering if $ordering; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | } | ||||||||||
| 80 | 1 | return 0; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 4 | ▶ | sub equal { | ||||||||
|
Subroutine
equal: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 84 | 4 | my ($left, $right) = @_; | |||||||||
| 85 | 4 | return compare($left, $right) == 0; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 1 | 1; | |||||||||