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