AsUTF8Bytes.xs

filter: Statementstmt 81.9% 100 / 122 Branchbran 76.9% 40 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.4% 140 / 174 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Clone/AsUTF8Bytes.pm »
1 #include <assert.h>
2  
3 #include "EXTERN.h"
4 #include "perl.h"
5 #include "XSUB.h"
6  
7 #define CLONE_KEY(x) ((char *) &x)
8  
9 #define CLONE_STORE(x,y) \
10 do { \
11 if (!hv_store(hseen, CLONE_KEY(x), PTRSIZE, SvREFCNT_inc(y), 0)) { \
12 SvREFCNT_dec(y); /* Restore the refcount */ \
13 croak("Can't store clone in seen hash (hseen)"); \
14 } \
15 else { \
16 TRACEME(("storing ref = 0x%x clone = 0x%x\n", ref, clone)); \
17 TRACEME(("clone = 0x%x(%d)\n", clone, SvREFCNT(clone))); \
18 TRACEME(("ref = 0x%x(%d)\n", ref, SvREFCNT(ref))); \
19 } \
20 } while (0)
21  
22 #define CLONE_FETCH(x) (hv_fetch(hseen, CLONE_KEY(x), PTRSIZE, 0))
23  
24 static SV *hv_clone (SV *, SV *, HV *, int);
25 static SV *av_clone (SV *, SV *, HV *, int);
26 static SV *sv_clone (SV *, HV *, int);
27 static SV *rv_clone (SV *, HV *, int);
28 static void _sv_string_to_bytes(SV *);
29  
30 #ifdef DEBUG_CLONE
31 #define TRACEME(a) printf("%s:%d: ",__FUNCTION__, __LINE__) && printf a;
32 #else
33 #define TRACEME(a)
34 #endif
35  
36 47 static void _sv_string_to_bytes(SV* sv) {
37  
38 TRACEME(("_sv_string_to_bytes called\n"));
39  
40 /* if this actually contains a string */
41 47 ▶ if (SvPOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(sv)) { 46 1 47
42 TRACEME(("Turning scalar string into bytes!\n"));
43  
44 /* if this doesn't have the utf-8 flag on already then we need to
45 upgrade this in place so that the internal byte representation of
46 this string is in UTF-8. */
47 46 ▶ if (!SvUTF8(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvUTF8(sv)) 44 2 46
48 44 sv_utf8_upgrade(sv);
49  
50 /* flipping the flag off will turn the internal utf-8 bytes that
51 make up the string into first class characters, turning the string
52 into bytes. */
53 46 SvUTF8_off(sv);
54 }
55 47 }
56  
57 static SV *
58 23 hv_clone (SV * ref, SV * target, HV* hseen, int depth)
59 {
60 23 HV *clone = (HV *) target;
61 23 HV *self = (HV *) ref;
62 23 HE *next = NULL;
63 23 ▶ int recur = depth ? depth - 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
int recur = depth ? depth - 1 : 0; 23 0 23
64  
65 assert(SvTYPE(ref) == SVt_PVHV);
66  
67 TRACEME(("ref = 0x%x(%d)\n", ref, SvREFCNT(ref)));
68  
69 23 hv_iterinit (self);
70 57 ▶ while ((next = hv_iternext (self)))
Branch: 1 branch
BranchTrueFalseTotal
while ((next = hv_iternext (self))) 34 23 57
71 {
72 34 SV *key = hv_iterkeysv (next);
73 TRACEME(("clone item %s\n", SvPV_nolen(key) ));
74 34 _sv_string_to_bytes(key);
75 34 hv_store_ent (clone, key,
76 sv_clone (hv_iterval (self, next), hseen, recur), 0);
77 }
78  
79 TRACEME(("clone = 0x%x(%d)\n", clone, SvREFCNT(clone)));
80 23 return (SV *) clone;
81 }
82  
83 static SV *
84 14 av_clone (SV * ref, SV * target, HV* hseen, int depth)
85 {
86 14 AV *clone = (AV *) target;
87 14 AV *self = (AV *) ref;
88 SV **svp;
89 14 SV *val = NULL;
90 14 I32 arrlen = 0;
91 14 int i = 0;
92 14 ▶ int recur = depth ? depth - 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
int recur = depth ? depth - 1 : 0; 14 0 14
93  
94 assert(SvTYPE(ref) == SVt_PVAV);
95  
96 TRACEME(("ref = 0x%x(%d)\n", ref, SvREFCNT(ref)));
97  
98 /* The following is a holdover from a very old version */
99 /* possible cause of memory leaks */
100 /* if ( (SvREFCNT(ref) > 1) ) */
101 /* CLONE_STORE(ref, (SV *)clone); */
102  
103 14 arrlen = av_len (self);
104 14 av_extend (clone, arrlen);
105  
106 43 ▶ for (i = 0; i <= arrlen; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= arrlen; i++) 29 14 43
107 {
108 29 svp = av_fetch (self, i, 0);
109 29 ▶ if (svp)
Branch: 1 branch
BranchTrueFalseTotal
if (svp) 29 0 29
110 29 av_store (clone, i, sv_clone (*svp, hseen, recur));
111 }
112  
113 TRACEME(("clone = 0x%x(%d)\n", clone, SvREFCNT(clone)));
114 14 return (SV *) clone;
115 }
116  
117 static SV *
118 0 rv_clone (SV * ref, HV* hseen, int depth)
119 {
120 0 SV *clone = NULL;
121 0 SV *rv = NULL;
122  
123 assert(SvROK(ref));
124  
125 TRACEME(("ref = 0x%x(%d)\n", ref, SvREFCNT(ref)));
126  
127 0 ▶ if (!SvROK (ref))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK (ref)) 0 0 0
128 0 return NULL;
129  
130 0 ▶ if (sv_isobject (ref))
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject (ref)) 0 0 0
131 {
132 0 clone = newRV_noinc(sv_clone (SvRV(ref), hseen, depth));
133 0 sv_2mortal (sv_bless (clone, SvSTASH (SvRV (ref))));
134 }
135 else
136 0 clone = newRV_inc(sv_clone (SvRV(ref), hseen, depth));
137
138 TRACEME(("clone = 0x%x(%d)\n", clone, SvREFCNT(clone)));
139 0 return clone;
140 }
141  
142 static SV *
143 158 sv_clone (SV * ref, HV* hseen, int depth)
144 {
145 158 SV *clone = ref;
146 158 SV **seen = NULL;
147 #if PERL_REVISION >= 5 && PERL_VERSION > 8
148 /* This is a hack for perl 5.9.*, save everything */
149 /* until I find out why mg_find is no longer working */
150 158 UV visible = 1;
151 #else
152 UV visible = (SvREFCNT(ref) > 1) || (SvMAGICAL(ref) && mg_find(ref, '<'));
153 #endif
154 158 int magic_ref = 0;
155  
156 TRACEME(("ref = 0x%x(%d)\n", ref, SvREFCNT(ref)));
157  
158 158 ▶ if (depth == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (depth == 0) 6 152 158
159 6 return SvREFCNT_inc(ref);
160  
161 152 ▶ if (visible && (seen = CLONE_FETCH(ref)))
Branch: 2 branches
BranchTrueFalseTotal
if (visible && (seen = CLONE_FETCH(ref))) 152 0 152
if (visible && (seen = CLONE_FETCH(ref))) 13 139 152
162 {
163 TRACEME(("fetch ref (0x%x)\n", ref));
164 13 return SvREFCNT_inc(*seen);
165 }
166  
167 TRACEME(("switch: (0x%x)\n", ref));
168 139 switch (SvTYPE (ref))
169 {
170 4 case SVt_NULL: /* 0 */
171 TRACEME(("sv_null\n"));
172 4 clone = newSVsv (ref);
173 4 break;
174 73 case SVt_IV: /* 1 */
175 TRACEME(("int scalar\n"));
176 case SVt_NV: /* 2 */
177 TRACEME(("double scalar\n"));
178 73 clone = newSVsv (ref);
179 73 break;
180 #if PERL_VERSION <= 10
181 case SVt_RV: /* 3 */
182 TRACEME(("ref scalar\n"));
183 clone = newSVsv (ref);
184 break;
185 #endif
186 13 case SVt_PV: /* 4 */
187 TRACEME(("string scalar\n"));
188 13 clone = newSVsv (ref);
189 13 _sv_string_to_bytes(clone);
190 13 break;
191 3 case SVt_PVIV: /* 5 */
192 TRACEME (("PVIV double-type\n"));
193 case SVt_PVNV: /* 6 */
194 TRACEME (("PVNV double-type\n"));
195 3 clone = newSVsv (ref);
196 3 break;
197 6 case SVt_PVMG: /* 7 */
198 TRACEME(("magic scalar\n"));
199 6 clone = newSVsv (ref);
200 6 break;
201 15 case SVt_PVAV: /* 10 */
202 TRACEME (("PVAV\n"));
203 15 clone = (SV *) newAV();
204 15 break;
205 24 case SVt_PVHV: /* 11 */
206 TRACEME (("PVHV\n"));
207 24 clone = (SV *) newHV();
208 24 break;
209 #if PERL_VERSION <= 8
210 case SVt_PVBM: /* 8 */
211 #elif PERL_VERSION >= 11
212 1 case SVt_REGEXP: /* 8 */
213 #endif
214 case SVt_PVLV: /* 9 */
215 case SVt_PVCV: /* 12 */
216 case SVt_PVGV: /* 13 */
217 case SVt_PVFM: /* 14 */
218 case SVt_PVIO: /* 15 */
219 TRACEME(("default: type = 0x%x\n", SvTYPE (ref)));
220 1 clone = SvREFCNT_inc(ref); /* just return the ref */
221 1 break;
222 0 default:
223 0 croak("unknown type: 0x%x", SvTYPE(ref));
224 }
225  
226 /**
227 * It is *vital* that this is performed *before* recursion,
228 * to properly handle circular references. cb 2001-02-06
229 */
230  
231 139 ▶ if ( visible )
Branch: 1 branch
BranchTrueFalseTotal
if ( visible ) 139 0 139
232 139 ▶ CLONE_STORE(ref,clone);
Branch: 1 branch
BranchTrueFalseTotal
CLONE_STORE(ref,clone); 0 139 139
233  
234 /*
235 * We'll assume (in the absence of evidence to the contrary) that A) a
236 * tied hash/array doesn't store its elements in the usual way (i.e.
237 * the mg->mg_object(s) take full responsibility for them) and B) that
238 * references aren't tied.
239 *
240 * If theses assumptions hold, the three options below are mutually
241 * exclusive.
242 *
243 * More precisely: 1 & 2 are probably mutually exclusive; 2 & 3 are
244 * definitely mutually exclusive; we have to test 1 before giving 2
245 * a chance; and we'll assume that 1 & 3 are mutually exclusive unless
246 * and until we can be test-cased out of our delusion.
247 *
248 * chocolateboy: 2001-05-29
249 */
250
251 /* 1: TIED */
252 139 ▶ if (SvMAGICAL(ref) )
Branch: 1 branch
BranchTrueFalseTotal
if (SvMAGICAL(ref) ) 5 134 139
253 {
254 MAGIC* mg;
255 5 MGVTBL *vtable = 0;
256  
257 10 ▶ for (mg = SvMAGIC(ref); mg; mg = mg->mg_moremagic)
Branch: 1 branch
BranchTrueFalseTotal
for (mg = SvMAGIC(ref); mg; mg = mg->mg_moremagic) 5 5 10
258 {
259 5 SV *obj = (SV *) NULL;
260 /* we don't want to clone a qr (regexp) object */
261 /* there are probably other types as well ... */
262 TRACEME(("magic type: %c\n", mg->mg_type));
263 /* Some mg_obj's can be null, don't bother cloning */
264 5 ▶ if ( mg->mg_obj != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if ( mg->mg_obj != NULL ) 4 1 5
265 {
266 4 switch (mg->mg_type)
267 {
268 0 case 'r': /* PERL_MAGIC_qr */
269 0 obj = mg->mg_obj;
270 0 break;
271 0 case 't': /* PERL_MAGIC_taint */
272 0 continue;
273 break;
274 0 case '<': /* PERL_MAGIC_backref */
275 0 continue;
276 break;
277 0 case '@': /* PERL_MAGIC_arylen_p */
278 0 continue;
279 break;
280 3 case 'P': /* PERL_MAGIC_tied */
281 case 'p': /* PERL_MAGIC_tiedelem */
282 case 'q': /* PERL_MAGIC_tiedscalar */
283 3 magic_ref++;
284 /* fall through */
285 4 default:
286 4 obj = sv_clone(mg->mg_obj, hseen, -1);
287 }
288 } else {
289 TRACEME(("magic object for type %c in NULL\n", mg->mg_type));
290 }
291 /* this is plain old magic, so do the same thing */
292 5 sv_magic(clone,
293 obj,
294 mg->mg_type,
295 mg->mg_ptr,
296 mg->mg_len);
297 }
298 /* major kludge - why does the vtable for a qr type need to be null? */
299 5 ▶ if (( mg = mg_find(clone, 'r') ))
Branch: 1 branch
BranchTrueFalseTotal
if (( mg = mg_find(clone, 'r') )) 0 5 5
300 0 mg->mg_virtual = (MGVTBL *) NULL;
301 }
302 /* 2: HASH/ARRAY - (with 'internal' elements) */
303 139 ▶ if ( magic_ref )
Branch: 1 branch
BranchTrueFalseTotal
if ( magic_ref ) 136 3 139
304 {
305 ;;
306 }
307 136 ▶ else if ( SvTYPE(ref) == SVt_PVHV )
Branch: 1 branch
BranchTrueFalseTotal
else if ( SvTYPE(ref) == SVt_PVHV ) 23 113 136
308 23 clone = hv_clone (ref, clone, hseen, depth);
309 113 ▶ else if ( SvTYPE(ref) == SVt_PVAV )
Branch: 1 branch
BranchTrueFalseTotal
else if ( SvTYPE(ref) == SVt_PVAV ) 14 99 113
310 14 clone = av_clone (ref, clone, hseen, depth);
311 /* 3: REFERENCE (inlined for speed) */
312 99 ▶ else if (SvROK (ref))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvROK (ref)) 61 38 99
313 {
314 TRACEME(("clone = 0x%x(%d)\n", clone, SvREFCNT(clone)));
315 61 SvREFCNT_dec(SvRV(clone));
316 61 SvRV(clone) = sv_clone (SvRV(ref), hseen, depth); /* Clone the referent */
317 61 ▶ if (sv_isobject (ref))
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject (ref)) 20 41 61
318 {
319 20 sv_bless (clone, SvSTASH (SvRV (ref)));
320 }
321 61 ▶ if (SvWEAKREF(ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvWEAKREF(ref)) { 3 58 61
322 3 sv_rvweaken(clone);
323 }
324 }
325  
326 TRACEME(("clone = 0x%x(%d)\n", clone, SvREFCNT(clone)));
327 139 return clone;
328 }
329  
330 MODULE = Clone::AsUTF8Bytes PACKAGE = Clone::AsUTF8Bytes
331  
332 PROTOTYPES: ENABLE
333  
334 void
335 clone_as_utf8_bytes(self, depth=-1)
336 SV *self
337 int depth
338 PREINIT:
339 30 SV *clone = &PL_sv_undef;
340 30 ▶ HV *hseen = newHV();
Branch: 1 branch
BranchTrueFalseTotal
HV *hseen = newHV(); 24 6 30
341 PPCODE:
342 TRACEME(("ref = 0x%x\n", self));
343 30 clone = sv_clone(self, hseen, depth);
344 30 hv_clear(hseen); /* Free HV */
345 30 SvREFCNT_dec((SV *)hseen);
346 30 ▶ EXTEND(SP,1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP,1); 0 30 30
347 30 PUSHs(sv_2mortal(clone));
↑ Summary blib/lib/Clone/AsUTF8Bytes.pm »