Traditional.xs

filter: Statementstmt 97.0% 98 / 101 Branchbran 87.0% 94 / 108 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.8% 192 / 209 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Lingua/ZH/MacChinese/Traditional.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "fmmaccnt.h"
6 #include "tomaccnt.h"
7  
8 #define PkgName "Lingua::ZH::MacChinese::Traditional"
9  
10 #define FromMbTbl fm_maccnt
11 #define ToMbTbl to_maccnt
12 #define ToMbTblC to_maccnt_contra
13  
14 #define IsMbLED(i) (0xA1<=(i) && (i)<=0xFC)
15 #define IsMbTRL(i) (0x40<=(i) && (i)<=0x7E || 0xA1<=(i) && (i)<=0xFE)
16  
17 /* Perl 5.6.1 ? */
18 #ifndef uvuni_to_utf8
19 #define uvuni_to_utf8 uv_to_utf8
20 #endif /* uvuni_to_utf8 */
21  
22 /* Perl 5.6.1 ? */
23 #ifndef utf8n_to_uvuni
24 #define utf8n_to_uvuni utf8_to_uv
25 #endif /* utf8n_to_uvuni */
26  
27 static void
28 5 sv_cat_cvref (SV *dst, SV *cv, SV *sv)
29 {
30 5 dSP;
31 int count;
32 5 ENTER;
33 5 SAVETMPS;
34 5 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 5 5
35 5 ▶ XPUSHs(sv_2mortal(sv));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(sv)); 0 5 5
36 5 PUTBACK;
37 5 count = call_sv(cv, (G_EVAL|G_SCALAR));
38 5 SPAGAIN;
39 5 ▶ if (SvTRUE(ERRSV) || count != 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || count != 1) { 5 0 5
if (SvTRUE(ERRSV) || count != 1) { 5 0 5
if (SvTRUE(ERRSV) || count != 1) { 0 5 5
40 0 croak("died in XS, " PkgName "\n");
41 }
42 5 sv_catsv(dst,POPs);
43 5 PUTBACK;
44 5 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 5 0 5
45 5 LEAVE;
46 5 }
47  
48 MODULE = Lingua::ZH::MacChinese::Traditional PACKAGE = Lingua::ZH::MacChinese::Traditional
49 PROTOTYPES: DISABLE
50  
51 void
52 decode(...)
53 ALIAS:
54 decodeMacChineseTrad = 1
55 PREINIT:
56 SV *src, *dst, *ref;
57 STRLEN srclen, mblen;
58 U8 *s, *e, *p;
59 32 bool has_cv = 0;
60 32 bool has_pv = 0;
61 STDCHAR **lb, *tb;
62 PPCODE:
63 32 ref = NULL;
64 32 ▶ if (0 < items && SvROK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < items && SvROK(ST(0))) { 31 1 32
if (0 < items && SvROK(ST(0))) { 10 21 31
65 10 ref = SvRV(ST(0));
66 10 ▶ if (SvTYPE(ref) == SVt_PVCV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(ref) == SVt_PVCV) 3 7 10
67 3 has_cv = TRUE;
68 7 ▶ else if (SvPOK(ref))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvPOK(ref)) 5 2 7
69 5 has_pv = TRUE;
70 else
71 2 croak(PkgName " 1st argument is not STRING nor CODEREF");
72 }
73 30 src = ref
74 8 ▶ ? (1 < items) ? ST(1) : &PL_sv_undef
Branch: 1 branch
BranchTrueFalseTotal
? (1 < items) ? ST(1) : &PL_sv_undef 6 2 8
75 38 ▶ : (0 < items) ? ST(0) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
: (0 < items) ? ST(0) : &PL_sv_undef; 8 22 30
: (0 < items) ? ST(0) : &PL_sv_undef; 21 1 22
76  
77 30 ▶ if (SvUTF8(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(src)) { 0 30 30
78 0 src = sv_mortalcopy(src);
79 0 sv_utf8_downgrade(src, 0);
80 }
81 30 s = (U8*)SvPV(src,srclen);
82 30 e = s + srclen;
83 30 dst = sv_2mortal(newSV(1));
84 30 (void)SvPOK_only(dst);
85 30 SvUTF8_on(dst);
86  
87 555 ▶ for (p = s; p < e; p += mblen) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e; p += mblen) { 525 30 555
88 525 ▶ mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1;
Branch: 7 branches
BranchTrueFalseTotal
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 505 20 525
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 11 494 505
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 11 0 11
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 11 0 11
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 6 5 11
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 6 0 6
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 6 0 6
89  
90 525 ▶ lb = FromMbTbl[mblen == 2 ? *p : 0];
Branch: 1 branch
BranchTrueFalseTotal
lb = FromMbTbl[mblen == 2 ? *p : 0]; 11 514 525
91 525 ▶ tb = lb ? lb[mblen == 2 ? p[1] : *p] : NULL;
Branch: 2 branches
BranchTrueFalseTotal
tb = lb ? lb[mblen == 2 ? p[1] : *p] : NULL; 520 5 525
tb = lb ? lb[mblen == 2 ? p[1] : *p] : NULL; 6 514 520
92  
93 525 ▶ if (tb) {
Branch: 1 branch
BranchTrueFalseTotal
if (tb) { 520 5 525
94 520 ▶ if (*tb)
Branch: 1 branch
BranchTrueFalseTotal
if (*tb) 517 3 520
95 517 sv_catpv(dst, (char*)tb);
96 else /* \0 to \0 */
97 3 sv_catpvn(dst, (char*)tb, 1);
98 }
99 5 ▶ else if (has_pv)
Branch: 1 branch
BranchTrueFalseTotal
else if (has_pv) 3 2 5
100 3 sv_catsv(dst, ref);
101 2 ▶ else if (has_cv)
Branch: 1 branch
BranchTrueFalseTotal
else if (has_cv) 1 1 2
102 1 sv_cat_cvref(dst, ref, newSVpvn((char*)p, mblen));
103 }
104 30 ▶ XPUSHs(dst);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dst); 0 30 30
105  
106  
107 void
108 encode(...)
109 ALIAS:
110 encodeMacChineseTrad = 1
111 PREINIT:
112 SV *src, *dst, *ref;
113 STRLEN srclen, retlen;
114 U8 *s, *e, *p, mbcbuf[3];
115 U16 mc, *t;
116 struct mbc_contra *p_contra, *cel_contra, **row_contra;
117 UV uv;
118 32 bool has_cv = 0;
119 32 bool has_pv = 0;
120 PPCODE:
121 32 ref = NULL;
122 32 ▶ if (0 < items && SvROK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < items && SvROK(ST(0))) { 31 1 32
if (0 < items && SvROK(ST(0))) { 10 21 31
123 10 ref = SvRV(ST(0));
124 10 ▶ if (SvTYPE(ref) == SVt_PVCV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(ref) == SVt_PVCV) 4 6 10
125 4 has_cv = TRUE;
126 6 ▶ else if (SvPOK(ref))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvPOK(ref)) 4 2 6
127 4 has_pv = TRUE;
128 else
129 2 croak(PkgName " 1st argument is not STRING nor CODEREF");
130 }
131 30 src = ref
132 8 ▶ ? (1 < items) ? ST(1) : &PL_sv_undef
Branch: 1 branch
BranchTrueFalseTotal
? (1 < items) ? ST(1) : &PL_sv_undef 6 2 8
133 38 ▶ : (0 < items) ? ST(0) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
: (0 < items) ? ST(0) : &PL_sv_undef; 8 22 30
: (0 < items) ? ST(0) : &PL_sv_undef; 21 1 22
134  
135 30 ▶ if (!SvUTF8(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvUTF8(src)) { 12 18 30
136 12 src = sv_mortalcopy(src);
137 12 sv_utf8_upgrade(src);
138 }
139 30 s = (U8*)SvPV(src,srclen);
140 30 e = s + srclen;
141 30 dst = sv_2mortal(newSV(1));
142 30 (void)SvPOK_only(dst);
143 30 SvUTF8_off(dst);
144  
145 565 ▶ for (p = s; p < e;) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e;) { 535 30 565
146 535 uv = utf8n_to_uvuni(p, e - p, &retlen, 0);
147 535 p += retlen;
148  
149 535 mc = 0;
150 535 ▶ row_contra = uv < 0x10000 ? ToMbTblC[uv >> 8] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
row_contra = uv < 0x10000 ? ToMbTblC[uv >> 8] : NULL; 530 5 535
151 535 ▶ cel_contra = row_contra ? row_contra[uv & 0xff] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
cel_contra = row_contra ? row_contra[uv & 0xff] : NULL; 523 12 535
152  
153 535 ▶ if (cel_contra) {
Branch: 1 branch
BranchTrueFalseTotal
if (cel_contra) { 5 530 535
154 8 ▶ for (p_contra = cel_contra; p_contra->string; p_contra++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p_contra = cel_contra; p_contra->string; p_contra++) { 5 3 8
155 5 ▶ if (p_contra->len <= (e - p) &&
Branch: 1 branch
BranchTrueFalseTotal
if (p_contra->len <= (e - p) && 5 0 5
156 5 ▶ memEQ(p, p_contra->string, p_contra->len)) {
Branch: 1 branch
BranchTrueFalseTotal
memEQ(p, p_contra->string, p_contra->len)) { 2 3 5
157 2 mc = p_contra->mchar;
158 2 p += p_contra->len;
159 2 break;
160 }
161 }
162 }
163  
164 535 ▶ if (!mc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mc) { 533 2 535
165 533 ▶ t = uv < 0x10000 ? ToMbTbl[uv >> 8] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
t = uv < 0x10000 ? ToMbTbl[uv >> 8] : NULL; 528 5 533
166 533 ▶ mc = t ? t[uv & 0xff] : 0;
Branch: 1 branch
BranchTrueFalseTotal
mc = t ? t[uv & 0xff] : 0; 523 10 533
167 }
168  
169 535 ▶ if (mc || uv == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (mc || uv == 0) { 13 522 535
if (mc || uv == 0) { 3 10 13
170 525 ▶ if (mc >= 256) {
Branch: 1 branch
BranchTrueFalseTotal
if (mc >= 256) { 6 519 525
171 6 mbcbuf[0] = (U8)(mc >> 8);
172 6 mbcbuf[1] = (U8)(mc & 0xff);
173 6 mbcbuf[2] = '\0';
174 6 sv_catpvn(dst, (char*)mbcbuf, 2);
175 }
176 else {
177 519 mbcbuf[0] = (U8)(mc & 0xff);
178 519 mbcbuf[1] = '\0';
179 519 sv_catpvn(dst, (char*)mbcbuf, 1);
180 }
181 }
182 10 ▶ else if (has_pv)
Branch: 1 branch
BranchTrueFalseTotal
else if (has_pv) 4 6 10
183 4 sv_catsv(dst, ref);
184 6 ▶ else if (has_cv)
Branch: 1 branch
BranchTrueFalseTotal
else if (has_cv) 4 2 6
185 4 sv_cat_cvref(dst, ref, newSVuv(uv));
186 }
187 30 ▶ XPUSHs(dst);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dst); 0 30 30
188  
↑ Summary blib/lib/Lingua/ZH/MacChinese/Traditional.pm »