MacFarsi.xs

filter: Statementstmt 95.7% 89 / 93 Branchbran 76.1% 64 / 84 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.4% 153 / 177 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Lingua/FA/MacFarsi.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "fmmacfa.h"
6 #include "tomacfa.h"
7  
8 #define PkgName "Lingua::FA::MacFarsi"
9  
10 /* Perl 5.6.1 ? */
11 #ifndef uvuni_to_utf8
12 #define uvuni_to_utf8 uv_to_utf8
13 #endif /* uvuni_to_utf8 */
14  
15 /* Perl 5.6.1 ? */
16 #ifndef utf8n_to_uvuni
17 #define utf8n_to_uvuni utf8_to_uv
18 #endif /* utf8n_to_uvuni */
19  
20 #define SBCS_LEN 1
21  
22 #define FromMacTbl fm_macfa_tbl
23 #define FromMacDir fm_macfa_dir
24 #define ToMacTbl to_macfa_table
25 #define ToMacTblN to_macfa_N
26 #define ToMacTblL to_macfa_L
27 #define ToMacTblR to_macfa_R
28  
29 static STDCHAR ** ToMacTbl [] = {
30 ToMacTblN,
31 ToMacTblL,
32 ToMacTblR
33 };
34  
35 static void
36 4 sv_cat_cvref (SV *dst, SV *cv, SV *sv)
37 {
38 4 dSP;
39 int count;
40 4 ENTER;
41 4 SAVETMPS;
42 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
43 4 ▶ XPUSHs(sv_2mortal(sv));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(sv)); 0 4 4
44 4 PUTBACK;
45 4 count = call_sv(cv, (G_EVAL|G_SCALAR));
46 4 SPAGAIN;
47 4 ▶ if (SvTRUE(ERRSV) || count != 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || count != 1) { 4 0 4
if (SvTRUE(ERRSV) || count != 1) { 4 0 4
if (SvTRUE(ERRSV) || count != 1) { 0 4 4
48 0 croak("died in XS, " PkgName "\n");
49 }
50 4 sv_catsv(dst,POPs);
51 4 PUTBACK;
52 4 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 4 0 4
53 4 LEAVE;
54 4 }
55  
56 MODULE = Lingua::FA::MacFarsi PACKAGE = Lingua::FA::MacFarsi
57 PROTOTYPES: DISABLE
58  
59 void
60 decode(...)
61 ALIAS:
62 decodeMacFarsi = 1
63 PREINIT:
64 SV *src, *dst;
65 STRLEN srclen;
66 U8 *s, *e, *p, uni[UTF8_MAXLEN + 1];
67 UV uv;
68 STDCHAR curdir, predir;
69 PPCODE:
70 284 ▶ if (0 < items && SvROK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < items && SvROK(ST(0))) { 283 1 284
if (0 < items && SvROK(ST(0))) { 6 277 283
71 6 croak(PkgName " 1st argument is REF, but handler for decode is NG.");
72 }
73 278 ▶ src = (0 < items) ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
src = (0 < items) ? ST(0) : &PL_sv_undef; 277 1 278
74  
75 278 ▶ if (SvUTF8(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(src)) { 1 277 278
76 1 src = sv_mortalcopy(src);
77 1 sv_utf8_downgrade(src, 0);
78 }
79 278 s = (U8*)SvPV(src,srclen);
80 278 e = s + srclen;
81 278 dst = sv_2mortal(newSV(1));
82 278 (void)SvPOK_only(dst);
83 278 SvUTF8_on(dst);
84  
85 278 predir = MACBIDI_DIR_NT;
86 585 ▶ for (p = s; p < e; p++, predir = curdir) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e; p++, predir = curdir) { 307 278 585
87 307 curdir = FromMacDir[*p];
88  
89 307 ▶ if (predir != curdir) {
Branch: 1 branch
BranchTrueFalseTotal
if (predir != curdir) { 76 231 307
90 76 ▶ if (predir != MACBIDI_DIR_NT) {
Branch: 1 branch
BranchTrueFalseTotal
if (predir != MACBIDI_DIR_NT) { 0 76 76
91 0 uv = MACBIDI_UV_PDF;
92 0 (void)uvuni_to_utf8(uni, uv);
93 0 ▶ sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv));
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv)); 0 0 0
sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv)); 0 0 0
94 }
95 76 ▶ if (curdir != MACBIDI_DIR_NT) {
Branch: 1 branch
BranchTrueFalseTotal
if (curdir != MACBIDI_DIR_NT) { 76 0 76
96 76 ▶ uv = (curdir == MACBIDI_DIR_LR) ? MACBIDI_UV_LRO :
Branch: 2 branches
BranchTrueFalseTotal
uv = (curdir == MACBIDI_DIR_LR) ? MACBIDI_UV_LRO : 44 32 76
uv = (curdir == MACBIDI_DIR_LR) ? MACBIDI_UV_LRO : 44 0 44
97 (curdir == MACBIDI_DIR_RL) ? MACBIDI_UV_RLO :
98 0; /* Panic: undefined direction in decode" */;
99 76 (void)uvuni_to_utf8(uni, uv);
100 76 ▶ sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv));
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv)); 76 0 76
sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv)); 76 0 76
101 }
102 }
103  
104 307 uv = FromMacTbl[*p];
105 307 (void)uvuni_to_utf8(uni, uv);
106 307 ▶ sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv));
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv)); 116 191 307
sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv)); 116 0 116
107 }
108  
109 278 ▶ if (predir != MACBIDI_DIR_NT) {
Branch: 1 branch
BranchTrueFalseTotal
if (predir != MACBIDI_DIR_NT) { 76 202 278
110 76 uv = MACBIDI_UV_PDF;
111 76 (void)uvuni_to_utf8(uni, uv);
112 76 ▶ sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv));
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv)); 76 0 76
sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv)); 76 0 76
113 }
114 278 ▶ XPUSHs(dst);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dst); 0 278 278
115  
116  
117  
118 void
119 encode(...)
120 ALIAS:
121 encodeMacFarsi = 1
122 PREINIT:
123 SV *src, *dst, *ref;
124 STRLEN srclen, retlen;
125 U8 *s, *e, *p;
126 STDCHAR b, *t, **table;
127 UV uv;
128 STDCHAR dir;
129 294 bool has_cv = FALSE;
130 294 bool has_pv = FALSE;
131 PPCODE:
132 294 ref = NULL;
133 294 ▶ if (0 < items && SvROK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < items && SvROK(ST(0))) { 293 1 294
if (0 < items && SvROK(ST(0))) { 10 283 293
134 10 ref = SvRV(ST(0));
135 10 ▶ if (SvTYPE(ref) == SVt_PVCV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(ref) == SVt_PVCV) 4 6 10
136 4 has_cv = TRUE;
137 6 ▶ else if (SvPOK(ref))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvPOK(ref)) 4 2 6
138 4 has_pv = TRUE;
139 else
140 2 croak(PkgName " 1st argument is not STRING nor CODEREF");
141 }
142 292 src = ref
143 8 ▶ ? (1 < items) ? ST(1) : &PL_sv_undef
Branch: 1 branch
BranchTrueFalseTotal
? (1 < items) ? ST(1) : &PL_sv_undef 6 2 8
144 300 ▶ : (0 < items) ? ST(0) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
: (0 < items) ? ST(0) : &PL_sv_undef; 8 284 292
: (0 < items) ? ST(0) : &PL_sv_undef; 283 1 284
145  
146 292 ▶ if (!SvUTF8(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvUTF8(src)) { 18 274 292
147 18 src = sv_mortalcopy(src);
148 18 sv_utf8_upgrade(src);
149 }
150 292 s = (U8*)SvPV(src,srclen);
151 292 e = s + srclen;
152 292 dst = sv_2mortal(newSV(1));
153 292 (void)SvPOK_only(dst);
154 292 SvUTF8_off(dst);
155  
156 292 dir = MACBIDI_DIR_NT;
157  
158 797 ▶ for (p = s; p < e;) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e;) { 505 292 797
159 505 uv = utf8n_to_uvuni(p, e - p, &retlen, 0);
160 505 p += retlen;
161  
162 505 switch (uv) {
163 76 case MACBIDI_UV_PDF:
164 76 dir = MACBIDI_DIR_NT;
165 76 break;
166 34 case MACBIDI_UV_LRO:
167 34 dir = MACBIDI_DIR_LR;
168 34 break;
169 46 case MACBIDI_UV_RLO:
170 46 dir = MACBIDI_DIR_RL;
171 46 break;
172 349 default:
173 349 table = ToMacTbl[dir];
174 349 ▶ t = uv < 0x10000 ? table[uv >> 8] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
t = uv < 0x10000 ? table[uv >> 8] : NULL; 344 5 349
175 349 ▶ b = t ? t[uv & 0xff] : 0;
Branch: 1 branch
BranchTrueFalseTotal
b = t ? t[uv & 0xff] : 0; 339 10 349
176  
177 349 ▶ if (b || uv == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (b || uv == 0) 12 337 349
if (b || uv == 0) 2 10 12
178 339 sv_catpvn(dst, (char*)&b, SBCS_LEN);
179 10 ▶ else if (has_pv)
Branch: 1 branch
BranchTrueFalseTotal
else if (has_pv) 4 6 10
180 4 sv_catsv(dst, ref);
181 6 ▶ else if (has_cv)
Branch: 1 branch
BranchTrueFalseTotal
else if (has_cv) 4 2 6
182 4 sv_cat_cvref(dst, ref, newSVuv(uv));
183 }
184 }
185 292 ▶ XPUSHs(dst);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dst); 0 292 292
186  
↑ Summary blib/lib/Lingua/FA/MacFarsi.pm »