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