MacHebrew.xs

filter: Statementstmt 97.1% 101 / 104 Branchbran 85.2% 75 / 88 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.6% 176 / 192 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Lingua/HE/MacHebrew.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "fmmache.h"
6 #include "tomache.h"
7  
8 #define PkgName "Lingua::HE::MacHebrew"
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_mache_tbl
23 #define FromMacDir fm_mache_dir
24 #define ToMacTbl to_mache_table
25 #define ToMacTblN to_mache_N
26 #define ToMacTblL to_mache_L
27 #define ToMacTblR to_mache_R
28 #define ToMacTblC to_mache_C
29  
30 static STDCHAR ** ToMacTbl [] = {
31 ToMacTblN,
32 ToMacTblL,
33 ToMacTblR
34 };
35  
36 static void
37 4 sv_cat_cvref (SV *dst, SV *cv, SV *sv)
38 {
39 4 dSP;
40 int count;
41 4 ENTER;
42 4 SAVETMPS;
43 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
44 4 ▶ XPUSHs(sv_2mortal(sv));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(sv)); 0 4 4
45 4 PUTBACK;
46 4 count = call_sv(cv, (G_EVAL|G_SCALAR));
47 4 SPAGAIN;
48 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
49 0 croak("died in XS, " PkgName "\n");
50 }
51 4 sv_catsv(dst,POPs);
52 4 PUTBACK;
53 4 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 4 0 4
54 4 LEAVE;
55 4 }
56  
57 MODULE = Lingua::HE::MacHebrew PACKAGE = Lingua::HE::MacHebrew
58 PROTOTYPES: DISABLE
59  
60 void
61 decode(...)
62 ALIAS:
63 decodeMacHebrew = 1
64 PREINIT:
65 SV *src, *dst;
66 STRLEN srclen;
67 U8 *s, *e, *p;
68 STDCHAR *str, *utf_string;
69 STDCHAR curdir, predir;
70 PPCODE:
71 288 ▶ if (0 < items && SvROK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < items && SvROK(ST(0))) { 287 1 288
if (0 < items && SvROK(ST(0))) { 6 281 287
72 6 croak(PkgName " 1st argument is REF, but handler for decode is NG.");
73 }
74 282 ▶ src = (0 < items) ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
src = (0 < items) ? ST(0) : &PL_sv_undef; 281 1 282
75  
76 282 ▶ if (SvUTF8(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(src)) { 1 281 282
77 1 src = sv_mortalcopy(src);
78 1 sv_utf8_downgrade(src, 0);
79 }
80 282 s = (U8*)SvPV(src,srclen);
81 282 e = s + srclen;
82 282 dst = sv_2mortal(newSV(1));
83 282 (void)SvPOK_only(dst);
84 282 SvUTF8_on(dst);
85  
86 282 predir = MACBIDI_DIR_NT;
87 593 ▶ for (p = s; p < e; p++, predir = curdir) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e; p++, predir = curdir) { 311 282 593
88 311 curdir = FromMacDir[*p];
89  
90 311 ▶ if (predir != curdir) {
Branch: 1 branch
BranchTrueFalseTotal
if (predir != curdir) { 75 236 311
91 75 ▶ if (predir != MACBIDI_DIR_NT) {
Branch: 1 branch
BranchTrueFalseTotal
if (predir != MACBIDI_DIR_NT) { 0 75 75
92 0 sv_catpv(dst, (char*)MACBIDI_STR_PDF);
93 }
94 75 ▶ if (curdir != MACBIDI_DIR_NT) {
Branch: 1 branch
BranchTrueFalseTotal
if (curdir != MACBIDI_DIR_NT) { 75 0 75
95 122 ▶ str = (curdir == MACBIDI_DIR_LR) ? MACBIDI_STR_LRO :
Branch: 1 branch
BranchTrueFalseTotal
str = (curdir == MACBIDI_DIR_LR) ? MACBIDI_STR_LRO : 47 28 75
96 47 ▶ (curdir == MACBIDI_DIR_RL) ? MACBIDI_STR_RLO :
Branch: 1 branch
BranchTrueFalseTotal
(curdir == MACBIDI_DIR_RL) ? MACBIDI_STR_RLO : 47 0 47
97 NULL; /* Panic */;
98 75 ▶ if (!str) {
Branch: 1 branch
BranchTrueFalseTotal
if (!str) { 0 75 75
99 0 croak(PkgName "Panic: undefined direction in decode");
100 }
101 75 sv_catpv(dst, (char*)str);
102 }
103 }
104  
105 311 utf_string = FromMacTbl[*p];
106 311 ▶ if (utf_string) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf_string) { 311 0 311
107 311 ▶ if (*utf_string)
Branch: 1 branch
BranchTrueFalseTotal
if (*utf_string) 309 2 311
108 309 sv_catpv(dst, (char*)utf_string);
109 else /* \0 to \0 */
110 2 sv_catpvn(dst, (char*)utf_string, 1);
111 }
112 }
113  
114 282 ▶ if (predir != MACBIDI_DIR_NT) {
Branch: 1 branch
BranchTrueFalseTotal
if (predir != MACBIDI_DIR_NT) { 75 207 282
115 75 sv_catpv(dst, (char*)MACBIDI_STR_PDF);
116 }
117 282 ▶ XPUSHs(dst);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dst); 0 282 282
118  
119  
120  
121 void
122 encode(...)
123 ALIAS:
124 encodeMacHebrew = 1
125 PREINIT:
126 SV *src, *dst, *ref;
127 STRLEN srclen, retlen;
128 U8 *s, *e, *p;
129 STDCHAR b, *t, **table;
130 struct macbidi_contra *p_contra, *cel_contra, **row_contra;
131 UV uv;
132 STDCHAR dir;
133 304 bool has_cv = FALSE;
134 304 bool has_pv = FALSE;
135 PPCODE:
136 304 ref = NULL;
137 304 ▶ if (0 < items && SvROK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < items && SvROK(ST(0))) { 303 1 304
if (0 < items && SvROK(ST(0))) { 10 293 303
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 302 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 310 ▶ : (0 < items) ? ST(0) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
: (0 < items) ? ST(0) : &PL_sv_undef; 8 294 302
: (0 < items) ? ST(0) : &PL_sv_undef; 293 1 294
149  
150 302 ▶ if (!SvUTF8(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvUTF8(src)) { 21 281 302
151 21 src = sv_mortalcopy(src);
152 21 sv_utf8_upgrade(src);
153 }
154 302 s = (U8*)SvPV(src,srclen);
155 302 e = s + srclen;
156 302 dst = sv_2mortal(newSV(1));
157 302 (void)SvPOK_only(dst);
158 302 SvUTF8_off(dst);
159  
160 302 dir = MACBIDI_DIR_NT;
161  
162 822 ▶ for (p = s; p < e;) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e;) { 520 302 822
163 520 uv = utf8n_to_uvuni(p, e - p, &retlen, 0);
164 520 p += retlen;
165  
166 520 switch (uv) {
167 75 case MACBIDI_UV_PDF:
168 75 dir = MACBIDI_DIR_NT;
169 75 break;
170 30 case MACBIDI_UV_LRO:
171 30 dir = MACBIDI_DIR_LR;
172 30 break;
173 49 case MACBIDI_UV_RLO:
174 49 dir = MACBIDI_DIR_RL;
175 49 break;
176 366 default:
177 366 b = 0;
178 366 ▶ row_contra = uv < 0x10000 ? ToMacTblC[uv >> 8] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
row_contra = uv < 0x10000 ? ToMacTblC[uv >> 8] : NULL; 361 5 366
179 366 ▶ cel_contra = row_contra ? row_contra[uv & 0xff] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
cel_contra = row_contra ? row_contra[uv & 0xff] : NULL; 63 303 366
180  
181 366 ▶ if (cel_contra) {
Branch: 1 branch
BranchTrueFalseTotal
if (cel_contra) { 17 349 366
182 21 ▶ for (p_contra = cel_contra; cel_contra->len; cel_contra++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p_contra = cel_contra; cel_contra->len; cel_contra++) { 17 4 21
183 17 ▶ if (cel_contra->len <= (e - p) &&
Branch: 1 branch
BranchTrueFalseTotal
if (cel_contra->len <= (e - p) && 15 2 17
184 15 ▶ memEQ(p, cel_contra->string, cel_contra->len)) {
Branch: 1 branch
BranchTrueFalseTotal
memEQ(p, cel_contra->string, cel_contra->len)) { 13 2 15
185 13 p += cel_contra->len;
186 13 b = cel_contra->byte;
187 13 break;
188 }
189 }
190 }
191  
192 366 ▶ if (!b) {
Branch: 1 branch
BranchTrueFalseTotal
if (!b) { 353 13 366
193 353 table = ToMacTbl[dir];
194 353 ▶ t = uv < 0x10000 ? table[uv >> 8] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
t = uv < 0x10000 ? table[uv >> 8] : NULL; 348 5 353
195 353 ▶ b = t ? t[uv & 0xff] : 0;
Branch: 1 branch
BranchTrueFalseTotal
b = t ? t[uv & 0xff] : 0; 343 10 353
196 }
197  
198 366 ▶ if (b || uv == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (b || uv == 0) 12 354 366
if (b || uv == 0) 2 10 12
199 356 sv_catpvn(dst, (char*)&b, SBCS_LEN);
200 10 ▶ else if (has_pv)
Branch: 1 branch
BranchTrueFalseTotal
else if (has_pv) 4 6 10
201 4 sv_catsv(dst, ref);
202 6 ▶ else if (has_cv)
Branch: 1 branch
BranchTrueFalseTotal
else if (has_cv) 4 2 6
203 4 sv_cat_cvref(dst, ref, newSVuv(uv));
204 }
205 }
206 302 ▶ XPUSHs(dst);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(dst); 0 302 302
207  
↑ Summary blib/lib/Lingua/HE/MacHebrew.pm »