src/xh_sort.c

filter: Statementstmt 92.8% 13 / 14 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.8% 16 / 18 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/xh_writer.h ↑ Summary src/xh_h2x_native.c »
1 #include "xh_config.h"
2 #include "xh_core.h"
3  
4 static int
5 25 xh_sort_hash_cmp(const void *p1, const void *p2)
6 {
7 25 return xh_strcmp(((xh_sort_hash_t *) p1)->key, ((xh_sort_hash_t *) p2)->key);
8 }
9  
10 xh_sort_hash_t *
11 13 xh_sort_hash(HV *hash, size_t len)
12 {
13 xh_sort_hash_t *sorted_hash;
14 size_t i;
15  
16 13 sorted_hash = malloc(sizeof(xh_sort_hash_t) * len);
17 13 ▶ if (sorted_hash == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (sorted_hash == NULL) { 0 13 13
18 0 croak("Memory allocation error");
19 }
20  
21 13 hv_iterinit(hash);
22  
23 45 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 32 13 45
24 32 sorted_hash[i].value = hv_iternextsv(hash, (char **) &sorted_hash[i].key, &sorted_hash[i].key_len);
25 }
26  
27 13 qsort(sorted_hash, len, sizeof(xh_sort_hash_t), xh_sort_hash_cmp);
28  
29 13 return sorted_hash;
30 }
31  
32 /* Callers register this with SAVEDESTRUCTOR_X so that the sorted hash is
33 * released both on the normal path and when a perl callback dies in the
34 * middle of the loop and longjmps past the end of the block. */
35 void
36 13 xh_sort_hash_free(pTHX_ void *sorted_hash)
37 {
38 PERL_UNUSED_CONTEXT;
39 13 free(sorted_hash);
40 13 }
« src/xh_writer.h ↑ Summary src/xh_h2x_native.c »