XS.xs

filter: Statementstmt 63.1% 65 / 103 Branchbran 48.6% 36 / 74 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 57.0% 101 / 177 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/HTTP/Headers/Fast/XS.pm »
1 #define PERL_NO_GET_CONTEXT /* we want efficiency */
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "ppport.h"
6  
7 #include <string.h>
8  
9 #define MY_CXT_KEY "HTTP::Headers::Fast::XS::_guts" XS_VERSION
10  
11 typedef struct {
12 HV *standard_case;
13 SV **translate;
14 } my_cxt_t;
15  
16 START_MY_CXT;
17  
18 21 void translate_underscore(pTHX_ char *field, int len) {
19 dMY_CXT;
20 int i;
21 21 SV *translate = GvSV( *MY_CXT.translate );
22  
23 21 ▶ if (!translate || field[0] == ':')
Branch: 2 branches
BranchTrueFalseTotal
if (!translate || field[0] == ':') 21 0 21
if (!translate || field[0] == ':') 0 21 21
24 0 croak("$TRANSLATE_UNDERSCORE variable does not exist");
25  
26 21 ▶ if ( !SvOK(translate) || !SvTRUE(translate) )
Branch: 2 branches
BranchTrueFalseTotal
if ( !SvOK(translate) || !SvTRUE(translate) ) 21 0 21
if ( !SvOK(translate) || !SvTRUE(translate) ) 2 19 21
27 2 return;
28  
29 109 ▶ for ( i = 0; i < len; i++ )
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 0; i < len; i++ ) 90 19 109
30 90 ▶ if ( field[i] == '_' )
Branch: 1 branch
BranchTrueFalseTotal
if ( field[i] == '_' ) 6 84 90
31 6 field[i] = '-';
32 }
33  
34  
35 21 void handle_standard_case(pTHX_ char *field, int len) {
36 dMY_CXT;
37 char *orig;
38 bool word_boundary;
39 int i;
40 SV **standard_case_val;
41  
42 /* leading ':' means "don't standardize" */
43 21 ▶ if ( field[0] == ':' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( field[0] == ':' ) { 0 21 21
44 0 return;
45 }
46  
47 21 translate_underscore(aTHX_ field, len);
48  
49 /* make a copy to represent the original one */
50 21 orig = (char *) alloca(len + 1);
51  
52 /* copy and lc */
53 135 ▶ for ( i = 0; i < len; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 0; i < len; i++ ) { 114 21 135
54 114 orig[i] = field[i];
55 114 field[i] = tolower( field[i] );
56 }
57  
58 21 orig[len] = '\0';
59  
60 /* if we already have a value in the hash table, nothing to do */
61 21 standard_case_val = hv_fetch(
62 MY_CXT.standard_case, field, len, 1
63 );
64  
65 21 ▶ if (!standard_case_val)
Branch: 1 branch
BranchTrueFalseTotal
if (!standard_case_val) 0 21 21
66 0 croak("hv_fetch() failed. This should not happen.");
67  
68 21 ▶ if ( SvOK(*standard_case_val) )
Branch: 1 branch
BranchTrueFalseTotal
if ( SvOK(*standard_case_val) ) 15 6 21
69 15 return;
70  
71 /* uc first char after word boundary */
72 6 word_boundary = true;
73 54 ▶ for (i = 0; i < len; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++ ) { 48 6 54
74 48 ▶ if (word_boundary) {
Branch: 1 branch
BranchTrueFalseTotal
if (word_boundary) { 9 39 48
75 9 orig[i] = toupper( orig[i] );
76 }
77  
78 48 word_boundary = !isWORDCHAR( orig[i] );
79 }
80  
81 /* save result in hash table */
82 6 *standard_case_val = newSVpv( orig, len );
83 }
84  
85 /* Returns if we store that field name or not */
86 12 bool put_header_value_on_perl_stack(pTHX_ SV *self, char *field, STRLEN len) {
87 12 dSP;
88 SV **h, **a_value;
89 AV *av_entry;
90 int top_index, i;
91  
92 12 h = hv_fetch( (HV *) SvRV(self), field, len, 0 );
93 12 ▶ if ( h == NULL || !SvOK(*h) )
Branch: 2 branches
BranchTrueFalseTotal
if ( h == NULL || !SvOK(*h) ) 5 7 12
if ( h == NULL || !SvOK(*h) ) 0 5 5
94 /* If the field is not found, don't put anything on stack -> that will return () to perl */
95 7 return false;
96
97 5 ▶ if ( SvROK(*h) && SvTYPE( SvRV(*h) ) == SVt_PVAV ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( SvROK(*h) && SvTYPE( SvRV(*h) ) == SVt_PVAV ) { 3 2 5
if ( SvROK(*h) && SvTYPE( SvRV(*h) ) == SVt_PVAV ) { 3 0 3
98 /* If the value is an array, put all the values of the array on stack. This will return @$h to perl */
99 3 av_entry = (AV *) SvRV(*h);
100 3 top_index = av_len(av_entry);
101 3 ▶ EXTEND(SP, top_index);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, top_index); 3 0 3
EXTEND(SP, top_index); 0 3 3
102  
103 11 ▶ for (i = 0; i <= top_index; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= top_index; i++) { 8 3 11
104 8 a_value = av_fetch( av_entry, i, 0 );
105  
106 8 ▶ if ( !a_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !a_value ) { 0 8 8
107 0 croak("av_fetch() failed. This should not happen.");
108 }
109  
110 8 PUSHs(sv_2mortal(newSVsv(*a_value)));
111 }
112 } else {
113 /* If we have one value, just put it on stack. This will return ($h) to perl */
114 2 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 2 2
115 2 PUSHs(sv_2mortal(newSVsv(*h)));
116 }
117  
118 /* put the local SP in THX -> SP was EXTENDED */
119 5 PUTBACK;
120 5 return true;
121 }
122  
123 0 void __push_header(pTHX_ HV *self, char *field, STRLEN len, SV *val) {
124 SV **h;
125 AV *h_copy;
126 SV **a_value;
127 int i, top_index;
128  
129 0 h = hv_fetch( self, field, len, 1 );
130 0 ▶ if ( h == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( h == NULL ) 0 0 0
131 0 croak("hv_fetch() failed. This should not happen.");
132  
133 0 ▶ if ( ! SvOK(*h) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ! SvOK(*h) ) { 0 0 0
134 0 *h = newRV_noinc( (SV *) newAV() );
135 0 ▶ } else if ( ! SvROK(*h) || SvTYPE( SvRV(*h) ) != SVt_PVAV ) {
Branch: 2 branches
BranchTrueFalseTotal
} else if ( ! SvROK(*h) || SvTYPE( SvRV(*h) ) != SVt_PVAV ) { 0 0 0
} else if ( ! SvROK(*h) || SvTYPE( SvRV(*h) ) != SVt_PVAV ) { 0 0 0
136 0 h_copy = av_make( 1, h );
137 0 *h = newRV_noinc( (SV *)h_copy );
138 }
139  
140 0 ▶ if ( SvROK(val) && SvTYPE( SvRV(val) ) == SVt_PVAV ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( SvROK(val) && SvTYPE( SvRV(val) ) == SVt_PVAV ) { 0 0 0
if ( SvROK(val) && SvTYPE( SvRV(val) ) == SVt_PVAV ) { 0 0 0
141 0 h_copy = (AV *) SvRV(val);
142 0 top_index = av_len(h_copy);
143  
144 0 ▶ for ( i = 0; i <= top_index; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 0; i <= top_index; i++ ) { 0 0 0
145 0 a_value = av_fetch( h_copy, i, 0 );
146 0 ▶ if (a_value)
Branch: 1 branch
BranchTrueFalseTotal
if (a_value) 0 0 0
147 0 av_push( (AV *) SvRV(*h), *a_value );
148 }
149 } else {
150 0 av_push( (AV *) SvRV(*h), val );
151 }
152 0 }
153  
154 MODULE = HTTP::Headers::Fast::XS PACKAGE = HTTP::Headers::Fast::XS
155 PROTOTYPES: DISABLE
156  
157 BOOT:
158 {
159 MY_CXT_INIT;
160 3 MY_CXT.standard_case = get_hv( "HTTP::Headers::Fast::standard_case", 0 );
161 3 MY_CXT.translate = hv_fetch(
162 gv_stashpvn( "HTTP::Headers::Fast", 19, 0 ),
163 "TRANSLATE_UNDERSCORE",
164 20,
165 0
166 );
167 }
168  
169 char *
170 _standardize_field_name(SV *field)
171 PREINIT:
172 char *field_name;
173 STRLEN len;
174 CODE:
175 9 field_name = SvPV(field, len);
176 9 handle_standard_case(aTHX_ field_name, len);
177 9 RETVAL = field_name;
178 OUTPUT: RETVAL
179  
180 void
181 push_header( SV *self, ... )
182 PREINIT:
183 int i;
184 STRLEN len;
185 char *field;
186 SV *val;
187 CODE:
188 0 ▶ if ( items % 2 == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( items % 2 == 0 ) 0 0 0
189 0 croak("You must provide key/value pairs");
190  
191 0 ▶ for ( i = 1; i < items; i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 1; i < items; i += 2 ) { 0 0 0
192 0 field = SvPV(ST(i), len);
193 0 val = newSVsv( ST( i + 1 ) );
194  
195 0 handle_standard_case(aTHX_ field, len);
196  
197 0 __push_header(aTHX_ (HV *) SvRV(self), field, len, val);
198 }
199  
200  
201 void
202 _header_get( SV *self, SV *field_name, ... )
203 PREINIT:
204 char *field;
205 STRLEN len;
206 bool skip_standardize;
207 PPCODE:
208 5 field = SvPV(field_name, len);
209 5 ▶ skip_standardize = ( items == 3 ) && SvTRUE(ST(3));
Branch: 2 branches
BranchTrueFalseTotal
skip_standardize = ( items == 3 ) && SvTRUE(ST(3)); 0 5 5
skip_standardize = ( items == 3 ) && SvTRUE(ST(3)); 0 0 0
210 5 ▶ if (!skip_standardize)
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_standardize) 5 0 5
211 5 handle_standard_case(aTHX_ field, len);
212  
213 /* we are putting the decremented(with the number of input parameters) SP back in the THX */
214 5 PUTBACK;
215  
216 5 put_header_value_on_perl_stack(aTHX_ self, field, len);
217  
218 /* we are setting the local SP variable to the value in THX(it was changed inside the previous function call) */
219 5 SPAGAIN;
220  
221  
222 void
223 _header_set(SV *self, SV *field_name, SV *val)
224 PREINIT:
225 char *field;
226 STRLEN len;
227 bool found;
228 SV **a_value;
229 PPCODE:
230 7 field = SvPV(field_name, len);
231  
232 7 handle_standard_case(aTHX_ field, len);
233  
234 /* we are putting the decremented(with the number of input parameters) SP back in the THX */
235 7 PUTBACK;
236  
237 7 found = put_header_value_on_perl_stack(aTHX_ self, field, len);
238  
239 /* we are setting the local SP variable to the value in THX */
240 7 SPAGAIN;
241  
242 7 ▶ if (!SvOK(val) && found) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(val) && found) { 0 7 7
if (!SvOK(val) && found) { 0 0 0
243 0 hv_delete((HV *) SvRV(self), field, len, G_DISCARD);
244 } else {
245 /* av_len == 0 here means that we have one item in av */
246 7 ▶ if ( SvROK(val) &&
Branch: 1 branch
BranchTrueFalseTotal
if ( SvROK(val) && 1 6 7
247 2 SvTYPE( SvRV(val) ) == SVt_PVAV &&
248 1 av_len((AV *)SvRV(val)) == 0) {
249 0 a_value = av_fetch( (AV *)SvRV(val), 0, 0 );
250 0 val = *a_value;
251 }
252 7 hv_store( (HV *) SvRV(self), field, len, newSVsv(val), 0);
253 }
254  
255 void
256 _header_push(SV *self, SV *field_name, SV *val)
257 PREINIT:
258 char *field;
259 STRLEN len;
260 PPCODE:
261 0 field = SvPV(field_name, len);
262  
263 0 handle_standard_case(aTHX_ field, len);
264  
265 /* we are putting the decremented (with the number of
266 input parameters) SP back in the THX */
267 0 PUTBACK;
268  
269 0 put_header_value_on_perl_stack(aTHX_ self, field, len);
270  
271 /* we are setting the local SP variable to the value in THX */
272 0 SPAGAIN;
273  
274 0 __push_header(aTHX_ (HV *) SvRV(self), field, len, newSVsv(val));
↑ Summary blib/lib/HTTP/Headers/Fast/XS.pm »