Simplified.xs

filter: Statementstmt 97.0% 98 / 101 Branchbran 87.5% 91 / 104 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.2% 189 / 205 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Lingua/ZH/MacChinese/Simplified.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "fmmaccns.h"
6 #include "tomaccns.h"
7  
8 #define PkgName "Lingua::ZH::MacChinese::Simplified"
9  
10 #define FromMbTbl fm_maccns
11 #define ToMbTbl to_maccns
12 #define ToMbTblC to_maccns_contra
13  
14 #define IsMbLED(i) (0xA1<=(i) && (i)<=0xFC)
15 #define IsMbTRL(i) (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::Simplified PACKAGE = Lingua::ZH::MacChinese::Simplified
49 PROTOTYPES: DISABLE
50  
51 void
52 decode(...)
53 ALIAS:
54 decodeMacChineseSimp = 1
55 PREINIT:
56 SV *src, *dst, *ref;
57 STRLEN srclen, mblen;
58 U8 *s, *e, *p;
59 33 bool has_cv = 0;
60 33 bool has_pv = 0;
61 STDCHAR **lb, *tb;
62 PPCODE:
63 33 ref = NULL;
64 33 ▶ if (0 < items && SvROK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < items && SvROK(ST(0))) { 32 1 33
if (0 < items && SvROK(ST(0))) { 10 22 32
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 31 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 39 ▶ : (0 < items) ? ST(0) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
: (0 < items) ? ST(0) : &PL_sv_undef; 8 23 31
: (0 < items) ? ST(0) : &PL_sv_undef; 22 1 23
76  
77 31 ▶ if (SvUTF8(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(src)) { 0 31 31
78 0 src = sv_mortalcopy(src);
79 0 sv_utf8_downgrade(src, 0);
80 }
81 31 s = (U8*)SvPV(src,srclen);
82 31 e = s + srclen;
83 31 dst = sv_2mortal(newSV(1));
84 31 (void)SvPOK_only(dst);
85 31 SvUTF8_on(dst);
86  
87 557 ▶ for (p = s; p < e; p += mblen) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e; p += mblen) { 526 31 557
88 526 ▶ mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1;
Branch: 5 branches
BranchTrueFalseTotal
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 506 20 526
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 12 494 506
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 12 0 12
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 12 0 12
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 12 0 12
89  
90 526 ▶ lb = FromMbTbl[mblen == 2 ? *p : 0];
Branch: 1 branch
BranchTrueFalseTotal
lb = FromMbTbl[mblen == 2 ? *p : 0]; 12 514 526
91 526 ▶ tb = lb ? lb[mblen == 2 ? p[1] : *p] : NULL;
Branch: 2 branches
BranchTrueFalseTotal
tb = lb ? lb[mblen == 2 ? p[1] : *p] : NULL; 521 5 526
tb = lb ? lb[mblen == 2 ? p[1] : *p] : NULL; 7 514 521
92  
93 526 ▶ if (tb) {
Branch: 1 branch
BranchTrueFalseTotal
if (tb) { 521 5 526
94 521 ▶ if (*tb)
Branch: 1 branch
BranchTrueFalseTotal
if (*tb) 518 3 521
95 518 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 31 ▶ XPUSHs(dst);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dst); 0 31 31
105  
106  
107 void
108 encode(...)
109 ALIAS:
110 encodeMacChineseSimp = 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 34 bool has_cv = 0;
119 34 bool has_pv = 0;
120 PPCODE:
121 34 ref = NULL;
122 34 ▶ if (0 < items && SvROK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < items && SvROK(ST(0))) { 33 1 34
if (0 < items && SvROK(ST(0))) { 10 23 33
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 32 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 40 ▶ : (0 < items) ? ST(0) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
: (0 < items) ? ST(0) : &PL_sv_undef; 8 24 32
: (0 < items) ? ST(0) : &PL_sv_undef; 23 1 24
134  
135 32 ▶ if (!SvUTF8(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvUTF8(src)) { 12 20 32
136 12 src = sv_mortalcopy(src);
137 12 sv_utf8_upgrade(src);
138 }
139 32 s = (U8*)SvPV(src,srclen);
140 32 e = s + srclen;
141 32 dst = sv_2mortal(newSV(1));
142 32 (void)SvPOK_only(dst);
143 32 SvUTF8_off(dst);
144  
145 569 ▶ for (p = s; p < e;) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e;) { 537 32 569
146 537 uv = utf8n_to_uvuni(p, e - p, &retlen, 0);
147 537 p += retlen;
148  
149 537 mc = 0;
150 537 ▶ row_contra = uv < 0x10000 ? ToMbTblC[uv >> 8] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
row_contra = uv < 0x10000 ? ToMbTblC[uv >> 8] : NULL; 532 5 537
151 537 ▶ cel_contra = row_contra ? row_contra[uv & 0xff] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
cel_contra = row_contra ? row_contra[uv & 0xff] : NULL; 523 14 537
152  
153 537 ▶ if (cel_contra) {
Branch: 1 branch
BranchTrueFalseTotal
if (cel_contra) { 6 531 537
154 9 ▶ for (p_contra = cel_contra; p_contra->string; p_contra++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p_contra = cel_contra; p_contra->string; p_contra++) { 6 3 9
155 6 ▶ if (p_contra->len <= (e - p) &&
Branch: 1 branch
BranchTrueFalseTotal
if (p_contra->len <= (e - p) && 6 0 6
156 6 ▶ memEQ(p, p_contra->string, p_contra->len)) {
Branch: 1 branch
BranchTrueFalseTotal
memEQ(p, p_contra->string, p_contra->len)) { 3 3 6
157 3 mc = p_contra->mchar;
158 3 p += p_contra->len;
159 3 break;
160 }
161 }
162 }
163  
164 537 ▶ if (!mc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mc) { 534 3 537
165 534 ▶ t = uv < 0x10000 ? ToMbTbl[uv >> 8] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
t = uv < 0x10000 ? ToMbTbl[uv >> 8] : NULL; 529 5 534
166 534 ▶ mc = t ? t[uv & 0xff] : 0;
Branch: 1 branch
BranchTrueFalseTotal
mc = t ? t[uv & 0xff] : 0; 529 5 534
167 }
168  
169 537 ▶ if (mc || uv == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (mc || uv == 0) { 13 524 537
if (mc || uv == 0) { 3 10 13
170 527 ▶ if (mc >= 256) {
Branch: 1 branch
BranchTrueFalseTotal
if (mc >= 256) { 8 519 527
171 8 mbcbuf[0] = (U8)(mc >> 8);
172 8 mbcbuf[1] = (U8)(mc & 0xff);
173 8 mbcbuf[2] = '\0';
174 8 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 32 ▶ XPUSHs(dst);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dst); 0 32 32
188  
↑ Summary blib/lib/Lingua/ZH/MacChinese/Simplified.pm »