Transform.xs

filter: Statementstmt 96.5% 84 / 87 Branchbran 88.5% 62 / 70 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.9% 146 / 157 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« unitrans.h ↑ Summary blib/lib/Unicode/Transform.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #define PkgName "Unicode::Transform"
6  
7 /* Some functions are defined in this. */
8 #include "unitrans.h"
9  
10 #define Num_UTFs_here (8)
11  
12 /* in the range of valid Unicode (0..10FFFF) */
13 static STDCHAR
14 MaxLenAmplUni[Num_UTFs_here * Num_UTFs_here] = {
15 1, 2, 2, 4, 4, 2, 2, 2,
16 2, 1, 1, 2, 2, 2, 2, 2,
17 2, 1, 1, 2, 2, 2, 2, 2,
18 2, 1, 1, 1, 1, 1, 2, 2,
19 2, 1, 1, 1, 1, 1, 2, 2,
20 2, 2, 2, 4, 4, 1, 2, 2,
21 2, 2, 2, 4, 4, 2, 1, 1,
22 2, 2, 2, 4, 4, 2, 1, 1,
23 };
24  
25  
26 static UV (*ord_uv_in[Num_UTFs_here])(U8 *, STRLEN, STRLEN *) = {
27 ord_in_unicode,
28 ord_in_utf16le,
29 ord_in_utf16be,
30 ord_in_utf32le,
31 ord_in_utf32be,
32 ord_in_utf8,
33 ord_in_utf8mod,
34 ord_in_utfcp1047,
35 };
36  
37 static U8* (*app_uv_in[Num_UTFs_here])(U8 *, UV) = {
38 app_in_unicode,
39 app_in_utf16le,
40 app_in_utf16be,
41 app_in_utf32le,
42 app_in_utf32be,
43 app_in_utf8,
44 app_in_utf8mod,
45 app_in_utfcp1047,
46 };
47  
48 static void
49 269 sv_cat_retcvref (SV *dst, SV *cv, SV *sv)
50 {
51 269 dSP;
52 int count;
53 269 ENTER;
54 269 SAVETMPS;
55 269 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 269 269
56 269 ▶ XPUSHs(sv_2mortal(sv));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(sv)); 0 269 269
57 269 PUTBACK;
58 269 count = call_sv(cv, G_EVAL|G_SCALAR);
59 269 SPAGAIN;
60 269 ▶ if (SvTRUE(ERRSV) || count != 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || count != 1) { 269 0 269
if (SvTRUE(ERRSV) || count != 1) { 267 2 269
if (SvTRUE(ERRSV) || count != 1) { 0 267 267
61 2 croak("died in subroutine call from XS, " PkgName "\n");
62 }
63 267 sv_catsv(dst,POPs);
64 267 PUTBACK;
65 267 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 267 0 267
66 267 LEAVE;
67 267 }
68  
69 MODULE = Unicode::Transform PACKAGE = Unicode::Transform
70  
71 SV*
72 unicode_to_unicode (arg1, arg2=0)
73 SV* arg1
74 SV* arg2
75 PROTOTYPE: $;$
76 ALIAS:
77 unicode_to_utf16le = 1
78 unicode_to_utf16be = 2
79 unicode_to_utf32le = 3
80 unicode_to_utf32be = 4
81 unicode_to_utf8 = 5
82 unicode_to_utf8mod = 6
83 unicode_to_utfcp1047 = 7
84 utf16le_to_unicode = 8
85 utf16le_to_utf16le = 9
86 utf16le_to_utf16be = 10
87 utf16le_to_utf32le = 11
88 utf16le_to_utf32be = 12
89 utf16le_to_utf8 = 13
90 utf16le_to_utf8mod = 14
91 utf16le_to_utfcp1047 = 15
92 utf16be_to_unicode = 16
93 utf16be_to_utf16le = 17
94 utf16be_to_utf16be = 18
95 utf16be_to_utf32le = 19
96 utf16be_to_utf32be = 20
97 utf16be_to_utf8 = 21
98 utf16be_to_utf8mod = 22
99 utf16be_to_utfcp1047 = 23
100 utf32le_to_unicode = 24
101 utf32le_to_utf16le = 25
102 utf32le_to_utf16be = 26
103 utf32le_to_utf32le = 27
104 utf32le_to_utf32be = 28
105 utf32le_to_utf8 = 29
106 utf32le_to_utf8mod = 30
107 utf32le_to_utfcp1047 = 31
108 utf32be_to_unicode = 32
109 utf32be_to_utf16le = 33
110 utf32be_to_utf16be = 34
111 utf32be_to_utf32le = 35
112 utf32be_to_utf32be = 36
113 utf32be_to_utf8 = 37
114 utf32be_to_utf8mod = 38
115 utf32be_to_utfcp1047 = 39
116 utf8_to_unicode = 40
117 utf8_to_utf16le = 41
118 utf8_to_utf16be = 42
119 utf8_to_utf32le = 43
120 utf8_to_utf32be = 44
121 utf8_to_utf8 = 45
122 utf8_to_utf8mod = 46
123 utf8_to_utfcp1047 = 47
124 utf8mod_to_unicode = 48
125 utf8mod_to_utf16le = 49
126 utf8mod_to_utf16be = 50
127 utf8mod_to_utf32le = 51
128 utf8mod_to_utf32be = 52
129 utf8mod_to_utf8 = 53
130 utf8mod_to_utf8mod = 54
131 utf8mod_to_utfcp1047 = 55
132 utfcp1047_to_unicode = 56
133 utfcp1047_to_utf16le = 57
134 utfcp1047_to_utf16be = 58
135 utfcp1047_to_utf32le = 59
136 utfcp1047_to_utf32be = 60
137 utfcp1047_to_utf8 = 61
138 utfcp1047_to_utf8mod = 62
139 utfcp1047_to_utfcp1047 = 63
140 PREINIT:
141 SV *src, *dst, *cvref;
142 STRLEN srclen, dstlen, retlen, ulen;
143 U8 *s, *e, *p, *d, ubuf[UTF8_MAXLEN + 1];
144 UV uv;
145 UV (*ord_uv)(U8 *, STRLEN, STRLEN *);
146 U8* (*app_uv)(U8*, UV);
147 int from_utf_num, to_utf_num;
148 bool from_unicode, to_unicode;
149 CODE:
150 878 cvref = NULL;
151 878 ▶ if (SvROK(arg1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(arg1)) { 469 409 878
152 469 ▶ if (SvTYPE(SvRV(arg1)) == SVt_PVCV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(arg1)) == SVt_PVCV) 469 0 469
153 469 cvref = SvRV(arg1);
154 else
155 0 croak(PkgName " CALLBACK is not a CODEREF");
156 }
157 878 src = cvref
158 469 ▶ ? items == 1 ? &PL_sv_undef : arg2
Branch: 1 branch
BranchTrueFalseTotal
? items == 1 ? &PL_sv_undef : arg2 469 0 469
159 1347 ▶ : arg1;
Branch: 1 branch
BranchTrueFalseTotal
: arg1; 469 409 878
160  
161 878 from_utf_num = ix / Num_UTFs_here;
162 878 to_utf_num = ix % Num_UTFs_here;
163  
164 878 from_unicode = from_utf_num == 0;
165 878 to_unicode = to_utf_num == 0;
166  
167 878 ▶ if (!from_unicode && SvUTF8(src)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!from_unicode && SvUTF8(src)) { 733 145 878
if (!from_unicode && SvUTF8(src)) { 1 732 733
168 1 src = sv_mortalcopy(src);
169 1 sv_utf8_downgrade(src, 0);
170 }
171 877 ▶ else if (from_unicode && !SvUTF8(src)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (from_unicode && !SvUTF8(src)) { 145 732 877
else if (from_unicode && !SvUTF8(src)) { 61 84 145
172 61 src = sv_mortalcopy(src);
173 61 sv_utf8_upgrade(src);
174 }
175  
176 878 s = (U8*)SvPV(src,srclen);
177 878 e = s + srclen;
178  
179 878 dstlen = srclen * MaxLenAmplUni[ix] + 1;
180  
181 878 dst = newSV(dstlen);
182 878 (void)SvPOK_only(dst);
183 878 ▶ if (to_unicode) {
Branch: 1 branch
BranchTrueFalseTotal
if (to_unicode) { 149 729 878
184 149 SvUTF8_on(dst);
185 }
186  
187 878 ord_uv = ord_uv_in[from_utf_num];
188 878 app_uv = app_uv_in[to_utf_num];
189  
190 878 ▶ if (cvref) {
Branch: 1 branch
BranchTrueFalseTotal
if (cvref) { 469 409 878
191 1988 ▶ for (p = s; p < e;) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e;) { 1521 467 1988
192 1521 uv = ord_uv(p, e - p, &retlen);
193  
194 1521 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 1496 25 1521
195 1496 p += retlen;
196 else
197 25 uv = (UV)*p++;
198  
199 1521 ▶ if (retlen && Is_VALID_UTF(uv)) {
Branch: 4 branches
BranchTrueFalseTotal
if (retlen && Is_VALID_UTF(uv)) { 1496 25 1521
if (retlen && Is_VALID_UTF(uv)) { 1360 136 1496
if (retlen && Is_VALID_UTF(uv)) { 246 1114 1360
if (retlen && Is_VALID_UTF(uv)) { 138 108 246
200 1252 ulen = app_uv(ubuf, uv) - ubuf;
201 1252 sv_catpvn(dst, (char*)ubuf, ulen);
202 }
203 else
204 269 sv_cat_retcvref(dst, cvref, newSVuv(uv));
205 }
206 }
207 else {
208 409 d = (U8*)SvPVX(dst);
209  
210 11510 ▶ for (p = s; p < e;) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = s; p < e;) { 11101 409 11510
211 11101 uv = ord_uv(p, e - p, &retlen);
212  
213 11101 ▶ if (retlen)
Branch: 1 branch
BranchTrueFalseTotal
if (retlen) 11097 4 11101
214 11097 p += retlen;
215 else {
216 4 p++;
217 4 continue;
218 }
219  
220 11097 ▶ if (Is_VALID_UTF(uv))
Branch: 3 branches
BranchTrueFalseTotal
if (Is_VALID_UTF(uv)) 11053 44 11097
if (Is_VALID_UTF(uv)) 202 10851 11053
if (Is_VALID_UTF(uv)) 166 36 202
221 11017 d = app_uv(d, uv);
222 }
223 409 *d = '\0';
224 409 SvCUR_set(dst, d - (U8*)SvPVX(dst));
225 }
226 876 RETVAL = dst;
227 OUTPUT:
228 RETVAL
229  
230  
231 SV*
232 chr_unicode (uv)
233 UV uv
234 PROTOTYPE: $
235 ALIAS:
236 chr_utf16le = 1
237 chr_utf16be = 2
238 chr_utf32le = 3
239 chr_utf32be = 4
240 chr_utf8 = 5
241 chr_utf8mod = 6
242 chr_utfcp1047 = 7
243 PREINIT:
244 SV *dst;
245 U8 *u, ubuf[UTF8_MAXLEN + 1];
246 U8* (*app_uv)(U8*, UV);
247 CODE:
248 199 dst = newSVpvn("", 0);
249 199 (void)SvPOK_only(dst);
250 199 ▶ if (ix == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 0) { 34 165 199
251 34 SvUTF8_on(dst);
252 }
253  
254 199 app_uv = app_uv_in[ix];
255 199 u = app_uv(ubuf, uv);
256 199 ▶ if (u == ubuf)
Branch: 1 branch
BranchTrueFalseTotal
if (u == ubuf) 2 197 199
257 2 XSRETURN_UNDEF;
258  
259 197 sv_catpvn(dst, (char*)ubuf, u - ubuf);
260 197 RETVAL = dst;
261 OUTPUT:
262 RETVAL
263  
264  
265 SV*
266 ord_unicode (src)
267 SV* src
268 PROTOTYPE: $
269 ALIAS:
270 ord_utf16le = 1
271 ord_utf16be = 2
272 ord_utf32le = 3
273 ord_utf32be = 4
274 ord_utf8 = 5
275 ord_utf8mod = 6
276 ord_utfcp1047 = 7
277 PREINIT:
278 STRLEN srclen, retlen;
279 U8 *s;
280 UV uv;
281 UV (*ord_uv)(U8 *, STRLEN, STRLEN *);
282 CODE:
283 115 ▶ if (ix != 0 && SvUTF8(src)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ix != 0 && SvUTF8(src)) { 98 17 115
if (ix != 0 && SvUTF8(src)) { 0 98 98
284 0 src = sv_mortalcopy(src);
285 0 sv_utf8_downgrade(src, 0);
286 }
287 115 ▶ else if (ix == 0 && !SvUTF8(src)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (ix == 0 && !SvUTF8(src)) { 17 98 115
else if (ix == 0 && !SvUTF8(src)) { 3 14 17
288 3 src = sv_mortalcopy(src);
289 3 sv_utf8_upgrade(src);
290 }
291  
292 115 s = (U8*)SvPV(src,srclen);
293 115 ▶ if (!srclen)
Branch: 1 branch
BranchTrueFalseTotal
if (!srclen) 8 107 115
294 8 XSRETURN_UNDEF;
295  
296 107 ord_uv = ord_uv_in[ix];
297 107 uv = ord_uv(s, srclen, &retlen);
298 107 ▶ RETVAL = retlen ? newSVuv(uv) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = retlen ? newSVuv(uv) : &PL_sv_undef; 103 4 107
299 OUTPUT:
300 RETVAL
301  
« unitrans.h ↑ Summary blib/lib/Unicode/Transform.pm »