MacJapanese.xs

filter: Statementstmt 97.3% 111 / 114 Branchbran 87.3% 110 / 126 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.0% 221 / 240 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Lingua/JA/MacJapanese.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "fmmacja.h"
6 #include "tomacja.h"
7  
8 #define PkgName "Lingua::JA::MacJapanese"
9  
10 #define FromMbTbl fm_macja
11 #define ToMbTbl to_macja
12 #define ToMbTblC to_macja_contra
13  
14 #define IsMbLED(i) (0x81<=(i) && (i)<=0x9F || 0xE0<=(i) && (i)<=0xFC)
15 #define IsMbTRL(i) (0x40<=(i) && (i)<=0x7E || 0x80<=(i) && (i)<=0xFC)
16  
17 #define Is_SJIS_UDC(i) (0xF0<=(i) && (i)<=0xFC)
18 #define Is_SJIS_PUA(uv) (0xE000 <= (uv) && (uv) <= 0xE98B)
19 #define SJIS_PUA_BASE (0xE000)
20  
21 /* Perl 5.6.1 ? */
22 #ifndef uvuni_to_utf8
23 #define uvuni_to_utf8 uv_to_utf8
24 #endif /* uvuni_to_utf8 */
25  
26 /* Perl 5.6.1 ? */
27 #ifndef utf8n_to_uvuni
28 #define utf8n_to_uvuni utf8_to_uv
29 #endif /* utf8n_to_uvuni */
30  
31 static void
32 5 sv_cat_cvref (SV *dst, SV *cv, SV *sv)
33 {
34 5 dSP;
35 int count;
36 5 ENTER;
37 5 SAVETMPS;
38 5 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 5 5
39 5 ▶ XPUSHs(sv_2mortal(sv));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(sv)); 0 5 5
40 5 PUTBACK;
41 5 count = call_sv(cv, (G_EVAL|G_SCALAR));
42 5 SPAGAIN;
43 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
44 0 croak("died in XS, " PkgName "\n");
45 }
46 5 sv_catsv(dst,POPs);
47 5 PUTBACK;
48 5 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 5 0 5
49 5 LEAVE;
50 5 }
51  
52 MODULE = Lingua::JA::MacJapanese PACKAGE = Lingua::JA::MacJapanese
53 PROTOTYPES: DISABLE
54  
55 void
56 decode(...)
57 ALIAS:
58 decodeMacJapanese = 1
59 PREINIT:
60 SV *src, *dst, *ref;
61 STRLEN srclen, mblen;
62 U8 *s, *e, *p;
63 50 bool has_cv = 0;
64 50 bool has_pv = 0;
65 STDCHAR **lb, *tb;
66 UV uv;
67 U8 unibuf[UTF8_MAXLEN + 1];
68 STRLEN ulen;
69 PPCODE:
70 50 ref = NULL;
71 50 ▶ if (0 < items && SvROK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < items && SvROK(ST(0))) { 49 1 50
if (0 < items && SvROK(ST(0))) { 10 39 49
72 10 ref = SvRV(ST(0));
73 10 ▶ if (SvTYPE(ref) == SVt_PVCV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(ref) == SVt_PVCV) 3 7 10
74 3 has_cv = TRUE;
75 7 ▶ else if (SvPOK(ref))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvPOK(ref)) 5 2 7
76 5 has_pv = TRUE;
77 else
78 2 croak(PkgName " 1st argument is not STRING nor CODEREF");
79 }
80 48 src = ref
81 8 ▶ ? (1 < items) ? ST(1) : &PL_sv_undef
Branch: 1 branch
BranchTrueFalseTotal
? (1 < items) ? ST(1) : &PL_sv_undef 6 2 8
82 56 ▶ : (0 < items) ? ST(0) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
: (0 < items) ? ST(0) : &PL_sv_undef; 8 40 48
: (0 < items) ? ST(0) : &PL_sv_undef; 39 1 40
83  
84 48 ▶ if (SvUTF8(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(src)) { 0 48 48
85 0 src = sv_mortalcopy(src);
86 0 sv_utf8_downgrade(src, 0);
87 }
88 48 s = (U8*)SvPV(src,srclen);
89 48 e = s + srclen;
90 48 dst = sv_2mortal(newSV(1));
91 48 (void)SvPOK_only(dst);
92 48 SvUTF8_on(dst);
93  
94 384 ▶ for (p = s; p < e; p += mblen) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e; p += mblen) { 336 48 384
95 336 ▶ mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1;
Branch: 9 branches
BranchTrueFalseTotal
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 316 20 336
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 33 283 316
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 26 7 33
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 26 283 309
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 26 0 26
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 33 0 33
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 20 13 33
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 20 0 20
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 20 0 20
96  
97 336 ▶ if (mblen == 2 && Is_SJIS_UDC(*(p))) {
Branch: 3 branches
BranchTrueFalseTotal
if (mblen == 2 && Is_SJIS_UDC(*(p))) { 33 303 336
if (mblen == 2 && Is_SJIS_UDC(*(p))) { 19 14 33
if (mblen == 2 && Is_SJIS_UDC(*(p))) { 19 0 19
98 38 uv = SJIS_PUA_BASE + 188 * (*(p) - 0xF0) +
99 19 ▶ (p)[1] - ((p)[1] > 0x7E ? 0x41 : 0x40);
Branch: 1 branch
BranchTrueFalseTotal
(p)[1] - ((p)[1] > 0x7E ? 0x41 : 0x40); 10 9 19
100 19 ulen = uvuni_to_utf8((U8*)unibuf, uv) - unibuf;
101 19 sv_catpvn(dst, (char*)unibuf, ulen);
102 19 continue;
103 }
104  
105 317 ▶ lb = FromMbTbl[mblen == 2 ? *p : 0];
Branch: 1 branch
BranchTrueFalseTotal
lb = FromMbTbl[mblen == 2 ? *p : 0]; 14 303 317
106 317 ▶ tb = lb ? lb[mblen == 2 ? p[1] : *p] : NULL;
Branch: 2 branches
BranchTrueFalseTotal
tb = lb ? lb[mblen == 2 ? p[1] : *p] : NULL; 312 5 317
tb = lb ? lb[mblen == 2 ? p[1] : *p] : NULL; 9 303 312
107  
108 317 ▶ if (tb) {
Branch: 1 branch
BranchTrueFalseTotal
if (tb) { 312 5 317
109 312 ▶ if (*tb)
Branch: 1 branch
BranchTrueFalseTotal
if (*tb) 310 2 312
110 310 sv_catpv(dst, (char*)tb);
111 else /* \0 to \0 */
112 2 sv_catpvn(dst, (char*)tb, 1);
113 }
114 5 ▶ else if (has_pv)
Branch: 1 branch
BranchTrueFalseTotal
else if (has_pv) 3 2 5
115 3 sv_catsv(dst, ref);
116 2 ▶ else if (has_cv)
Branch: 1 branch
BranchTrueFalseTotal
else if (has_cv) 1 1 2
117 1 sv_cat_cvref(dst, ref, newSVpvn((char*)p, mblen));
118 }
119 48 ▶ XPUSHs(dst);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dst); 0 48 48
120  
121  
122 void
123 encode(...)
124 ALIAS:
125 encodeMacJapanese = 1
126 PREINIT:
127 SV *src, *dst, *ref;
128 STRLEN srclen, retlen;
129 U8 *s, *e, *p, mbcbuf[3];
130 U16 mc, *t;
131 struct mbc_contra *p_contra, *cel_contra, **row_contra;
132 UV uv;
133 51 bool has_cv = 0;
134 51 bool has_pv = 0;
135 PPCODE:
136 51 ref = NULL;
137 51 ▶ if (0 < items && SvROK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < items && SvROK(ST(0))) { 50 1 51
if (0 < items && SvROK(ST(0))) { 10 40 50
138 10 ref = SvRV(ST(0));
139 10 ▶ if (SvTYPE(ref) == SVt_PVCV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(ref) == SVt_PVCV) 4 6 10
140 4 has_cv = TRUE;
141 6 ▶ else if (SvPOK(ref))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvPOK(ref)) 4 2 6
142 4 has_pv = TRUE;
143 else
144 2 croak(PkgName " 1st argument is not STRING nor CODEREF");
145 }
146 49 src = ref
147 8 ▶ ? (1 < items) ? ST(1) : &PL_sv_undef
Branch: 1 branch
BranchTrueFalseTotal
? (1 < items) ? ST(1) : &PL_sv_undef 6 2 8
148 57 ▶ : (0 < items) ? ST(0) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
: (0 < items) ? ST(0) : &PL_sv_undef; 8 41 49
: (0 < items) ? ST(0) : &PL_sv_undef; 40 1 41
149  
150 49 ▶ if (!SvUTF8(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvUTF8(src)) { 13 36 49
151 13 src = sv_mortalcopy(src);
152 13 sv_utf8_upgrade(src);
153 }
154 49 s = (U8*)SvPV(src,srclen);
155 49 e = s + srclen;
156 49 dst = sv_2mortal(newSV(1));
157 49 (void)SvPOK_only(dst);
158 49 SvUTF8_off(dst);
159  
160 396 ▶ for (p = s; p < e;) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e;) { 347 49 396
161 347 uv = utf8n_to_uvuni(p, e - p, &retlen, 0);
162 347 p += retlen;
163  
164 347 ▶ if (Is_SJIS_PUA(uv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Is_SJIS_PUA(uv)) { 28 319 347
if (Is_SJIS_PUA(uv)) { 19 9 28
165 19 uv -= SJIS_PUA_BASE;
166 19 mbcbuf[0] = (U8)((uv / 188) + 0xF0);
167 19 ▶ mbcbuf[1] = (U8)(uv % 188 + (uv % 188 > 0x3E ? 0x41 : 0x40));
Branch: 1 branch
BranchTrueFalseTotal
mbcbuf[1] = (U8)(uv % 188 + (uv % 188 > 0x3E ? 0x41 : 0x40)); 10 9 19
168 19 mbcbuf[2] = '\0';
169 19 sv_catpvn(dst, (char*)mbcbuf, 2);
170 19 continue;
171 }
172  
173 328 mc = 0;
174 328 ▶ row_contra = uv < 0x10000 ? ToMbTblC[uv >> 8] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
row_contra = uv < 0x10000 ? ToMbTblC[uv >> 8] : NULL; 323 5 328
175 328 ▶ cel_contra = row_contra ? row_contra[uv & 0xff] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
cel_contra = row_contra ? row_contra[uv & 0xff] : NULL; 11 317 328
176  
177 328 ▶ if (cel_contra) {
Branch: 1 branch
BranchTrueFalseTotal
if (cel_contra) { 4 324 328
178 7 ▶ for (p_contra = cel_contra; p_contra->string; p_contra++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p_contra = cel_contra; p_contra->string; p_contra++) { 7 0 7
179 7 ▶ if (p_contra->len <= (e - p) &&
Branch: 1 branch
BranchTrueFalseTotal
if (p_contra->len <= (e - p) && 4 3 7
180 4 ▶ memEQ(p, p_contra->string, p_contra->len)) {
Branch: 1 branch
BranchTrueFalseTotal
memEQ(p, p_contra->string, p_contra->len)) { 4 0 4
181 4 mc = p_contra->mchar;
182 4 p += p_contra->len;
183 4 break;
184 }
185 }
186 }
187  
188 328 ▶ if (!mc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mc) { 324 4 328
189 324 ▶ t = uv < 0x10000 ? ToMbTbl[uv >> 8] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
t = uv < 0x10000 ? ToMbTbl[uv >> 8] : NULL; 319 5 324
190 324 ▶ mc = t ? t[uv & 0xff] : 0;
Branch: 1 branch
BranchTrueFalseTotal
mc = t ? t[uv & 0xff] : 0; 314 10 324
191 }
192  
193 328 ▶ if (mc || uv == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (mc || uv == 0) { 12 316 328
if (mc || uv == 0) { 2 10 12
194 318 ▶ if (mc >= 256) {
Branch: 1 branch
BranchTrueFalseTotal
if (mc >= 256) { 10 308 318
195 10 mbcbuf[0] = (U8)(mc >> 8);
196 10 mbcbuf[1] = (U8)(mc & 0xff);
197 10 mbcbuf[2] = '\0';
198 10 sv_catpvn(dst, (char*)mbcbuf, 2);
199 }
200 else {
201 308 mbcbuf[0] = (U8)(mc & 0xff);
202 308 mbcbuf[1] = '\0';
203 308 sv_catpvn(dst, (char*)mbcbuf, 1);
204 }
205 }
206 10 ▶ else if (has_pv)
Branch: 1 branch
BranchTrueFalseTotal
else if (has_pv) 4 6 10
207 4 sv_catsv(dst, ref);
208 6 ▶ else if (has_cv)
Branch: 1 branch
BranchTrueFalseTotal
else if (has_cv) 4 2 6
209 4 sv_cat_cvref(dst, ref, newSVuv(uv));
210 }
211 49 ▶ XPUSHs(dst);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dst); 0 49 49
212  
↑ Summary blib/lib/Lingua/JA/MacJapanese.pm »