MacKorean.xs

filter: Statementstmt 97.0% 98 / 101 Branchbran 87.9% 95 / 108 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.3% 193 / 209 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Lingua/KO/MacKorean.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "fmmacko.h"
6 #include "tomacko.h"
7  
8 #define PkgName "Lingua::KO::MacKorean"
9  
10 #define FromMbTbl fm_macko
11 #define ToMbTbl to_macko
12 #define ToMbTblC to_macko_contra
13  
14 #define IsMbLED(i) (0xA1<=(i) && (i)<=0xFE)
15 #define IsMbTRL(i) (0x41<=(i) && (i)<=0x7D || 0x81<=(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::KO::MacKorean PACKAGE = Lingua::KO::MacKorean
49 PROTOTYPES: DISABLE
50  
51 void
52 decode(...)
53 ALIAS:
54 decodeMacKorean = 1
55 PREINIT:
56 SV *src, *dst, *ref;
57 STRLEN srclen, mblen;
58 U8 *s, *e, *p;
59 35 bool has_cv = 0;
60 35 bool has_pv = 0;
61 STDCHAR **lb, *tb;
62 PPCODE:
63 35 ref = NULL;
64 35 ▶ if (0 < items && SvROK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < items && SvROK(ST(0))) { 34 1 35
if (0 < items && SvROK(ST(0))) { 10 24 34
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 33 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 41 ▶ : (0 < items) ? ST(0) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
: (0 < items) ? ST(0) : &PL_sv_undef; 8 25 33
: (0 < items) ? ST(0) : &PL_sv_undef; 24 1 25
76  
77 33 ▶ if (SvUTF8(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(src)) { 0 33 33
78 0 src = sv_mortalcopy(src);
79 0 sv_utf8_downgrade(src, 0);
80 }
81 33 s = (U8*)SvPV(src,srclen);
82 33 e = s + srclen;
83 33 dst = sv_2mortal(newSV(1));
84 33 (void)SvPOK_only(dst);
85 33 SvUTF8_on(dst);
86  
87 557 ▶ for (p = s; p < e; p += mblen) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e; p += mblen) { 524 33 557
88 524 ▶ 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; 503 21 524
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 14 489 503
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 14 0 14
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 14 0 14
mblen = 2 <= (e - p) && IsMbLED(*p) && IsMbTRL(p[1]) ? 2 : 1; 11 3 14
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
89  
90 524 ▶ lb = FromMbTbl[mblen == 2 ? *p : 0];
Branch: 1 branch
BranchTrueFalseTotal
lb = FromMbTbl[mblen == 2 ? *p : 0]; 14 510 524
91 524 ▶ tb = lb ? lb[mblen == 2 ? p[1] : *p] : NULL;
Branch: 2 branches
BranchTrueFalseTotal
tb = lb ? lb[mblen == 2 ? p[1] : *p] : NULL; 519 5 524
tb = lb ? lb[mblen == 2 ? p[1] : *p] : NULL; 9 510 519
92  
93 524 ▶ if (tb) {
Branch: 1 branch
BranchTrueFalseTotal
if (tb) { 519 5 524
94 519 ▶ if (*tb)
Branch: 1 branch
BranchTrueFalseTotal
if (*tb) 516 3 519
95 516 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 33 ▶ XPUSHs(dst);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dst); 0 33 33
105  
106  
107 void
108 encode(...)
109 ALIAS:
110 encodeMacKorean = 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 39 bool has_cv = 0;
119 39 bool has_pv = 0;
120 PPCODE:
121 39 ref = NULL;
122 39 ▶ if (0 < items && SvROK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < items && SvROK(ST(0))) { 38 1 39
if (0 < items && SvROK(ST(0))) { 10 28 38
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 37 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 45 ▶ : (0 < items) ? ST(0) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
: (0 < items) ? ST(0) : &PL_sv_undef; 8 29 37
: (0 < items) ? ST(0) : &PL_sv_undef; 28 1 29
134  
135 37 ▶ if (!SvUTF8(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvUTF8(src)) { 12 25 37
136 12 src = sv_mortalcopy(src);
137 12 sv_utf8_upgrade(src);
138 }
139 37 s = (U8*)SvPV(src,srclen);
140 37 e = s + srclen;
141 37 dst = sv_2mortal(newSV(1));
142 37 (void)SvPOK_only(dst);
143 37 SvUTF8_off(dst);
144  
145 576 ▶ for (p = s; p < e;) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e;) { 539 37 576
146 539 uv = utf8n_to_uvuni(p, e - p, &retlen, 0);
147 539 p += retlen;
148  
149 539 mc = 0;
150 539 ▶ row_contra = uv < 0x10000 ? ToMbTblC[uv >> 8] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
row_contra = uv < 0x10000 ? ToMbTblC[uv >> 8] : NULL; 534 5 539
151 539 ▶ cel_contra = row_contra ? row_contra[uv & 0xff] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
cel_contra = row_contra ? row_contra[uv & 0xff] : NULL; 529 10 539
152  
153 539 ▶ if (cel_contra) {
Branch: 1 branch
BranchTrueFalseTotal
if (cel_contra) { 87 452 539
154 491 ▶ for (p_contra = cel_contra; p_contra->string; p_contra++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p_contra = cel_contra; p_contra->string; p_contra++) { 412 79 491
155 412 ▶ if (p_contra->len <= (e - p) &&
Branch: 1 branch
BranchTrueFalseTotal
if (p_contra->len <= (e - p) && 358 54 412
156 358 ▶ memEQ(p, p_contra->string, p_contra->len)) {
Branch: 1 branch
BranchTrueFalseTotal
memEQ(p, p_contra->string, p_contra->len)) { 8 350 358
157 8 mc = p_contra->mchar;
158 8 p += p_contra->len;
159 8 break;
160 }
161 }
162 }
163  
164 539 ▶ if (!mc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mc) { 531 8 539
165 531 ▶ t = uv < 0x10000 ? ToMbTbl[uv >> 8] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
t = uv < 0x10000 ? ToMbTbl[uv >> 8] : NULL; 526 5 531
166 531 ▶ mc = t ? t[uv & 0xff] : 0;
Branch: 1 branch
BranchTrueFalseTotal
mc = t ? t[uv & 0xff] : 0; 526 5 531
167 }
168  
169 539 ▶ if (mc || uv == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (mc || uv == 0) { 13 526 539
if (mc || uv == 0) { 3 10 13
170 529 ▶ if (mc >= 256) {
Branch: 1 branch
BranchTrueFalseTotal
if (mc >= 256) { 13 516 529
171 13 mbcbuf[0] = (U8)(mc >> 8);
172 13 mbcbuf[1] = (U8)(mc & 0xff);
173 13 mbcbuf[2] = '\0';
174 13 sv_catpvn(dst, (char*)mbcbuf, 2);
175 }
176 else {
177 516 mbcbuf[0] = (U8)(mc & 0xff);
178 516 mbcbuf[1] = '\0';
179 516 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 37 ▶ XPUSHs(dst);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dst); 0 37 37
188  
↑ Summary blib/lib/Lingua/KO/MacKorean.pm »