MacArabic.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/AR/MacArabic.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "fmmacar.h"
6 #include "tomacar.h"
7  
8 #define PkgName "Lingua::AR::MacArabic"
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_macar_tbl
23 #define FromMacDir fm_macar_dir
24 #define ToMacTbl to_macar_table
25 #define ToMacTblN to_macar_N
26 #define ToMacTblL to_macar_L
27 #define ToMacTblR to_macar_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::AR::MacArabic PACKAGE = Lingua::AR::MacArabic
57 PROTOTYPES: DISABLE
58  
59 void
60 decode(...)
61 ALIAS:
62 decodeMacArabic = 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 286 ▶ if (0 < items && SvROK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < items && SvROK(ST(0))) { 285 1 286
if (0 < items && SvROK(ST(0))) { 6 279 285
71 6 croak(PkgName " 1st argument is REF, but handler for decode is NG.");
72 }
73 280 ▶ src = (0 < items) ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
src = (0 < items) ? ST(0) : &PL_sv_undef; 279 1 280
74  
75 280 ▶ if (SvUTF8(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(src)) { 1 279 280
76 1 src = sv_mortalcopy(src);
77 1 sv_utf8_downgrade(src, 0);
78 }
79 280 s = (U8*)SvPV(src,srclen);
80 280 e = s + srclen;
81 280 dst = sv_2mortal(newSV(1));
82 280 (void)SvPOK_only(dst);
83 280 SvUTF8_on(dst);
84  
85 280 predir = MACBIDI_DIR_NT;
86 589 ▶ for (p = s; p < e; p++, predir = curdir) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e; p++, predir = curdir) { 309 280 589
87 309 curdir = FromMacDir[*p];
88  
89 309 ▶ if (predir != curdir) {
Branch: 1 branch
BranchTrueFalseTotal
if (predir != curdir) { 78 231 309
90 78 ▶ if (predir != MACBIDI_DIR_NT) {
Branch: 1 branch
BranchTrueFalseTotal
if (predir != MACBIDI_DIR_NT) { 0 78 78
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 78 ▶ if (curdir != MACBIDI_DIR_NT) {
Branch: 1 branch
BranchTrueFalseTotal
if (curdir != MACBIDI_DIR_NT) { 78 0 78
96 78 ▶ uv = (curdir == MACBIDI_DIR_LR) ? MACBIDI_UV_LRO :
Branch: 2 branches
BranchTrueFalseTotal
uv = (curdir == MACBIDI_DIR_LR) ? MACBIDI_UV_LRO : 46 32 78
uv = (curdir == MACBIDI_DIR_LR) ? MACBIDI_UV_LRO : 46 0 46
97 (curdir == MACBIDI_DIR_RL) ? MACBIDI_UV_RLO :
98 0; /* Panic: undefined direction in decode" */;
99 78 (void)uvuni_to_utf8(uni, uv);
100 78 ▶ sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv));
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv)); 78 0 78
sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv)); 78 0 78
101 }
102 }
103  
104 309 uv = FromMacTbl[*p];
105 309 (void)uvuni_to_utf8(uni, uv);
106 309 ▶ sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv));
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv)); 118 191 309
sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv)); 118 0 118
107 }
108  
109 280 ▶ if (predir != MACBIDI_DIR_NT) {
Branch: 1 branch
BranchTrueFalseTotal
if (predir != MACBIDI_DIR_NT) { 78 202 280
110 78 uv = MACBIDI_UV_PDF;
111 78 (void)uvuni_to_utf8(uni, uv);
112 78 ▶ sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv));
Branch: 2 branches
BranchTrueFalseTotal
sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv)); 78 0 78
sv_catpvn(dst, (char*)uni, (STRLEN)UNISKIP(uv)); 78 0 78
113 }
114 280 ▶ XPUSHs(dst);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dst); 0 280 280
115  
116  
117  
118 void
119 encode(...)
120 ALIAS:
121 encodeMacArabic = 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 297 bool has_cv = FALSE;
130 297 bool has_pv = FALSE;
131 PPCODE:
132 297 ref = NULL;
133 297 ▶ if (0 < items && SvROK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < items && SvROK(ST(0))) { 296 1 297
if (0 < items && SvROK(ST(0))) { 10 286 296
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 295 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 303 ▶ : (0 < items) ? ST(0) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
: (0 < items) ? ST(0) : &PL_sv_undef; 8 287 295
: (0 < items) ? ST(0) : &PL_sv_undef; 286 1 287
145  
146 295 ▶ if (!SvUTF8(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvUTF8(src)) { 18 277 295
147 18 src = sv_mortalcopy(src);
148 18 sv_utf8_upgrade(src);
149 }
150 295 s = (U8*)SvPV(src,srclen);
151 295 e = s + srclen;
152 295 dst = sv_2mortal(newSV(1));
153 295 (void)SvPOK_only(dst);
154 295 SvUTF8_off(dst);
155  
156 295 dir = MACBIDI_DIR_NT;
157  
158 805 ▶ for (p = s; p < e;) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e;) { 510 295 805
159 510 uv = utf8n_to_uvuni(p, e - p, &retlen, 0);
160 510 p += retlen;
161  
162 510 switch (uv) {
163 76 case MACBIDI_UV_PDF:
164 76 dir = MACBIDI_DIR_NT;
165 76 break;
166 35 case MACBIDI_UV_LRO:
167 35 dir = MACBIDI_DIR_LR;
168 35 break;
169 47 case MACBIDI_UV_RLO:
170 47 dir = MACBIDI_DIR_RL;
171 47 break;
172 352 default:
173 352 table = ToMacTbl[dir];
174 352 ▶ t = uv < 0x10000 ? table[uv >> 8] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
t = uv < 0x10000 ? table[uv >> 8] : NULL; 347 5 352
175 352 ▶ b = t ? t[uv & 0xff] : 0;
Branch: 1 branch
BranchTrueFalseTotal
b = t ? t[uv & 0xff] : 0; 340 12 352
176  
177 352 ▶ if (b || uv == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (b || uv == 0) 14 338 352
if (b || uv == 0) 2 12 14
178 340 sv_catpvn(dst, (char*)&b, SBCS_LEN);
179 12 ▶ else if (has_pv)
Branch: 1 branch
BranchTrueFalseTotal
else if (has_pv) 4 8 12
180 4 sv_catsv(dst, ref);
181 8 ▶ else if (has_cv)
Branch: 1 branch
BranchTrueFalseTotal
else if (has_cv) 4 4 8
182 4 sv_cat_cvref(dst, ref, newSVuv(uv));
183 }
184 }
185 295 ▶ XPUSHs(dst);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dst); 0 295 295
186  
↑ Summary blib/lib/Lingua/AR/MacArabic.pm »