Commit | Line | Data |
a0d0e21e |
1 | /* doop.c |
79072805 |
2 | * |
4bb101f2 |
3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
af3babe4 |
4 | * 2000, 2001, 2002, 2004, 2005, by Larry Wall and others |
79072805 |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public |
7 | * License or the Artistic License, as specified in the README file. |
8 | * |
a0d0e21e |
9 | */ |
10 | |
11 | /* |
12 | * "'So that was the job I felt I had to do when I started,' thought Sam." |
79072805 |
13 | */ |
14 | |
166f8a29 |
15 | /* This file contains some common functions needed to carry out certain |
16 | * ops. For example both pp_schomp() and pp_chomp() - scalar and array |
17 | * chomp operations - call the function do_chomp() found in this file. |
18 | */ |
19 | |
79072805 |
20 | #include "EXTERN.h" |
864dbfa3 |
21 | #define PERL_IN_DOOP_C |
79072805 |
22 | #include "perl.h" |
23 | |
64ca3a65 |
24 | #ifndef PERL_MICRO |
79072805 |
25 | #include <signal.h> |
26 | #endif |
27 | |
942e002e |
28 | STATIC I32 |
b250498f |
29 | S_do_trans_simple(pTHX_ SV *sv) |
79072805 |
30 | { |
4757a243 |
31 | U8 *s; |
b250498f |
32 | U8 *d; |
f54cb97a |
33 | const U8 *send; |
b250498f |
34 | U8 *dstart; |
4757a243 |
35 | I32 matches = 0; |
f54cb97a |
36 | const I32 grows = PL_op->op_private & OPpTRANS_GROWS; |
463ee0b2 |
37 | STRLEN len; |
79072805 |
38 | |
f54cb97a |
39 | const short *tbl = (short*)cPVOP->op_pv; |
4757a243 |
40 | if (!tbl) |
7d85a32c |
41 | Perl_croak(aTHX_ "panic: do_trans_simple line %d",__LINE__); |
a0ed51b3 |
42 | |
4757a243 |
43 | s = (U8*)SvPV(sv, len); |
44 | send = s + len; |
45 | |
1e54db1a |
46 | /* First, take care of non-UTF-8 input strings, because they're easy */ |
1aa99e6b |
47 | if (!SvUTF8(sv)) { |
01ec43d0 |
48 | while (s < send) { |
f54cb97a |
49 | const I32 ch = tbl[*s]; |
50 | if (ch >= 0) { |
01ec43d0 |
51 | matches++; |
eb160463 |
52 | *s++ = (U8)ch; |
01ec43d0 |
53 | } |
54 | else |
55 | s++; |
56 | } |
57 | SvSETMAGIC(sv); |
b250498f |
58 | return matches; |
59 | } |
4757a243 |
60 | |
b250498f |
61 | /* Allow for expansion: $_="a".chr(400); tr/a/\xFE/, FE needs encoding */ |
9b877dbb |
62 | if (grows) |
a02a5408 |
63 | Newx(d, len*2+1, U8); |
9b877dbb |
64 | else |
65 | d = s; |
b250498f |
66 | dstart = d; |
67 | while (s < send) { |
ba210ebe |
68 | STRLEN ulen; |
f54cb97a |
69 | I32 ch; |
b250498f |
70 | |
b250498f |
71 | /* Need to check this, otherwise 128..255 won't match */ |
f54cb97a |
72 | const UV c = utf8n_to_uvchr(s, send - s, &ulen, 0); |
9b877dbb |
73 | if (c < 0x100 && (ch = tbl[c]) >= 0) { |
b250498f |
74 | matches++; |
9041c2e3 |
75 | d = uvchr_to_utf8(d, ch); |
b250498f |
76 | s += ulen; |
01ec43d0 |
77 | } |
78 | else { /* No match -> copy */ |
0376ff32 |
79 | Move(s, d, ulen, U8); |
9b877dbb |
80 | d += ulen; |
81 | s += ulen; |
b250498f |
82 | } |
83 | } |
9b877dbb |
84 | if (grows) { |
85 | sv_setpvn(sv, (char*)dstart, d - dstart); |
86 | Safefree(dstart); |
87 | } |
88 | else { |
89 | *d = '\0'; |
90 | SvCUR_set(sv, d - dstart); |
91 | } |
b250498f |
92 | SvUTF8_on(sv); |
b250498f |
93 | SvSETMAGIC(sv); |
4757a243 |
94 | return matches; |
95 | } |
96 | |
942e002e |
97 | STATIC I32 |
21304a3b |
98 | S_do_trans_count(pTHX_ SV *sv) |
4757a243 |
99 | { |
83003860 |
100 | const U8 *s; |
101 | const U8 *send; |
4757a243 |
102 | I32 matches = 0; |
103 | STRLEN len; |
f54cb97a |
104 | const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT; |
4757a243 |
105 | |
f54cb97a |
106 | const short * const tbl = (short*)cPVOP->op_pv; |
4757a243 |
107 | if (!tbl) |
7d85a32c |
108 | Perl_croak(aTHX_ "panic: do_trans_count line %d",__LINE__); |
4757a243 |
109 | |
83003860 |
110 | s = (const U8*)SvPV_const(sv, len); |
4757a243 |
111 | send = s + len; |
112 | |
1aa99e6b |
113 | if (!SvUTF8(sv)) |
114 | while (s < send) { |
115 | if (tbl[*s++] >= 0) |
036b4402 |
116 | matches++; |
1aa99e6b |
117 | } |
118 | else |
119 | while (s < send) { |
1aa99e6b |
120 | STRLEN ulen; |
f54cb97a |
121 | const UV c = utf8n_to_uvchr(s, send - s, &ulen, 0); |
8973db79 |
122 | if (c < 0x100) { |
123 | if (tbl[c] >= 0) |
124 | matches++; |
125 | } else if (complement) |
1aa99e6b |
126 | matches++; |
127 | s += ulen; |
128 | } |
4757a243 |
129 | |
130 | return matches; |
131 | } |
132 | |
942e002e |
133 | STATIC I32 |
21304a3b |
134 | S_do_trans_complex(pTHX_ SV *sv) |
4757a243 |
135 | { |
4757a243 |
136 | U8 *s; |
137 | U8 *send; |
138 | U8 *d; |
5e44153e |
139 | U8 *dstart; |
1aa99e6b |
140 | I32 isutf8; |
4757a243 |
141 | I32 matches = 0; |
f54cb97a |
142 | const I32 grows = PL_op->op_private & OPpTRANS_GROWS; |
143 | const I32 complement = PL_op->op_private & OPpTRANS_COMPLEMENT; |
144 | const I32 del = PL_op->op_private & OPpTRANS_DELETE; |
b7953727 |
145 | STRLEN len, rlen = 0; |
4757a243 |
146 | |
f54cb97a |
147 | const short * const tbl = (short*)cPVOP->op_pv; |
4757a243 |
148 | if (!tbl) |
7d85a32c |
149 | Perl_croak(aTHX_ "panic: do_trans_complex line %d",__LINE__); |
4757a243 |
150 | |
151 | s = (U8*)SvPV(sv, len); |
1aa99e6b |
152 | isutf8 = SvUTF8(sv); |
4757a243 |
153 | send = s + len; |
154 | |
1aa99e6b |
155 | if (!isutf8) { |
156 | dstart = d = s; |
157 | if (PL_op->op_private & OPpTRANS_SQUASH) { |
f54cb97a |
158 | const U8* p = send; |
1aa99e6b |
159 | while (s < send) { |
f54cb97a |
160 | const I32 ch = tbl[*s]; |
161 | if (ch >= 0) { |
eb160463 |
162 | *d = (U8)ch; |
036b4402 |
163 | matches++; |
1aa99e6b |
164 | if (p != d - 1 || *p != *d) |
165 | p = d++; |
166 | } |
167 | else if (ch == -1) /* -1 is unmapped character */ |
168 | *d++ = *s; |
169 | else if (ch == -2) /* -2 is delete character */ |
170 | matches++; |
171 | s++; |
172 | } |
a0ed51b3 |
173 | } |
1aa99e6b |
174 | else { |
175 | while (s < send) { |
f54cb97a |
176 | const I32 ch = tbl[*s]; |
177 | if (ch >= 0) { |
1aa99e6b |
178 | matches++; |
eb160463 |
179 | *d++ = (U8)ch; |
1aa99e6b |
180 | } |
181 | else if (ch == -1) /* -1 is unmapped character */ |
182 | *d++ = *s; |
183 | else if (ch == -2) /* -2 is delete character */ |
184 | matches++; |
185 | s++; |
186 | } |
187 | } |
76ef7183 |
188 | *d = '\0'; |
1aa99e6b |
189 | SvCUR_set(sv, d - dstart); |
4757a243 |
190 | } |
1aa99e6b |
191 | else { /* isutf8 */ |
9b877dbb |
192 | if (grows) |
a02a5408 |
193 | Newx(d, len*2+1, U8); |
9b877dbb |
194 | else |
195 | d = s; |
1aa99e6b |
196 | dstart = d; |
8973db79 |
197 | if (complement && !del) |
198 | rlen = tbl[0x100]; |
1aa99e6b |
199 | |
4aecb5b5 |
200 | #ifdef MACOS_TRADITIONAL |
201 | #define comp CoMP /* "comp" is a keyword in some compilers ... */ |
202 | #endif |
203 | |
1aa99e6b |
204 | if (PL_op->op_private & OPpTRANS_SQUASH) { |
1aa99e6b |
205 | UV pch = 0xfeedface; |
206 | while (s < send) { |
207 | STRLEN len; |
f54cb97a |
208 | const UV comp = utf8_to_uvchr(s, &len); |
209 | I32 ch; |
1aa99e6b |
210 | |
8973db79 |
211 | if (comp > 0xff) { |
212 | if (!complement) { |
213 | Copy(s, d, len, U8); |
214 | d += len; |
215 | } |
216 | else { |
217 | matches++; |
218 | if (!del) { |
05d340b8 |
219 | ch = (rlen == 0) ? comp : |
220 | (comp - 0x100 < rlen) ? |
8973db79 |
221 | tbl[comp+1] : tbl[0x100+rlen]; |
eb160463 |
222 | if ((UV)ch != pch) { |
9041c2e3 |
223 | d = uvchr_to_utf8(d, ch); |
eb160463 |
224 | pch = (UV)ch; |
8973db79 |
225 | } |
226 | s += len; |
227 | continue; |
228 | } |
229 | } |
9b877dbb |
230 | } |
1aa99e6b |
231 | else if ((ch = tbl[comp]) >= 0) { |
232 | matches++; |
eb160463 |
233 | if ((UV)ch != pch) { |
9041c2e3 |
234 | d = uvchr_to_utf8(d, ch); |
eb160463 |
235 | pch = (UV)ch; |
1aa99e6b |
236 | } |
237 | s += len; |
238 | continue; |
239 | } |
9b877dbb |
240 | else if (ch == -1) { /* -1 is unmapped character */ |
241 | Copy(s, d, len, U8); |
242 | d += len; |
243 | } |
1aa99e6b |
244 | else if (ch == -2) /* -2 is delete character */ |
245 | matches++; |
246 | s += len; |
247 | pch = 0xfeedface; |
248 | } |
5d06d08e |
249 | } |
1aa99e6b |
250 | else { |
251 | while (s < send) { |
252 | STRLEN len; |
4373e329 |
253 | const UV comp = utf8_to_uvchr(s, &len); |
f54cb97a |
254 | I32 ch; |
8973db79 |
255 | if (comp > 0xff) { |
256 | if (!complement) { |
0376ff32 |
257 | Move(s, d, len, U8); |
8973db79 |
258 | d += len; |
259 | } |
260 | else { |
261 | matches++; |
262 | if (!del) { |
9041c2e3 |
263 | if (comp - 0x100 < rlen) |
264 | d = uvchr_to_utf8(d, tbl[comp+1]); |
8973db79 |
265 | else |
9041c2e3 |
266 | d = uvchr_to_utf8(d, tbl[0x100+rlen]); |
8973db79 |
267 | } |
268 | } |
9b877dbb |
269 | } |
1aa99e6b |
270 | else if ((ch = tbl[comp]) >= 0) { |
9041c2e3 |
271 | d = uvchr_to_utf8(d, ch); |
1aa99e6b |
272 | matches++; |
273 | } |
274 | else if (ch == -1) { /* -1 is unmapped character */ |
9b877dbb |
275 | Copy(s, d, len, U8); |
276 | d += len; |
1aa99e6b |
277 | } |
278 | else if (ch == -2) /* -2 is delete character */ |
279 | matches++; |
280 | s += len; |
281 | } |
282 | } |
9b877dbb |
283 | if (grows) { |
284 | sv_setpvn(sv, (char*)dstart, d - dstart); |
285 | Safefree(dstart); |
286 | } |
287 | else { |
288 | *d = '\0'; |
289 | SvCUR_set(sv, d - dstart); |
290 | } |
1aa99e6b |
291 | SvUTF8_on(sv); |
4757a243 |
292 | } |
5e44153e |
293 | SvSETMAGIC(sv); |
4757a243 |
294 | return matches; |
295 | } |
296 | |
942e002e |
297 | STATIC I32 |
21304a3b |
298 | S_do_trans_simple_utf8(pTHX_ SV *sv) |
4757a243 |
299 | { |
4757a243 |
300 | U8 *s; |
301 | U8 *send; |
302 | U8 *d; |
036b4402 |
303 | U8 *start; |
1aa99e6b |
304 | U8 *dstart, *dend; |
4757a243 |
305 | I32 matches = 0; |
f54cb97a |
306 | const I32 grows = PL_op->op_private & OPpTRANS_GROWS; |
4757a243 |
307 | STRLEN len; |
308 | |
53c1dcc0 |
309 | SV* const rv = (SV*)cSVOP->op_sv; |
310 | HV* const hv = (HV*)SvRV(rv); |
4757a243 |
311 | SV** svp = hv_fetch(hv, "NONE", 4, FALSE); |
f54cb97a |
312 | const UV none = svp ? SvUV(*svp) : 0x7fffffff; |
313 | const UV extra = none + 1; |
b7953727 |
314 | UV final = 0; |
4757a243 |
315 | UV uv; |
1aa99e6b |
316 | I32 isutf8; |
317 | U8 hibit = 0; |
4757a243 |
318 | |
319 | s = (U8*)SvPV(sv, len); |
1aa99e6b |
320 | isutf8 = SvUTF8(sv); |
321 | if (!isutf8) { |
f54cb97a |
322 | const U8 *t = s, *e = s + len; |
c4d5f83a |
323 | while (t < e) { |
f54cb97a |
324 | const U8 ch = *t++; |
c4d5f83a |
325 | if ((hibit = !NATIVE_IS_INVARIANT(ch))) |
1aa99e6b |
326 | break; |
c4d5f83a |
327 | } |
1aa99e6b |
328 | if (hibit) |
329 | s = bytes_to_utf8(s, &len); |
330 | } |
4757a243 |
331 | send = s + len; |
036b4402 |
332 | start = s; |
4757a243 |
333 | |
334 | svp = hv_fetch(hv, "FINAL", 5, FALSE); |
335 | if (svp) |
336 | final = SvUV(*svp); |
337 | |
9b877dbb |
338 | if (grows) { |
339 | /* d needs to be bigger than s, in case e.g. upgrading is required */ |
a02a5408 |
340 | Newx(d, len * 3 + UTF8_MAXBYTES, U8); |
9b877dbb |
341 | dend = d + len * 3; |
342 | dstart = d; |
343 | } |
344 | else { |
345 | dstart = d = s; |
346 | dend = d + len; |
347 | } |
1aa99e6b |
348 | |
4757a243 |
349 | while (s < send) { |
3568d838 |
350 | if ((uv = swash_fetch(rv, s, TRUE)) < none) { |
4757a243 |
351 | s += UTF8SKIP(s); |
352 | matches++; |
7d85a32c |
353 | d = uvuni_to_utf8(d, uv); |
4757a243 |
354 | } |
355 | else if (uv == none) { |
4373e329 |
356 | const int i = UTF8SKIP(s); |
0376ff32 |
357 | Move(s, d, i, U8); |
9b877dbb |
358 | d += i; |
359 | s += i; |
4757a243 |
360 | } |
361 | else if (uv == extra) { |
4373e329 |
362 | s += UTF8SKIP(s); |
4757a243 |
363 | matches++; |
7d85a32c |
364 | d = uvuni_to_utf8(d, final); |
4757a243 |
365 | } |
366 | else |
367 | s += UTF8SKIP(s); |
1aa99e6b |
368 | |
9b877dbb |
369 | if (d > dend) { |
4373e329 |
370 | const STRLEN clen = d - dstart; |
371 | const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES; |
9b877dbb |
372 | if (!grows) |
7d85a32c |
373 | Perl_croak(aTHX_ "panic: do_trans_simple_utf8 line %d",__LINE__); |
89ebb4a3 |
374 | Renew(dstart, nlen + UTF8_MAXBYTES, U8); |
1aa99e6b |
375 | d = dstart + clen; |
376 | dend = dstart + nlen; |
377 | } |
4757a243 |
378 | } |
16ec844d |
379 | if (grows || hibit) { |
9b877dbb |
380 | sv_setpvn(sv, (char*)dstart, d - dstart); |
381 | Safefree(dstart); |
16ec844d |
382 | if (grows && hibit) |
383 | Safefree(start); |
9b877dbb |
384 | } |
385 | else { |
386 | *d = '\0'; |
387 | SvCUR_set(sv, d - dstart); |
388 | } |
4757a243 |
389 | SvSETMAGIC(sv); |
1aa99e6b |
390 | SvUTF8_on(sv); |
4757a243 |
391 | |
392 | return matches; |
393 | } |
394 | |
942e002e |
395 | STATIC I32 |
21304a3b |
396 | S_do_trans_count_utf8(pTHX_ SV *sv) |
4757a243 |
397 | { |
93524f2b |
398 | const U8 *s; |
399 | const U8 *start = 0, *send; |
4757a243 |
400 | I32 matches = 0; |
401 | STRLEN len; |
402 | |
53c1dcc0 |
403 | SV* const rv = (SV*)cSVOP->op_sv; |
404 | HV* const hv = (HV*)SvRV(rv); |
405 | SV** const svp = hv_fetch(hv, "NONE", 4, FALSE); |
f54cb97a |
406 | const UV none = svp ? SvUV(*svp) : 0x7fffffff; |
407 | const UV extra = none + 1; |
1aa99e6b |
408 | U8 hibit = 0; |
4757a243 |
409 | |
93524f2b |
410 | s = (const U8*)SvPV_const(sv, len); |
1aa99e6b |
411 | if (!SvUTF8(sv)) { |
4373e329 |
412 | const U8 *t = s; |
413 | const U8 *e = s + len; |
c4d5f83a |
414 | while (t < e) { |
4373e329 |
415 | const U8 ch = *t++; |
c4d5f83a |
416 | if ((hibit = !NATIVE_IS_INVARIANT(ch))) |
1aa99e6b |
417 | break; |
c4d5f83a |
418 | } |
1aa99e6b |
419 | if (hibit) |
420 | start = s = bytes_to_utf8(s, &len); |
421 | } |
4757a243 |
422 | send = s + len; |
423 | |
424 | while (s < send) { |
f54cb97a |
425 | UV uv; |
3568d838 |
426 | if ((uv = swash_fetch(rv, s, TRUE)) < none || uv == extra) |
4757a243 |
427 | matches++; |
834a4ddd |
428 | s += UTF8SKIP(s); |
4757a243 |
429 | } |
1aa99e6b |
430 | if (hibit) |
431 | Safefree(start); |
4757a243 |
432 | |
433 | return matches; |
434 | } |
435 | |
942e002e |
436 | STATIC I32 |
21304a3b |
437 | S_do_trans_complex_utf8(pTHX_ SV *sv) |
4757a243 |
438 | { |
1aa99e6b |
439 | U8 *start, *send; |
4757a243 |
440 | U8 *d; |
441 | I32 matches = 0; |
4373e329 |
442 | const I32 squash = PL_op->op_private & OPpTRANS_SQUASH; |
443 | const I32 del = PL_op->op_private & OPpTRANS_DELETE; |
444 | const I32 grows = PL_op->op_private & OPpTRANS_GROWS; |
53c1dcc0 |
445 | SV * const rv = (SV*)cSVOP->op_sv; |
446 | HV * const hv = (HV*)SvRV(rv); |
4757a243 |
447 | SV** svp = hv_fetch(hv, "NONE", 4, FALSE); |
f54cb97a |
448 | const UV none = svp ? SvUV(*svp) : 0x7fffffff; |
449 | const UV extra = none + 1; |
b7953727 |
450 | UV final = 0; |
45005bfb |
451 | bool havefinal = FALSE; |
4757a243 |
452 | STRLEN len; |
1aa99e6b |
453 | U8 *dstart, *dend; |
1aa99e6b |
454 | U8 hibit = 0; |
4757a243 |
455 | |
f54cb97a |
456 | U8 *s = (U8*)SvPV(sv, len); |
457 | const I32 isutf8 = SvUTF8(sv); |
1aa99e6b |
458 | if (!isutf8) { |
53c1dcc0 |
459 | const U8 *t = s; |
460 | const U8 * const e = s + len; |
c4d5f83a |
461 | while (t < e) { |
f54cb97a |
462 | const U8 ch = *t++; |
c4d5f83a |
463 | if ((hibit = !NATIVE_IS_INVARIANT(ch))) |
1aa99e6b |
464 | break; |
c4d5f83a |
465 | } |
1aa99e6b |
466 | if (hibit) |
467 | s = bytes_to_utf8(s, &len); |
468 | } |
4757a243 |
469 | send = s + len; |
1aa99e6b |
470 | start = s; |
4757a243 |
471 | |
472 | svp = hv_fetch(hv, "FINAL", 5, FALSE); |
45005bfb |
473 | if (svp) { |
4757a243 |
474 | final = SvUV(*svp); |
45005bfb |
475 | havefinal = TRUE; |
476 | } |
4757a243 |
477 | |
9b877dbb |
478 | if (grows) { |
479 | /* d needs to be bigger than s, in case e.g. upgrading is required */ |
a02a5408 |
480 | Newx(d, len * 3 + UTF8_MAXBYTES, U8); |
9b877dbb |
481 | dend = d + len * 3; |
482 | dstart = d; |
483 | } |
484 | else { |
485 | dstart = d = s; |
486 | dend = d + len; |
487 | } |
4757a243 |
488 | |
489 | if (squash) { |
490 | UV puv = 0xfeedface; |
491 | while (s < send) { |
f54cb97a |
492 | UV uv = swash_fetch(rv, s, TRUE); |
9041c2e3 |
493 | |
9b877dbb |
494 | if (d > dend) { |
4373e329 |
495 | const STRLEN clen = d - dstart; |
496 | const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES; |
9b877dbb |
497 | if (!grows) |
7d85a32c |
498 | Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__); |
89ebb4a3 |
499 | Renew(dstart, nlen + UTF8_MAXBYTES, U8); |
1aa99e6b |
500 | d = dstart + clen; |
501 | dend = dstart + nlen; |
4757a243 |
502 | } |
503 | if (uv < none) { |
504 | matches++; |
94472101 |
505 | s += UTF8SKIP(s); |
4757a243 |
506 | if (uv != puv) { |
7d85a32c |
507 | d = uvuni_to_utf8(d, uv); |
4757a243 |
508 | puv = uv; |
509 | } |
4757a243 |
510 | continue; |
511 | } |
512 | else if (uv == none) { /* "none" is unmapped character */ |
4373e329 |
513 | const int i = UTF8SKIP(s); |
0376ff32 |
514 | Move(s, d, i, U8); |
9b877dbb |
515 | d += i; |
516 | s += i; |
4757a243 |
517 | puv = 0xfeedface; |
518 | continue; |
a0ed51b3 |
519 | } |
4757a243 |
520 | else if (uv == extra && !del) { |
521 | matches++; |
45005bfb |
522 | if (havefinal) { |
523 | s += UTF8SKIP(s); |
524 | if (puv != final) { |
7d85a32c |
525 | d = uvuni_to_utf8(d, final); |
45005bfb |
526 | puv = final; |
527 | } |
528 | } |
529 | else { |
530 | STRLEN len; |
7d85a32c |
531 | uv = utf8_to_uvuni(s, &len); |
45005bfb |
532 | if (uv != puv) { |
0376ff32 |
533 | Move(s, d, len, U8); |
45005bfb |
534 | d += len; |
535 | puv = uv; |
536 | } |
537 | s += len; |
4757a243 |
538 | } |
4757a243 |
539 | continue; |
540 | } |
01ec43d0 |
541 | matches++; /* "none+1" is delete character */ |
542 | s += UTF8SKIP(s); |
a0ed51b3 |
543 | } |
79072805 |
544 | } |
545 | else { |
4757a243 |
546 | while (s < send) { |
f54cb97a |
547 | const UV uv = swash_fetch(rv, s, TRUE); |
9b877dbb |
548 | if (d > dend) { |
f54cb97a |
549 | const STRLEN clen = d - dstart; |
550 | const STRLEN nlen = dend - dstart + len + UTF8_MAXBYTES; |
9b877dbb |
551 | if (!grows) |
7d85a32c |
552 | Perl_croak(aTHX_ "panic: do_trans_complex_utf8 line %d",__LINE__); |
89ebb4a3 |
553 | Renew(dstart, nlen + UTF8_MAXBYTES, U8); |
1aa99e6b |
554 | d = dstart + clen; |
555 | dend = dstart + nlen; |
a0ed51b3 |
556 | } |
4757a243 |
557 | if (uv < none) { |
558 | matches++; |
01ec43d0 |
559 | s += UTF8SKIP(s); |
7d85a32c |
560 | d = uvuni_to_utf8(d, uv); |
4757a243 |
561 | continue; |
a0ed51b3 |
562 | } |
4757a243 |
563 | else if (uv == none) { /* "none" is unmapped character */ |
f54cb97a |
564 | const int i = UTF8SKIP(s); |
0376ff32 |
565 | Move(s, d, i, U8); |
9b877dbb |
566 | d += i; |
567 | s += i; |
4757a243 |
568 | continue; |
79072805 |
569 | } |
4757a243 |
570 | else if (uv == extra && !del) { |
571 | matches++; |
01ec43d0 |
572 | s += UTF8SKIP(s); |
7d85a32c |
573 | d = uvuni_to_utf8(d, final); |
4757a243 |
574 | continue; |
575 | } |
01ec43d0 |
576 | matches++; /* "none+1" is delete character */ |
577 | s += UTF8SKIP(s); |
79072805 |
578 | } |
4757a243 |
579 | } |
16ec844d |
580 | if (grows || hibit) { |
9b877dbb |
581 | sv_setpvn(sv, (char*)dstart, d - dstart); |
582 | Safefree(dstart); |
16ec844d |
583 | if (grows && hibit) |
584 | Safefree(start); |
9b877dbb |
585 | } |
586 | else { |
587 | *d = '\0'; |
588 | SvCUR_set(sv, d - dstart); |
589 | } |
1aa99e6b |
590 | SvUTF8_on(sv); |
4757a243 |
591 | SvSETMAGIC(sv); |
592 | |
593 | return matches; |
594 | } |
595 | |
596 | I32 |
864dbfa3 |
597 | Perl_do_trans(pTHX_ SV *sv) |
4757a243 |
598 | { |
599 | STRLEN len; |
4373e329 |
600 | const I32 hasutf = (PL_op->op_private & |
036b4402 |
601 | (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)); |
4757a243 |
602 | |
2233f375 |
603 | if (SvREADONLY(sv)) { |
765f542d |
604 | if (SvIsCOW(sv)) |
605 | sv_force_normal_flags(sv, 0); |
2233f375 |
606 | if (SvREADONLY(sv) && !(PL_op->op_private & OPpTRANS_IDENTICAL)) |
607 | Perl_croak(aTHX_ PL_no_modify); |
608 | } |
10516c54 |
609 | (void)SvPV_const(sv, len); |
4757a243 |
610 | if (!len) |
611 | return 0; |
d59e14db |
612 | if (!(PL_op->op_private & OPpTRANS_IDENTICAL)) { |
613 | if (!SvPOKp(sv)) |
614 | (void)SvPV_force(sv, len); |
2de7b02f |
615 | (void)SvPOK_only_UTF8(sv); |
d59e14db |
616 | } |
4757a243 |
617 | |
cea2e8a9 |
618 | DEBUG_t( Perl_deb(aTHX_ "2.TBL\n")); |
4757a243 |
619 | |
a77e643a |
620 | switch (PL_op->op_private & ~hasutf & ( |
621 | OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF|OPpTRANS_IDENTICAL| |
622 | OPpTRANS_SQUASH|OPpTRANS_DELETE|OPpTRANS_COMPLEMENT)) { |
4757a243 |
623 | case 0: |
01ec43d0 |
624 | if (hasutf) |
625 | return do_trans_simple_utf8(sv); |
626 | else |
627 | return do_trans_simple(sv); |
4757a243 |
628 | |
629 | case OPpTRANS_IDENTICAL: |
05d340b8 |
630 | case OPpTRANS_IDENTICAL|OPpTRANS_COMPLEMENT: |
01ec43d0 |
631 | if (hasutf) |
632 | return do_trans_count_utf8(sv); |
633 | else |
634 | return do_trans_count(sv); |
4757a243 |
635 | |
636 | default: |
01ec43d0 |
637 | if (hasutf) |
036b4402 |
638 | return do_trans_complex_utf8(sv); |
4757a243 |
639 | else |
036b4402 |
640 | return do_trans_complex(sv); |
79072805 |
641 | } |
79072805 |
642 | } |
643 | |
644 | void |
864dbfa3 |
645 | Perl_do_join(pTHX_ register SV *sv, SV *del, register SV **mark, register SV **sp) |
79072805 |
646 | { |
53c1dcc0 |
647 | SV ** const oldmark = mark; |
79072805 |
648 | register I32 items = sp - mark; |
79072805 |
649 | register STRLEN len; |
463ee0b2 |
650 | STRLEN delimlen; |
79072805 |
651 | |
5c144d81 |
652 | (void) SvPV_const(del, delimlen); /* stringify and get the delimlen */ |
5ba99574 |
653 | /* SvCUR assumes it's SvPOK() and woe betide you if it's not. */ |
516a5887 |
654 | |
79072805 |
655 | mark++; |
656 | len = (items > 0 ? (delimlen * (items - 1) ) : 0); |
862a34c6 |
657 | SvUPGRADE(sv, SVt_PV); |
79072805 |
658 | if (SvLEN(sv) < len + items) { /* current length is way too short */ |
659 | while (items-- > 0) { |
1426bbf4 |
660 | if (*mark && !SvGAMAGIC(*mark) && SvOK(*mark)) { |
f54cb97a |
661 | STRLEN tmplen; |
5c144d81 |
662 | SvPV_const(*mark, tmplen); |
463ee0b2 |
663 | len += tmplen; |
79072805 |
664 | } |
665 | mark++; |
666 | } |
667 | SvGROW(sv, len + 1); /* so try to pre-extend */ |
668 | |
669 | mark = oldmark; |
db7c17d7 |
670 | items = sp - mark; |
79072805 |
671 | ++mark; |
672 | } |
673 | |
fc0199cd |
674 | sv_setpvn(sv, "", 0); |
e4803c42 |
675 | /* sv_setpv retains old UTF8ness [perl #24846] */ |
fb622db0 |
676 | SvUTF8_off(sv); |
e4803c42 |
677 | |
8d6d96c1 |
678 | if (PL_tainting && SvMAGICAL(sv)) |
679 | SvTAINTED_off(sv); |
680 | |
463ee0b2 |
681 | if (items-- > 0) { |
92d29cee |
682 | if (*mark) |
683 | sv_catsv(sv, *mark); |
463ee0b2 |
684 | mark++; |
685 | } |
8d6d96c1 |
686 | |
c512ce4f |
687 | if (delimlen) { |
79072805 |
688 | for (; items > 0; items--,mark++) { |
c512ce4f |
689 | sv_catsv(sv,del); |
79072805 |
690 | sv_catsv(sv,*mark); |
691 | } |
692 | } |
693 | else { |
694 | for (; items > 0; items--,mark++) |
695 | sv_catsv(sv,*mark); |
696 | } |
697 | SvSETMAGIC(sv); |
698 | } |
699 | |
700 | void |
864dbfa3 |
701 | Perl_do_sprintf(pTHX_ SV *sv, I32 len, SV **sarg) |
79072805 |
702 | { |
46fc3d4c |
703 | STRLEN patlen; |
53c1dcc0 |
704 | const char * const pat = SvPV_const(*sarg, patlen); |
46fc3d4c |
705 | bool do_taint = FALSE; |
706 | |
5b781b5b |
707 | SvUTF8_off(sv); |
2cf2cfc6 |
708 | if (DO_UTF8(*sarg)) |
709 | SvUTF8_on(sv); |
46fc3d4c |
710 | sv_vsetpvfn(sv, pat, patlen, Null(va_list*), sarg + 1, len - 1, &do_taint); |
79072805 |
711 | SvSETMAGIC(sv); |
46fc3d4c |
712 | if (do_taint) |
713 | SvTAINTED_on(sv); |
79072805 |
714 | } |
715 | |
33b45480 |
716 | /* currently converts input to bytes if possible, but doesn't sweat failure */ |
81e118e0 |
717 | UV |
718 | Perl_do_vecget(pTHX_ SV *sv, I32 offset, I32 size) |
719 | { |
720 | STRLEN srclen, len; |
10516c54 |
721 | const unsigned char *s = (const unsigned char *) SvPV_const(sv, srclen); |
81e118e0 |
722 | UV retnum = 0; |
723 | |
a50d7633 |
724 | if (offset < 0) |
81e118e0 |
725 | return retnum; |
8e84507e |
726 | if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */ |
a50d7633 |
727 | Perl_croak(aTHX_ "Illegal number of bits in vec"); |
246fae53 |
728 | |
dcad2880 |
729 | if (SvUTF8(sv)) |
33b45480 |
730 | (void) Perl_sv_utf8_downgrade(aTHX_ sv, TRUE); |
246fae53 |
731 | |
81e118e0 |
732 | offset *= size; /* turn into bit offset */ |
733 | len = (offset + size + 7) / 8; /* required number of bytes */ |
734 | if (len > srclen) { |
735 | if (size <= 8) |
736 | retnum = 0; |
737 | else { |
738 | offset >>= 3; /* turn into byte offset */ |
739 | if (size == 16) { |
eb160463 |
740 | if ((STRLEN)offset >= srclen) |
81e118e0 |
741 | retnum = 0; |
742 | else |
628e1a40 |
743 | retnum = (UV) s[offset] << 8; |
81e118e0 |
744 | } |
745 | else if (size == 32) { |
eb160463 |
746 | if ((STRLEN)offset >= srclen) |
81e118e0 |
747 | retnum = 0; |
eb160463 |
748 | else if ((STRLEN)(offset + 1) >= srclen) |
81e118e0 |
749 | retnum = |
750 | ((UV) s[offset ] << 24); |
eb160463 |
751 | else if ((STRLEN)(offset + 2) >= srclen) |
81e118e0 |
752 | retnum = |
753 | ((UV) s[offset ] << 24) + |
754 | ((UV) s[offset + 1] << 16); |
755 | else |
756 | retnum = |
757 | ((UV) s[offset ] << 24) + |
758 | ((UV) s[offset + 1] << 16) + |
759 | ( s[offset + 2] << 8); |
760 | } |
d7d93a81 |
761 | #ifdef UV_IS_QUAD |
c5a0f51a |
762 | else if (size == 64) { |
c5a0f51a |
763 | if (ckWARN(WARN_PORTABLE)) |
9014280d |
764 | Perl_warner(aTHX_ packWARN(WARN_PORTABLE), |
c5a0f51a |
765 | "Bit vector size > 32 non-portable"); |
766 | if (offset >= srclen) |
767 | retnum = 0; |
768 | else if (offset + 1 >= srclen) |
769 | retnum = |
770 | (UV) s[offset ] << 56; |
771 | else if (offset + 2 >= srclen) |
772 | retnum = |
773 | ((UV) s[offset ] << 56) + |
774 | ((UV) s[offset + 1] << 48); |
775 | else if (offset + 3 >= srclen) |
776 | retnum = |
777 | ((UV) s[offset ] << 56) + |
778 | ((UV) s[offset + 1] << 48) + |
779 | ((UV) s[offset + 2] << 40); |
780 | else if (offset + 4 >= srclen) |
781 | retnum = |
782 | ((UV) s[offset ] << 56) + |
783 | ((UV) s[offset + 1] << 48) + |
784 | ((UV) s[offset + 2] << 40) + |
785 | ((UV) s[offset + 3] << 32); |
786 | else if (offset + 5 >= srclen) |
787 | retnum = |
788 | ((UV) s[offset ] << 56) + |
789 | ((UV) s[offset + 1] << 48) + |
790 | ((UV) s[offset + 2] << 40) + |
791 | ((UV) s[offset + 3] << 32) + |
792 | ( s[offset + 4] << 24); |
793 | else if (offset + 6 >= srclen) |
794 | retnum = |
795 | ((UV) s[offset ] << 56) + |
796 | ((UV) s[offset + 1] << 48) + |
797 | ((UV) s[offset + 2] << 40) + |
798 | ((UV) s[offset + 3] << 32) + |
799 | ((UV) s[offset + 4] << 24) + |
800 | ((UV) s[offset + 5] << 16); |
801 | else |
8e84507e |
802 | retnum = |
c5a0f51a |
803 | ((UV) s[offset ] << 56) + |
804 | ((UV) s[offset + 1] << 48) + |
805 | ((UV) s[offset + 2] << 40) + |
806 | ((UV) s[offset + 3] << 32) + |
807 | ((UV) s[offset + 4] << 24) + |
808 | ((UV) s[offset + 5] << 16) + |
628e1a40 |
809 | ( s[offset + 6] << 8); |
c5a0f51a |
810 | } |
811 | #endif |
81e118e0 |
812 | } |
813 | } |
814 | else if (size < 8) |
815 | retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1); |
816 | else { |
817 | offset >>= 3; /* turn into byte offset */ |
818 | if (size == 8) |
819 | retnum = s[offset]; |
820 | else if (size == 16) |
821 | retnum = |
628e1a40 |
822 | ((UV) s[offset] << 8) + |
81e118e0 |
823 | s[offset + 1]; |
824 | else if (size == 32) |
825 | retnum = |
826 | ((UV) s[offset ] << 24) + |
827 | ((UV) s[offset + 1] << 16) + |
828 | ( s[offset + 2] << 8) + |
829 | s[offset + 3]; |
d7d93a81 |
830 | #ifdef UV_IS_QUAD |
c5a0f51a |
831 | else if (size == 64) { |
c5a0f51a |
832 | if (ckWARN(WARN_PORTABLE)) |
9014280d |
833 | Perl_warner(aTHX_ packWARN(WARN_PORTABLE), |
c5a0f51a |
834 | "Bit vector size > 32 non-portable"); |
835 | retnum = |
836 | ((UV) s[offset ] << 56) + |
837 | ((UV) s[offset + 1] << 48) + |
838 | ((UV) s[offset + 2] << 40) + |
839 | ((UV) s[offset + 3] << 32) + |
840 | ((UV) s[offset + 4] << 24) + |
841 | ((UV) s[offset + 5] << 16) + |
628e1a40 |
842 | ( s[offset + 6] << 8) + |
c5a0f51a |
843 | s[offset + 7]; |
844 | } |
845 | #endif |
81e118e0 |
846 | } |
847 | |
848 | return retnum; |
849 | } |
850 | |
33b45480 |
851 | /* currently converts input to bytes if possible but doesn't sweat failures, |
852 | * although it does ensure that the string it clobbers is not marked as |
853 | * utf8-valid any more |
854 | */ |
79072805 |
855 | void |
864dbfa3 |
856 | Perl_do_vecset(pTHX_ SV *sv) |
79072805 |
857 | { |
858 | SV *targ = LvTARG(sv); |
859 | register I32 offset; |
860 | register I32 size; |
8990e307 |
861 | register unsigned char *s; |
81e118e0 |
862 | register UV lval; |
79072805 |
863 | I32 mask; |
a0d0e21e |
864 | STRLEN targlen; |
865 | STRLEN len; |
79072805 |
866 | |
8990e307 |
867 | if (!targ) |
868 | return; |
a0d0e21e |
869 | s = (unsigned char*)SvPV_force(targ, targlen); |
246fae53 |
870 | if (SvUTF8(targ)) { |
33b45480 |
871 | /* This is handled by the SvPOK_only below... |
872 | if (!Perl_sv_utf8_downgrade(aTHX_ targ, TRUE)) |
873 | SvUTF8_off(targ); |
874 | */ |
875 | (void) Perl_sv_utf8_downgrade(aTHX_ targ, TRUE); |
246fae53 |
876 | } |
877 | |
4ebbc975 |
878 | (void)SvPOK_only(targ); |
81e118e0 |
879 | lval = SvUV(sv); |
79072805 |
880 | offset = LvTARGOFF(sv); |
fe58ced6 |
881 | if (offset < 0) |
ed9aa3b7 |
882 | Perl_croak(aTHX_ "Negative offset to vec in lvalue context"); |
79072805 |
883 | size = LvTARGLEN(sv); |
8e84507e |
884 | if (size < 1 || (size & (size-1))) /* size < 1 or not a power of two */ |
a50d7633 |
885 | Perl_croak(aTHX_ "Illegal number of bits in vec"); |
8e84507e |
886 | |
81e118e0 |
887 | offset *= size; /* turn into bit offset */ |
888 | len = (offset + size + 7) / 8; /* required number of bytes */ |
a0d0e21e |
889 | if (len > targlen) { |
890 | s = (unsigned char*)SvGROW(targ, len + 1); |
12ae5dfc |
891 | (void)memzero((char *)(s + targlen), len - targlen + 1); |
a0d0e21e |
892 | SvCUR_set(targ, len); |
893 | } |
8e84507e |
894 | |
79072805 |
895 | if (size < 8) { |
896 | mask = (1 << size) - 1; |
897 | size = offset & 7; |
898 | lval &= mask; |
81e118e0 |
899 | offset >>= 3; /* turn into byte offset */ |
79072805 |
900 | s[offset] &= ~(mask << size); |
901 | s[offset] |= lval << size; |
902 | } |
903 | else { |
81e118e0 |
904 | offset >>= 3; /* turn into byte offset */ |
79072805 |
905 | if (size == 8) |
eb160463 |
906 | s[offset ] = (U8)( lval & 0xff); |
79072805 |
907 | else if (size == 16) { |
eb160463 |
908 | s[offset ] = (U8)((lval >> 8) & 0xff); |
909 | s[offset+1] = (U8)( lval & 0xff); |
79072805 |
910 | } |
911 | else if (size == 32) { |
eb160463 |
912 | s[offset ] = (U8)((lval >> 24) & 0xff); |
913 | s[offset+1] = (U8)((lval >> 16) & 0xff); |
914 | s[offset+2] = (U8)((lval >> 8) & 0xff); |
915 | s[offset+3] = (U8)( lval & 0xff); |
c5a0f51a |
916 | } |
d7d93a81 |
917 | #ifdef UV_IS_QUAD |
c5a0f51a |
918 | else if (size == 64) { |
c5a0f51a |
919 | if (ckWARN(WARN_PORTABLE)) |
9014280d |
920 | Perl_warner(aTHX_ packWARN(WARN_PORTABLE), |
c5a0f51a |
921 | "Bit vector size > 32 non-portable"); |
eb160463 |
922 | s[offset ] = (U8)((lval >> 56) & 0xff); |
923 | s[offset+1] = (U8)((lval >> 48) & 0xff); |
924 | s[offset+2] = (U8)((lval >> 40) & 0xff); |
925 | s[offset+3] = (U8)((lval >> 32) & 0xff); |
926 | s[offset+4] = (U8)((lval >> 24) & 0xff); |
927 | s[offset+5] = (U8)((lval >> 16) & 0xff); |
928 | s[offset+6] = (U8)((lval >> 8) & 0xff); |
929 | s[offset+7] = (U8)( lval & 0xff); |
79072805 |
930 | } |
dc1e3f56 |
931 | #endif |
79072805 |
932 | } |
7bb043c3 |
933 | SvSETMAGIC(targ); |
79072805 |
934 | } |
935 | |
936 | void |
864dbfa3 |
937 | Perl_do_chop(pTHX_ register SV *astr, register SV *sv) |
79072805 |
938 | { |
463ee0b2 |
939 | STRLEN len; |
a0d0e21e |
940 | char *s; |
8e84507e |
941 | |
79072805 |
942 | if (SvTYPE(sv) == SVt_PVAV) { |
a0d0e21e |
943 | register I32 i; |
a0d0e21e |
944 | AV* av = (AV*)sv; |
f54cb97a |
945 | const I32 max = AvFILL(av); |
946 | |
947 | for (i = 0; i <= max; i++) { |
a0d0e21e |
948 | sv = (SV*)av_fetch(av, i, FALSE); |
3280af22 |
949 | if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef)) |
a0d0e21e |
950 | do_chop(astr, sv); |
951 | } |
952 | return; |
79072805 |
953 | } |
aa854799 |
954 | else if (SvTYPE(sv) == SVt_PVHV) { |
a0d0e21e |
955 | HV* hv = (HV*)sv; |
956 | HE* entry; |
957 | (void)hv_iterinit(hv); |
155aba94 |
958 | while ((entry = hv_iternext(hv))) |
a0d0e21e |
959 | do_chop(astr,hv_iterval(hv,entry)); |
960 | return; |
79072805 |
961 | } |
991e6d41 |
962 | else if (SvREADONLY(sv)) { |
963 | if (SvFAKE(sv)) { |
964 | /* SV is copy-on-write */ |
965 | sv_force_normal_flags(sv, 0); |
966 | } |
967 | if (SvREADONLY(sv)) |
968 | Perl_croak(aTHX_ PL_no_modify); |
969 | } |
a0d0e21e |
970 | s = SvPV(sv, len); |
748a9306 |
971 | if (len && !SvPOK(sv)) |
a0d0e21e |
972 | s = SvPV_force(sv, len); |
7e2040f0 |
973 | if (DO_UTF8(sv)) { |
a0ed51b3 |
974 | if (s && len) { |
975 | char *send = s + len; |
976 | char *start = s; |
977 | s = send - 1; |
a0dbb045 |
978 | while (s > start && UTF8_IS_CONTINUATION(*s)) |
979 | s--; |
ace7757b |
980 | if (is_utf8_string((U8*)s, send - s)) { |
a0dbb045 |
981 | sv_setpvn(astr, s, send - s); |
982 | *s = '\0'; |
983 | SvCUR_set(sv, s - start); |
984 | SvNIOK_off(sv); |
985 | SvUTF8_on(astr); |
986 | } |
a0ed51b3 |
987 | } |
988 | else |
989 | sv_setpvn(astr, "", 0); |
990 | } |
7e2040f0 |
991 | else if (s && len) { |
a0d0e21e |
992 | s += --len; |
993 | sv_setpvn(astr, s, 1); |
994 | *s = '\0'; |
995 | SvCUR_set(sv, len); |
2c19a612 |
996 | SvUTF8_off(sv); |
a0d0e21e |
997 | SvNIOK_off(sv); |
79072805 |
998 | } |
999 | else |
a0d0e21e |
1000 | sv_setpvn(astr, "", 0); |
1001 | SvSETMAGIC(sv); |
7e2040f0 |
1002 | } |
a0d0e21e |
1003 | |
1004 | I32 |
864dbfa3 |
1005 | Perl_do_chomp(pTHX_ register SV *sv) |
a0d0e21e |
1006 | { |
c07a80fd |
1007 | register I32 count; |
a0d0e21e |
1008 | STRLEN len; |
1009 | char *s; |
c4c87a06 |
1010 | char *temp_buffer = NULL; |
a6aa349d |
1011 | SV* svrecode = Nullsv; |
c07a80fd |
1012 | |
3280af22 |
1013 | if (RsSNARF(PL_rs)) |
c07a80fd |
1014 | return 0; |
4c5a6083 |
1015 | if (RsRECORD(PL_rs)) |
1016 | return 0; |
c07a80fd |
1017 | count = 0; |
a0d0e21e |
1018 | if (SvTYPE(sv) == SVt_PVAV) { |
1019 | register I32 i; |
890ce7af |
1020 | AV* const av = (AV*)sv; |
f54cb97a |
1021 | const I32 max = AvFILL(av); |
1022 | |
1023 | for (i = 0; i <= max; i++) { |
a0d0e21e |
1024 | sv = (SV*)av_fetch(av, i, FALSE); |
3280af22 |
1025 | if (sv && ((sv = *(SV**)sv), sv != &PL_sv_undef)) |
a0d0e21e |
1026 | count += do_chomp(sv); |
1027 | } |
1028 | return count; |
1029 | } |
aa854799 |
1030 | else if (SvTYPE(sv) == SVt_PVHV) { |
890ce7af |
1031 | HV* const hv = (HV*)sv; |
a0d0e21e |
1032 | HE* entry; |
1033 | (void)hv_iterinit(hv); |
155aba94 |
1034 | while ((entry = hv_iternext(hv))) |
a0d0e21e |
1035 | count += do_chomp(hv_iterval(hv,entry)); |
1036 | return count; |
1037 | } |
991e6d41 |
1038 | else if (SvREADONLY(sv)) { |
1039 | if (SvFAKE(sv)) { |
1040 | /* SV is copy-on-write */ |
1041 | sv_force_normal_flags(sv, 0); |
1042 | } |
1043 | if (SvREADONLY(sv)) |
1044 | Perl_croak(aTHX_ PL_no_modify); |
1045 | } |
a6aa349d |
1046 | |
1047 | if (PL_encoding) { |
1048 | if (!SvUTF8(sv)) { |
1049 | /* XXX, here sv is utf8-ized as a side-effect! |
1050 | If encoding.pm is used properly, almost string-generating |
1051 | operations, including literal strings, chr(), input data, etc. |
1052 | should have been utf8-ized already, right? |
1053 | */ |
1054 | sv_recode_to_utf8(sv, PL_encoding); |
1055 | } |
1056 | } |
1057 | |
a0d0e21e |
1058 | s = SvPV(sv, len); |
a0d0e21e |
1059 | if (s && len) { |
1060 | s += --len; |
3280af22 |
1061 | if (RsPARA(PL_rs)) { |
a0d0e21e |
1062 | if (*s != '\n') |
1063 | goto nope; |
1064 | ++count; |
1065 | while (len && s[-1] == '\n') { |
1066 | --len; |
1067 | --s; |
1068 | ++count; |
1069 | } |
1070 | } |
a0d0e21e |
1071 | else { |
a6aa349d |
1072 | STRLEN rslen, rs_charlen; |
93524f2b |
1073 | const char *rsptr = SvPV_const(PL_rs, rslen); |
a6aa349d |
1074 | |
1075 | rs_charlen = SvUTF8(PL_rs) |
1076 | ? sv_len_utf8(PL_rs) |
1077 | : rslen; |
1078 | |
c4c87a06 |
1079 | if (SvUTF8(PL_rs) != SvUTF8(sv)) { |
1080 | /* Assumption is that rs is shorter than the scalar. */ |
1081 | if (SvUTF8(PL_rs)) { |
1082 | /* RS is utf8, scalar is 8 bit. */ |
1083 | bool is_utf8 = TRUE; |
1084 | temp_buffer = (char*)bytes_from_utf8((U8*)rsptr, |
1085 | &rslen, &is_utf8); |
1086 | if (is_utf8) { |
1087 | /* Cannot downgrade, therefore cannot possibly match |
1088 | */ |
1089 | assert (temp_buffer == rsptr); |
1090 | temp_buffer = NULL; |
1091 | goto nope; |
1092 | } |
1093 | rsptr = temp_buffer; |
a6aa349d |
1094 | } |
1095 | else if (PL_encoding) { |
1096 | /* RS is 8 bit, encoding.pm is used. |
1097 | * Do not recode PL_rs as a side-effect. */ |
1098 | svrecode = newSVpvn(rsptr, rslen); |
1099 | sv_recode_to_utf8(svrecode, PL_encoding); |
93524f2b |
1100 | rsptr = SvPV_const(svrecode, rslen); |
a6aa349d |
1101 | rs_charlen = sv_len_utf8(svrecode); |
1102 | } |
1103 | else { |
c4c87a06 |
1104 | /* RS is 8 bit, scalar is utf8. */ |
1105 | temp_buffer = (char*)bytes_to_utf8((U8*)rsptr, &rslen); |
1106 | rsptr = temp_buffer; |
1107 | } |
1108 | } |
c07a80fd |
1109 | if (rslen == 1) { |
1110 | if (*s != *rsptr) |
1111 | goto nope; |
1112 | ++count; |
1113 | } |
1114 | else { |
8c2cee6f |
1115 | if (len < rslen - 1) |
c07a80fd |
1116 | goto nope; |
1117 | len -= rslen - 1; |
1118 | s -= rslen - 1; |
36477c24 |
1119 | if (memNE(s, rsptr, rslen)) |
c07a80fd |
1120 | goto nope; |
a6aa349d |
1121 | count += rs_charlen; |
c07a80fd |
1122 | } |
a0d0e21e |
1123 | } |
8b6b16e7 |
1124 | s = SvPV_force_nolen(sv); |
a0d0e21e |
1125 | SvCUR_set(sv, len); |
9b33ce3b |
1126 | *SvEND(sv) = '\0'; |
a0d0e21e |
1127 | SvNIOK_off(sv); |
9b33ce3b |
1128 | SvSETMAGIC(sv); |
a0d0e21e |
1129 | } |
1130 | nope: |
a6aa349d |
1131 | |
1132 | if (svrecode) |
1133 | SvREFCNT_dec(svrecode); |
1134 | |
c4c87a06 |
1135 | Safefree(temp_buffer); |
a0d0e21e |
1136 | return count; |
8e84507e |
1137 | } |
79072805 |
1138 | |
1139 | void |
864dbfa3 |
1140 | Perl_do_vop(pTHX_ I32 optype, SV *sv, SV *left, SV *right) |
79072805 |
1141 | { |
1142 | #ifdef LIBERAL |
1143 | register long *dl; |
1144 | register long *ll; |
1145 | register long *rl; |
1146 | #endif |
1147 | register char *dc; |
463ee0b2 |
1148 | STRLEN leftlen; |
1149 | STRLEN rightlen; |
e62f0680 |
1150 | register const char *lc; |
1151 | register const char *rc; |
79072805 |
1152 | register I32 len; |
a0d0e21e |
1153 | I32 lensave; |
e62f0680 |
1154 | const char *lsave; |
1155 | const char *rsave; |
f54cb97a |
1156 | const bool left_utf = DO_UTF8(left); |
1157 | const bool right_utf = DO_UTF8(right); |
b7953727 |
1158 | I32 needlen = 0; |
0c57e439 |
1159 | |
1160 | if (left_utf && !right_utf) |
1161 | sv_utf8_upgrade(right); |
a1ca4561 |
1162 | else if (!left_utf && right_utf) |
0c57e439 |
1163 | sv_utf8_upgrade(left); |
79072805 |
1164 | |
1fbd88dc |
1165 | if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv))) |
1166 | sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */ |
e62f0680 |
1167 | lsave = lc = SvPV_nomg_const(left, leftlen); |
1168 | rsave = rc = SvPV_nomg_const(right, rightlen); |
93a17b20 |
1169 | len = leftlen < rightlen ? leftlen : rightlen; |
a0d0e21e |
1170 | lensave = len; |
c9b3c8d0 |
1171 | if ((left_utf || right_utf) && (sv == left || sv == right)) { |
1172 | needlen = optype == OP_BIT_AND ? len : leftlen + rightlen; |
a02a5408 |
1173 | Newxz(dc, needlen + 1, char); |
c9b3c8d0 |
1174 | } |
1175 | else if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) { |
2596d9fe |
1176 | dc = SvPV_force_nomg_nolen(sv); |
eb160463 |
1177 | if (SvCUR(sv) < (STRLEN)len) { |
1178 | dc = SvGROW(sv, (STRLEN)(len + 1)); |
ff68c719 |
1179 | (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1); |
1180 | } |
c9b3c8d0 |
1181 | if (optype != OP_BIT_AND && (left_utf || right_utf)) |
1182 | dc = SvGROW(sv, leftlen + rightlen + 1); |
ff68c719 |
1183 | } |
1184 | else { |
c9b3c8d0 |
1185 | needlen = ((optype == OP_BIT_AND) |
1186 | ? len : (leftlen > rightlen ? leftlen : rightlen)); |
a02a5408 |
1187 | Newxz(dc, needlen + 1, char); |
ff68c719 |
1188 | (void)sv_usepvn(sv, dc, needlen); |
1189 | dc = SvPVX(sv); /* sv_usepvn() calls Renew() */ |
79072805 |
1190 | } |
a0d0e21e |
1191 | SvCUR_set(sv, len); |
1192 | (void)SvPOK_only(sv); |
0c57e439 |
1193 | if (left_utf || right_utf) { |
1194 | UV duc, luc, ruc; |
c9b3c8d0 |
1195 | char *dcsave = dc; |
0c57e439 |
1196 | STRLEN lulen = leftlen; |
1197 | STRLEN rulen = rightlen; |
ba210ebe |
1198 | STRLEN ulen; |
0c57e439 |
1199 | |
1200 | switch (optype) { |
1201 | case OP_BIT_AND: |
1202 | while (lulen && rulen) { |
9041c2e3 |
1203 | luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1204 | lc += ulen; |
1205 | lulen -= ulen; |
9041c2e3 |
1206 | ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1207 | rc += ulen; |
1208 | rulen -= ulen; |
1209 | duc = luc & ruc; |
9041c2e3 |
1210 | dc = (char*)uvchr_to_utf8((U8*)dc, duc); |
0c57e439 |
1211 | } |
c9b3c8d0 |
1212 | if (sv == left || sv == right) |
1213 | (void)sv_usepvn(sv, dcsave, needlen); |
1214 | SvCUR_set(sv, dc - dcsave); |
0c57e439 |
1215 | break; |
1216 | case OP_BIT_XOR: |
1217 | while (lulen && rulen) { |
9041c2e3 |
1218 | luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1219 | lc += ulen; |
1220 | lulen -= ulen; |
9041c2e3 |
1221 | ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1222 | rc += ulen; |
1223 | rulen -= ulen; |
1224 | duc = luc ^ ruc; |
9041c2e3 |
1225 | dc = (char*)uvchr_to_utf8((U8*)dc, duc); |
0c57e439 |
1226 | } |
1227 | goto mop_up_utf; |
1228 | case OP_BIT_OR: |
1229 | while (lulen && rulen) { |
9041c2e3 |
1230 | luc = utf8n_to_uvchr((U8*)lc, lulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1231 | lc += ulen; |
1232 | lulen -= ulen; |
9041c2e3 |
1233 | ruc = utf8n_to_uvchr((U8*)rc, rulen, &ulen, UTF8_ALLOW_ANYUV); |
0c57e439 |
1234 | rc += ulen; |
1235 | rulen -= ulen; |
1236 | duc = luc | ruc; |
9041c2e3 |
1237 | dc = (char*)uvchr_to_utf8((U8*)dc, duc); |
0c57e439 |
1238 | } |
1239 | mop_up_utf: |
c9b3c8d0 |
1240 | if (sv == left || sv == right) |
1241 | (void)sv_usepvn(sv, dcsave, needlen); |
1242 | SvCUR_set(sv, dc - dcsave); |
0c57e439 |
1243 | if (rulen) |
1244 | sv_catpvn(sv, rc, rulen); |
1245 | else if (lulen) |
1246 | sv_catpvn(sv, lc, lulen); |
1247 | else |
1248 | *SvEND(sv) = '\0'; |
1249 | break; |
1250 | } |
1251 | SvUTF8_on(sv); |
1252 | goto finish; |
1253 | } |
1254 | else |
79072805 |
1255 | #ifdef LIBERAL |
1256 | if (len >= sizeof(long)*4 && |
1257 | !((long)dc % sizeof(long)) && |
1258 | !((long)lc % sizeof(long)) && |
1259 | !((long)rc % sizeof(long))) /* It's almost always aligned... */ |
1260 | { |
f54cb97a |
1261 | const I32 remainder = len % (sizeof(long)*4); |
79072805 |
1262 | len /= (sizeof(long)*4); |
1263 | |
1264 | dl = (long*)dc; |
1265 | ll = (long*)lc; |
1266 | rl = (long*)rc; |
1267 | |
1268 | switch (optype) { |
1269 | case OP_BIT_AND: |
1270 | while (len--) { |
1271 | *dl++ = *ll++ & *rl++; |
1272 | *dl++ = *ll++ & *rl++; |
1273 | *dl++ = *ll++ & *rl++; |
1274 | *dl++ = *ll++ & *rl++; |
1275 | } |
1276 | break; |
a0d0e21e |
1277 | case OP_BIT_XOR: |
79072805 |
1278 | while (len--) { |
1279 | *dl++ = *ll++ ^ *rl++; |
1280 | *dl++ = *ll++ ^ *rl++; |
1281 | *dl++ = *ll++ ^ *rl++; |
1282 | *dl++ = *ll++ ^ *rl++; |
1283 | } |
1284 | break; |
1285 | case OP_BIT_OR: |
1286 | while (len--) { |
1287 | *dl++ = *ll++ | *rl++; |
1288 | *dl++ = *ll++ | *rl++; |
1289 | *dl++ = *ll++ | *rl++; |
1290 | *dl++ = *ll++ | *rl++; |
1291 | } |
1292 | } |
1293 | |
1294 | dc = (char*)dl; |
1295 | lc = (char*)ll; |
1296 | rc = (char*)rl; |
1297 | |
1298 | len = remainder; |
1299 | } |
1300 | #endif |
a0d0e21e |
1301 | { |
a0d0e21e |
1302 | switch (optype) { |
1303 | case OP_BIT_AND: |
1304 | while (len--) |
1305 | *dc++ = *lc++ & *rc++; |
1306 | break; |
1307 | case OP_BIT_XOR: |
1308 | while (len--) |
1309 | *dc++ = *lc++ ^ *rc++; |
1310 | goto mop_up; |
1311 | case OP_BIT_OR: |
1312 | while (len--) |
1313 | *dc++ = *lc++ | *rc++; |
1314 | mop_up: |
1315 | len = lensave; |
eb160463 |
1316 | if (rightlen > (STRLEN)len) |
a0d0e21e |
1317 | sv_catpvn(sv, rsave + len, rightlen - len); |
eb160463 |
1318 | else if (leftlen > (STRLEN)len) |
a0d0e21e |
1319 | sv_catpvn(sv, lsave + len, leftlen - len); |
4633a7c4 |
1320 | else |
1321 | *SvEND(sv) = '\0'; |
a0d0e21e |
1322 | break; |
1323 | } |
79072805 |
1324 | } |
0c57e439 |
1325 | finish: |
fb73857a |
1326 | SvTAINT(sv); |
79072805 |
1327 | } |
463ee0b2 |
1328 | |
1329 | OP * |
cea2e8a9 |
1330 | Perl_do_kv(pTHX) |
463ee0b2 |
1331 | { |
39644a26 |
1332 | dSP; |
463ee0b2 |
1333 | HV *hv = (HV*)POPs; |
800e9ae0 |
1334 | HV *keys; |
463ee0b2 |
1335 | register HE *entry; |
f54cb97a |
1336 | const I32 gimme = GIMME_V; |
1337 | const I32 dokv = (PL_op->op_type == OP_RV2HV || PL_op->op_type == OP_PADHV); |
1338 | const I32 dokeys = dokv || (PL_op->op_type == OP_KEYS); |
1339 | const I32 dovalues = dokv || (PL_op->op_type == OP_VALUES); |
463ee0b2 |
1340 | |
85581909 |
1341 | if (!hv) { |
78f9721b |
1342 | if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */ |
85581909 |
1343 | dTARGET; /* make sure to clear its target here */ |
1344 | if (SvTYPE(TARG) == SVt_PVLV) |
1345 | LvTARG(TARG) = Nullsv; |
1346 | PUSHs(TARG); |
1347 | } |
463ee0b2 |
1348 | RETURN; |
85581909 |
1349 | } |
748a9306 |
1350 | |
6d822dc4 |
1351 | keys = hv; |
800e9ae0 |
1352 | (void)hv_iterinit(keys); /* always reset iterator regardless */ |
748a9306 |
1353 | |
54310121 |
1354 | if (gimme == G_VOID) |
aa689395 |
1355 | RETURN; |
1356 | |
54310121 |
1357 | if (gimme == G_SCALAR) { |
6ee623d5 |
1358 | IV i; |
463ee0b2 |
1359 | dTARGET; |
1360 | |
78f9721b |
1361 | if (PL_op->op_flags & OPf_MOD || LVRET) { /* lvalue */ |
85581909 |
1362 | if (SvTYPE(TARG) < SVt_PVLV) { |
1363 | sv_upgrade(TARG, SVt_PVLV); |
14befaf4 |
1364 | sv_magic(TARG, Nullsv, PERL_MAGIC_nkeys, Nullch, 0); |
85581909 |
1365 | } |
1366 | LvTYPE(TARG) = 'k'; |
800e9ae0 |
1367 | if (LvTARG(TARG) != (SV*)keys) { |
6ff81951 |
1368 | if (LvTARG(TARG)) |
1369 | SvREFCNT_dec(LvTARG(TARG)); |
800e9ae0 |
1370 | LvTARG(TARG) = SvREFCNT_inc(keys); |
6ff81951 |
1371 | } |
85581909 |
1372 | PUSHs(TARG); |
1373 | RETURN; |
1374 | } |
1375 | |
14befaf4 |
1376 | if (! SvTIED_mg((SV*)keys, PERL_MAGIC_tied)) |
a98face1 |
1377 | i = HvKEYS(keys); |
463ee0b2 |
1378 | else { |
1379 | i = 0; |
800e9ae0 |
1380 | while (hv_iternext(keys)) i++; |
463ee0b2 |
1381 | } |
1382 | PUSHi( i ); |
1383 | RETURN; |
1384 | } |
1385 | |
8ed4b672 |
1386 | EXTEND(SP, HvKEYS(keys) * (dokeys + dovalues)); |
463ee0b2 |
1387 | |
463ee0b2 |
1388 | PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */ |
155aba94 |
1389 | while ((entry = hv_iternext(keys))) { |
463ee0b2 |
1390 | SPAGAIN; |
574c8022 |
1391 | if (dokeys) { |
890ce7af |
1392 | SV* const sv = hv_iterkeysv(entry); |
574c8022 |
1393 | XPUSHs(sv); /* won't clobber stack_sp */ |
1394 | } |
463ee0b2 |
1395 | if (dovalues) { |
f54cb97a |
1396 | SV *tmpstr; |
463ee0b2 |
1397 | PUTBACK; |
6d822dc4 |
1398 | tmpstr = hv_iterval(hv,entry); |
cea2e8a9 |
1399 | DEBUG_H(Perl_sv_setpvf(aTHX_ tmpstr, "%lu%%%d=%lu", |
46fc3d4c |
1400 | (unsigned long)HeHASH(entry), |
cdd3ba14 |
1401 | (int)HvMAX(keys)+1, |
800e9ae0 |
1402 | (unsigned long)(HeHASH(entry) & HvMAX(keys)))); |
463ee0b2 |
1403 | SPAGAIN; |
46fc3d4c |
1404 | XPUSHs(tmpstr); |
463ee0b2 |
1405 | } |
1406 | PUTBACK; |
1407 | } |
1408 | return NORMAL; |
1409 | } |
4e35701f |
1410 | |
af3babe4 |
1411 | /* |
1412 | * Local variables: |
1413 | * c-indentation-style: bsd |
1414 | * c-basic-offset: 4 |
1415 | * indent-tabs-mode: t |
1416 | * End: |
1417 | * |
37442d52 |
1418 | * ex: set ts=8 sts=4 sw=4 noet: |
1419 | */ |