Filename | /Users/ether/.perlbrew/libs/36.0@std/lib/perl5/YAML/PP/Schema/JSON.pm |
Statements | Executed 163 statements in 1.20ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
145 | 1 | 1 | 355µs | 355µs | _to_int | YAML::PP::Schema::JSON::
1 | 1 | 1 | 43µs | 46µs | BEGIN@1.283 | YAML::PP::
1 | 1 | 1 | 10µs | 39µs | BEGIN@14 | YAML::PP::Schema::JSON::
1 | 1 | 1 | 7µs | 9µs | BEGIN@13 | YAML::PP::Schema::JSON::
1 | 1 | 1 | 6µs | 35µs | BEGIN@2.284 | YAML::PP::
1 | 1 | 1 | 5µs | 18µs | BEGIN@16 | YAML::PP::Schema::JSON::
1 | 1 | 1 | 5µs | 69µs | BEGIN@7 | YAML::PP::Schema::JSON::
2 | 2 | 1 | 3µs | 3µs | CORE:qr (opcode) | YAML::PP::Schema::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:78] | YAML::PP::Schema::JSON::
0 | 0 | 0 | 0s | 0s | _to_float | YAML::PP::Schema::JSON::
0 | 0 | 0 | 0s | 0s | register | YAML::PP::Schema::JSON::
0 | 0 | 0 | 0s | 0s | represent_bool | YAML::PP::Schema::JSON::
0 | 0 | 0 | 0s | 0s | represent_float | YAML::PP::Schema::JSON::
0 | 0 | 0 | 0s | 0s | represent_int | YAML::PP::Schema::JSON::
0 | 0 | 0 | 0s | 0s | represent_literal | YAML::PP::Schema::JSON::
0 | 0 | 0 | 0s | 0s | represent_undef | YAML::PP::Schema::JSON::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | 2 | 29µs | 2 | 49µs | # spent 46µs (43+3) within YAML::PP::BEGIN@1.283 which was called:
# once (43µs+3µs) by YAML::PP::BEGIN@9 at line 1 # spent 46µs making 1 call to YAML::PP::BEGIN@1.283
# spent 3µs making 1 call to strict::import |
2 | 2 | 38µs | 2 | 64µs | # spent 35µs (6+29) within YAML::PP::BEGIN@2.284 which was called:
# once (6µs+29µs) by YAML::PP::BEGIN@9 at line 2 # spent 35µs making 1 call to YAML::PP::BEGIN@2.284
# spent 29µs making 1 call to warnings::import |
3 | package YAML::PP::Schema::JSON; | ||||
4 | |||||
5 | 1 | 1µs | our $VERSION = '0.035'; # VERSION | ||
6 | |||||
7 | 2 | 39µs | 2 | 133µs | # spent 69µs (5+64) within YAML::PP::Schema::JSON::BEGIN@7 which was called:
# once (5µs+64µs) by YAML::PP::BEGIN@9 at line 7 # spent 69µs making 1 call to YAML::PP::Schema::JSON::BEGIN@7
# spent 64µs making 1 call to base::import |
8 | 1 | 1µs | our @EXPORT_OK = qw/ | ||
9 | represent_int represent_float represent_literal represent_bool | ||||
10 | represent_undef | ||||
11 | /; | ||||
12 | |||||
13 | 2 | 29µs | 2 | 11µs | # spent 9µs (7+2) within YAML::PP::Schema::JSON::BEGIN@13 which was called:
# once (7µs+2µs) by YAML::PP::BEGIN@9 at line 13 # spent 9µs making 1 call to YAML::PP::Schema::JSON::BEGIN@13
# spent 2µs making 1 call to B::import |
14 | 2 | 20µs | 2 | 68µs | # spent 39µs (10+29) within YAML::PP::Schema::JSON::BEGIN@14 which was called:
# once (10µs+29µs) by YAML::PP::BEGIN@9 at line 14 # spent 39µs making 1 call to YAML::PP::Schema::JSON::BEGIN@14
# spent 29µs making 1 call to Exporter::import |
15 | |||||
16 | 2 | 675µs | 2 | 31µs | # spent 18µs (5+13) within YAML::PP::Schema::JSON::BEGIN@16 which was called:
# once (5µs+13µs) by YAML::PP::BEGIN@9 at line 16 # spent 18µs making 1 call to YAML::PP::Schema::JSON::BEGIN@16
# spent 13µs making 1 call to Exporter::import |
17 | |||||
18 | 1 | 7µs | 1 | 2µs | my $RE_INT = qr{^(-?(?:0|[1-9][0-9]*))$}; # spent 2µs making 1 call to YAML::PP::Schema::JSON::CORE:qr |
19 | 1 | 2µs | 1 | 1µs | my $RE_FLOAT = qr{^(-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?)$}; # spent 1µs making 1 call to YAML::PP::Schema::JSON::CORE:qr |
20 | |||||
21 | 145 | 349µs | # spent 355µs within YAML::PP::Schema::JSON::_to_int which was called 145 times, avg 2µs/call:
# 145 times (355µs+0s) by YAML::PP::Schema::load_scalar at line 342 of YAML/PP/Schema.pm, avg 2µs/call | ||
22 | |||||
23 | # DaTa++ && shmem++ | ||||
24 | sub _to_float { unpack F => pack F => $_[2]->[0] } | ||||
25 | |||||
26 | sub register { | ||||
27 | my ($self, %args) = @_; | ||||
28 | my $schema = $args{schema}; | ||||
29 | my $options = $args{options}; | ||||
30 | my $empty_null = 0; | ||||
31 | for my $opt (@$options) { | ||||
32 | if ($opt eq 'empty=str') { | ||||
33 | } | ||||
34 | elsif ($opt eq 'empty=null') { | ||||
35 | $empty_null = 1; | ||||
36 | } | ||||
37 | else { | ||||
38 | croak "Invalid option for JSON Schema: '$opt'"; | ||||
39 | } | ||||
40 | } | ||||
41 | |||||
42 | $schema->add_resolver( | ||||
43 | tag => 'tag:yaml.org,2002:null', | ||||
44 | match => [ equals => null => undef ], | ||||
45 | ); | ||||
46 | if ($empty_null) { | ||||
47 | $schema->add_resolver( | ||||
48 | tag => 'tag:yaml.org,2002:null', | ||||
49 | match => [ equals => '' => undef ], | ||||
50 | implicit => 1, | ||||
51 | ); | ||||
52 | } | ||||
53 | else { | ||||
54 | $schema->add_resolver( | ||||
55 | tag => 'tag:yaml.org,2002:str', | ||||
56 | match => [ equals => '' => '' ], | ||||
57 | implicit => 1, | ||||
58 | ); | ||||
59 | } | ||||
60 | $schema->add_resolver( | ||||
61 | tag => 'tag:yaml.org,2002:bool', | ||||
62 | match => [ equals => true => $schema->true ], | ||||
63 | ); | ||||
64 | $schema->add_resolver( | ||||
65 | tag => 'tag:yaml.org,2002:bool', | ||||
66 | match => [ equals => false => $schema->false ], | ||||
67 | ); | ||||
68 | $schema->add_resolver( | ||||
69 | tag => 'tag:yaml.org,2002:int', | ||||
70 | match => [ regex => $RE_INT => \&_to_int ], | ||||
71 | ); | ||||
72 | $schema->add_resolver( | ||||
73 | tag => 'tag:yaml.org,2002:float', | ||||
74 | match => [ regex => $RE_FLOAT => \&_to_float ], | ||||
75 | ); | ||||
76 | $schema->add_resolver( | ||||
77 | tag => 'tag:yaml.org,2002:str', | ||||
78 | match => [ all => sub { $_[1]->{value} } ], | ||||
79 | ); | ||||
80 | |||||
81 | $schema->add_representer( | ||||
82 | undefined => \&represent_undef, | ||||
83 | ); | ||||
84 | |||||
85 | my $int_flags = B::SVp_IOK; | ||||
86 | my $float_flags = B::SVp_NOK; | ||||
87 | $schema->add_representer( | ||||
88 | flags => $int_flags, | ||||
89 | code => \&represent_int, | ||||
90 | ); | ||||
91 | my %special = ( (0+'nan').'' => '.nan', (0+'inf').'' => '.inf', (0-'inf').'' => '-.inf' ); | ||||
92 | $schema->add_representer( | ||||
93 | flags => $float_flags, | ||||
94 | code => \&represent_float, | ||||
95 | ); | ||||
96 | $schema->add_representer( | ||||
97 | equals => $_, | ||||
98 | code => \&represent_literal, | ||||
99 | ) for ("", qw/ true false null /); | ||||
100 | $schema->add_representer( | ||||
101 | regex => qr{$RE_INT|$RE_FLOAT}, | ||||
102 | code => \&represent_literal, | ||||
103 | ); | ||||
104 | |||||
105 | if ($schema->bool_class) { | ||||
106 | for my $class (@{ $schema->bool_class }) { | ||||
107 | $schema->add_representer( | ||||
108 | class_equals => $class, | ||||
109 | code => \&represent_bool, | ||||
110 | ); | ||||
111 | } | ||||
112 | } | ||||
113 | |||||
114 | return; | ||||
115 | } | ||||
116 | |||||
117 | sub represent_undef { | ||||
118 | my ($rep, $node) = @_; | ||||
119 | $node->{style} = YAML_PLAIN_SCALAR_STYLE; | ||||
120 | $node->{data} = 'null'; | ||||
121 | return 1; | ||||
122 | } | ||||
123 | |||||
124 | sub represent_literal { | ||||
125 | my ($rep, $node) = @_; | ||||
126 | $node->{style} ||= YAML_SINGLE_QUOTED_SCALAR_STYLE; | ||||
127 | $node->{data} = "$node->{value}"; | ||||
128 | return 1; | ||||
129 | } | ||||
130 | |||||
131 | |||||
132 | sub represent_int { | ||||
133 | my ($rep, $node) = @_; | ||||
134 | if (int($node->{value}) ne $node->{value}) { | ||||
135 | return 0; | ||||
136 | } | ||||
137 | $node->{style} = YAML_PLAIN_SCALAR_STYLE; | ||||
138 | $node->{data} = "$node->{value}"; | ||||
139 | return 1; | ||||
140 | } | ||||
141 | |||||
142 | 1 | 2µs | my %special = ( | ||
143 | (0+'nan').'' => '.nan', | ||||
144 | (0+'inf').'' => '.inf', | ||||
145 | (0-'inf').'' => '-.inf' | ||||
146 | ); | ||||
147 | sub represent_float { | ||||
148 | my ($rep, $node) = @_; | ||||
149 | if (exists $special{ $node->{value} }) { | ||||
150 | $node->{style} = YAML_PLAIN_SCALAR_STYLE; | ||||
151 | $node->{data} = $special{ $node->{value} }; | ||||
152 | return 1; | ||||
153 | } | ||||
154 | if (0.0 + $node->{value} ne $node->{value}) { | ||||
155 | return 0; | ||||
156 | } | ||||
157 | if (int($node->{value}) eq $node->{value} and not $node->{value} =~ m/\./) { | ||||
158 | $node->{value} .= '.0'; | ||||
159 | } | ||||
160 | $node->{style} = YAML_PLAIN_SCALAR_STYLE; | ||||
161 | $node->{data} = "$node->{value}"; | ||||
162 | return 1; | ||||
163 | } | ||||
164 | |||||
165 | sub represent_bool { | ||||
166 | my ($rep, $node) = @_; | ||||
167 | my $string = $node->{value} ? 'true' : 'false'; | ||||
168 | $node->{style} = YAML_PLAIN_SCALAR_STYLE; | ||||
169 | @{ $node->{items} } = $string; | ||||
170 | $node->{data} = $string; | ||||
171 | return 1; | ||||
172 | } | ||||
173 | |||||
174 | 1 | 5µs | 1; | ||
175 | |||||
176 | __END__ | ||||
sub YAML::PP::Schema::JSON::CORE:qr; # opcode |