3 * Copyright (c) 1991-1994, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "'So that was the job I felt I had to do when I started,' thought Sam."
17 #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
22 #pragma function(memcmp)
23 #endif /* BUGGY_MSC */
26 #pragma intrinsic(memcmp)
27 #endif /* BUGGY_MSC */
36 register I32 matches = 0;
40 register I32 squash = op->op_private & OPpTRANS_SQUASH;
45 tbl = (short*) cPVOP->op_pv;
50 s = SvPV_force(sv, len);
54 croak("panic: do_trans");
55 DEBUG_t( deb("2.TBL\n"));
56 if (!op->op_private) {
58 if ((ch = tbl[*s & 0377]) >= 0) {
68 if ((ch = tbl[*s & 0377]) >= 0) {
70 if (matches++ && squash) {
79 else if (ch == -1) /* -1 is unmapped character */
80 *d++ = *s; /* -2 is delete character */
83 matches += send - d; /* account for disappeared chars */
85 SvCUR_set(sv, d - SvPVX(sv));
92 do_join(sv,del,mark,sp)
99 register I32 items = sp - mark;
102 register char *delim = SvPV(del, delimlen);
106 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
107 if (SvTYPE(sv) < SVt_PV)
108 sv_upgrade(sv, SVt_PV);
109 if (SvLEN(sv) < len + items) { /* current length is way too short */
110 while (items-- > 0) {
117 SvGROW(sv, len + 1); /* so try to pre-extend */
128 s = SvPV(*mark, tmplen);
129 sv_setpvn(sv, s, tmplen);
139 for (; items > 0; items--,mark++) {
140 sv_catpvn(sv,delim,len);
145 for (; items > 0; items--,mark++)
152 do_sprintf(sv,len,sarg)
175 len--; /* don't count pattern string */
176 t = s = SvPV(*sarg, arglen); /* XXX Don't know t is writeable */
182 if (len <= 0 || !(arg = *sarg++))
186 for ( ; t < send && *t != '%'; t++) ;
188 break; /* end of run_format string, ignore extra args */
197 for (t++; t < send; t++) {
206 case '0': case '1': case '2': case '3': case '4':
207 case '5': case '6': case '7': case '8': case '9':
208 case '.': case '#': case '-': case '+': case ' ':
223 if (strEQ(f,"%c")) { /* some printfs fail on null chars */
229 (void)sprintf(xs,f,xlen);
241 (void)sprintf(buf,s,(quad)SvNV(arg));
245 (void)sprintf(xs,f,(long)SvNV(arg));
247 (void)sprintf(xs,f,SvIV(arg));
253 case 'x': case 'o': case 'u':
259 (void)sprintf(buf,s,(unsigned quad)value);
263 (void)sprintf(xs,f,U_L(value));
265 (void)sprintf(xs,f,U_I(value));
268 case 'E': case 'e': case 'f': case 'G': case 'g':
271 (void)sprintf(xs,f,SvNV(arg));
277 xs = SvPV(arg, arglen);
279 if (strEQ(f,"%s")) { /* some printfs fail on >128 chars */
280 break; /* so handle simple cases */
282 else if (f[1] == '-') {
283 char *mp = strchr(f, '.');
287 I32 max = atoi(mp+1);
296 else if (isDIGIT(f[1])) {
297 char *mp = strchr(f, '.');
301 I32 max = atoi(mp+1);
310 strcpy(tokenbuf+64,f); /* sprintf($s,...$s...) */
312 (void)sprintf(buf,tokenbuf+64,xs);
317 /* end of switch, copy results */
319 SvGROW(sv, SvCUR(sv) + (f - s) + xlen + 1 + pre + post);
320 sv_catpvn(sv, s, f - s);
322 repeatcpy(SvPVX(sv) + SvCUR(sv), " ", 1, pre);
325 sv_catpvn(sv, xs, xlen);
327 repeatcpy(SvPVX(sv) + SvCUR(sv), " ", 1, post);
331 break; /* break from for loop */
334 sv_catpvn(sv, s, t - s);
342 SV *targ = LvTARG(sv);
345 register unsigned char *s;
346 register unsigned long lval;
353 s = (unsigned char*)SvPV_force(targ, targlen);
354 lval = U_L(SvNV(sv));
355 offset = LvTARGOFF(sv);
356 size = LvTARGLEN(sv);
358 len = (offset + size + 7) / 8;
360 s = (unsigned char*)SvGROW(targ, len + 1);
361 (void)memzero(s + targlen, len - targlen + 1);
362 SvCUR_set(targ, len);
366 mask = (1 << size) - 1;
370 s[offset] &= ~(mask << size);
371 s[offset] |= lval << size;
376 s[offset] = lval & 255;
377 else if (size == 16) {
378 s[offset] = (lval >> 8) & 255;
379 s[offset+1] = lval & 255;
381 else if (size == 32) {
382 s[offset] = (lval >> 24) & 255;
383 s[offset+1] = (lval >> 16) & 255;
384 s[offset+2] = (lval >> 8) & 255;
385 s[offset+3] = lval & 255;
398 if (SvTYPE(sv) == SVt_PVAV) {
403 for (i = 0; i <= max; i++) {
404 sv = (SV*)av_fetch(av, i, FALSE);
405 if (sv && ((sv = *(SV**)sv), sv != &sv_undef))
410 if (SvTYPE(sv) == SVt_PVHV) {
413 (void)hv_iterinit(hv);
415 while (entry = hv_iternext(hv))
416 do_chop(astr,hv_iterval(hv,entry));
420 if (len && !SvPOKp(sv))
421 s = SvPV_force(sv, len);
424 sv_setpvn(astr, s, 1);
430 sv_setpvn(astr, "", 0);
438 register I32 count = 0;
442 if (SvTYPE(sv) == SVt_PVAV) {
447 for (i = 0; i <= max; i++) {
448 sv = (SV*)av_fetch(av, i, FALSE);
449 if (sv && ((sv = *(SV**)sv), sv != &sv_undef))
450 count += do_chomp(sv);
454 if (SvTYPE(sv) == SVt_PVHV) {
457 (void)hv_iterinit(hv);
459 while (entry = hv_iternext(hv))
460 count += do_chomp(hv_iterval(hv,entry));
464 if (len && !SvPOKp(sv))
465 s = SvPV_force(sv, len);
472 while (len && s[-1] == '\n') {
478 else if (rslen == 1) {
488 if (bcmp(s, rs, rslen))
503 do_vop(optype,sv,left,right)
517 register char *lc = SvPV(left, leftlen);
518 register char *rc = SvPV(right, rightlen);
522 dc = SvPV_force(sv,na);
523 len = leftlen < rightlen ? leftlen : rightlen;
525 if (SvCUR(sv) < len) {
526 dc = SvGROW(sv,len + 1);
527 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
530 (void)SvPOK_only(sv);
532 if (len >= sizeof(long)*4 &&
533 !((long)dc % sizeof(long)) &&
534 !((long)lc % sizeof(long)) &&
535 !((long)rc % sizeof(long))) /* It's almost always aligned... */
537 I32 remainder = len % (sizeof(long)*4);
538 len /= (sizeof(long)*4);
547 *dl++ = *ll++ & *rl++;
548 *dl++ = *ll++ & *rl++;
549 *dl++ = *ll++ & *rl++;
550 *dl++ = *ll++ & *rl++;
555 *dl++ = *ll++ ^ *rl++;
556 *dl++ = *ll++ ^ *rl++;
557 *dl++ = *ll++ ^ *rl++;
558 *dl++ = *ll++ ^ *rl++;
563 *dl++ = *ll++ | *rl++;
564 *dl++ = *ll++ | *rl++;
565 *dl++ = *ll++ | *rl++;
566 *dl++ = *ll++ | *rl++;
584 *dc++ = *lc++ & *rc++;
588 *dc++ = *lc++ ^ *rc++;
592 *dc++ = *lc++ | *rc++;
596 sv_catpvn(sv, rsave + len, rightlen - len);
597 else if (leftlen > len)
598 sv_catpvn(sv, lsave + len, leftlen - len);
614 I32 dokeys = (op->op_type == OP_KEYS);
615 I32 dovalues = (op->op_type == OP_VALUES);
617 if (op->op_type == OP_RV2HV || op->op_type == OP_PADHV)
618 dokeys = dovalues = TRUE;
622 if (GIMME != G_ARRAY) {
625 if (!SvRMAGICAL(hv) || !mg_find((SV*)hv,'P'))
629 (void)hv_iterinit(hv);
631 while (entry = hv_iternext(hv)) {
639 /* Guess how much room we need. hv_max may be a few too many. Oh well. */
640 EXTEND(sp, HvMAX(hv) * (dokeys + dovalues));
642 (void)hv_iterinit(hv);
644 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
645 while (entry = hv_iternext(hv)) {
648 tmps = hv_iterkey(entry,&i); /* won't clobber stack_sp */
651 XPUSHs(sv_2mortal(newSVpv(tmps,i)));
654 tmpstr = NEWSV(45,0);
656 sv_setsv(tmpstr,hv_iterval(hv,entry));
659 sprintf(buf,"%d%%%d=%d\n",entry->hent_hash,
660 HvMAX(hv)+1,entry->hent_hash & HvMAX(hv));
661 sv_setpv(tmpstr,buf);
663 XPUSHs(sv_2mortal(tmpstr));