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 */
39 register I32 matches = 0;
40 register I32 squash = op->op_private & OPpTRANS_SQUASH;
45 tbl = (short*)cPVOP->op_pv;
46 s = (U8*)SvPV(sv, len);
50 s = (U8*)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]) >= 0) {
68 if ((ch = tbl[*s]) >= 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 - (U8*)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)
163 #endif /* HAS_QUAD */
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 */
194 #endif /* HAS_QUAD */
197 for (t++; t < send; t++) {
207 croak("Use of %c in printf format not supported", *t);
209 case '0': case '1': case '2': case '3': case '4':
210 case '5': case '6': case '7': case '8': case '9':
211 case '.': case '#': case '-': case '+': case ' ':
226 if (strEQ(f,"%c")) { /* some printfs fail on null chars */
232 (void)sprintf(xs,f,xlen);
244 (void)sprintf(buf,s,(Quad_t)SvNV(arg));
248 (void)sprintf(xs,f,(long)SvNV(arg));
250 (void)sprintf(xs,f,SvIV(arg));
256 case 'x': case 'o': case 'u':
262 (void)sprintf(buf,s,(unsigned Quad_t)value);
266 (void)sprintf(xs,f,U_L(value));
268 (void)sprintf(xs,f,U_I(value));
271 case 'E': case 'e': case 'f': case 'G': case 'g':
274 (void)sprintf(xs,f,SvNV(arg));
280 xs = SvPV(arg, arglen);
282 if (strEQ(f,"%s")) { /* some printfs fail on >128 chars */
283 break; /* so handle simple cases */
285 else if (f[1] == '-') {
286 char *mp = strchr(f, '.');
290 I32 max = atoi(mp+1);
299 else if (isDIGIT(f[1])) {
300 char *mp = strchr(f, '.');
304 I32 max = atoi(mp+1);
313 strcpy(tokenbuf+64,f); /* sprintf($s,...$s...) */
315 (void)sprintf(buf,tokenbuf+64,xs);
320 /* end of switch, copy results */
322 if (xs == buf && xlen >= sizeof(buf)) { /* Ooops! */
323 PerlIO_puts(PerlIO_stderr(),"panic: sprintf overflow - memory corrupted!\n");
326 SvGROW(sv, SvCUR(sv) + (f - s) + xlen + 1 + pre + post);
327 sv_catpvn(sv, s, f - s);
329 repeatcpy(SvPVX(sv) + SvCUR(sv), " ", 1, pre);
332 sv_catpvn(sv, xs, xlen);
334 repeatcpy(SvPVX(sv) + SvCUR(sv), " ", 1, post);
338 break; /* break from for loop */
341 sv_catpvn(sv, s, t - s);
349 SV *targ = LvTARG(sv);
352 register unsigned char *s;
353 register unsigned long lval;
360 s = (unsigned char*)SvPV_force(targ, targlen);
361 lval = U_L(SvNV(sv));
362 offset = LvTARGOFF(sv);
363 size = LvTARGLEN(sv);
365 len = (offset + size + 7) / 8;
367 s = (unsigned char*)SvGROW(targ, len + 1);
368 (void)memzero(s + targlen, len - targlen + 1);
369 SvCUR_set(targ, len);
373 mask = (1 << size) - 1;
377 s[offset] &= ~(mask << size);
378 s[offset] |= lval << size;
383 s[offset] = lval & 255;
384 else if (size == 16) {
385 s[offset] = (lval >> 8) & 255;
386 s[offset+1] = lval & 255;
388 else if (size == 32) {
389 s[offset] = (lval >> 24) & 255;
390 s[offset+1] = (lval >> 16) & 255;
391 s[offset+2] = (lval >> 8) & 255;
392 s[offset+3] = lval & 255;
405 if (SvTYPE(sv) == SVt_PVAV) {
410 for (i = 0; i <= max; i++) {
411 sv = (SV*)av_fetch(av, i, FALSE);
412 if (sv && ((sv = *(SV**)sv), sv != &sv_undef))
417 if (SvTYPE(sv) == SVt_PVHV) {
420 (void)hv_iterinit(hv);
422 while (entry = hv_iternext(hv))
423 do_chop(astr,hv_iterval(hv,entry));
427 if (len && !SvPOK(sv))
428 s = SvPV_force(sv, len);
431 sv_setpvn(astr, s, 1);
437 sv_setpvn(astr, "", 0);
452 if (SvTYPE(sv) == SVt_PVAV) {
457 for (i = 0; i <= max; i++) {
458 sv = (SV*)av_fetch(av, i, FALSE);
459 if (sv && ((sv = *(SV**)sv), sv != &sv_undef))
460 count += do_chomp(sv);
464 if (SvTYPE(sv) == SVt_PVHV) {
467 (void)hv_iterinit(hv);
469 while (entry = hv_iternext(hv))
470 count += do_chomp(hv_iterval(hv,entry));
474 if (len && !SvPOKp(sv))
475 s = SvPV_force(sv, len);
482 while (len && s[-1] == '\n') {
490 char *rsptr = SvPV(rs, rslen);
501 if (memcmp(s, rsptr, rslen))
516 do_vop(optype,sv,left,right)
530 register char *lc = SvPV(left, leftlen);
531 register char *rc = SvPV(right, rightlen);
537 dc = SvPV_force(sv,na);
538 len = leftlen < rightlen ? leftlen : rightlen;
540 if (SvCUR(sv) < len) {
541 dc = SvGROW(sv,len + 1);
542 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
545 (void)SvPOK_only(sv);
547 if (len >= sizeof(long)*4 &&
548 !((long)dc % sizeof(long)) &&
549 !((long)lc % sizeof(long)) &&
550 !((long)rc % sizeof(long))) /* It's almost always aligned... */
552 I32 remainder = len % (sizeof(long)*4);
553 len /= (sizeof(long)*4);
562 *dl++ = *ll++ & *rl++;
563 *dl++ = *ll++ & *rl++;
564 *dl++ = *ll++ & *rl++;
565 *dl++ = *ll++ & *rl++;
570 *dl++ = *ll++ ^ *rl++;
571 *dl++ = *ll++ ^ *rl++;
572 *dl++ = *ll++ ^ *rl++;
573 *dl++ = *ll++ ^ *rl++;
578 *dl++ = *ll++ | *rl++;
579 *dl++ = *ll++ | *rl++;
580 *dl++ = *ll++ | *rl++;
581 *dl++ = *ll++ | *rl++;
596 *dc++ = *lc++ & *rc++;
600 *dc++ = *lc++ ^ *rc++;
604 *dc++ = *lc++ | *rc++;
608 sv_catpvn(sv, rsave + len, rightlen - len);
609 else if (leftlen > len)
610 sv_catpvn(sv, lsave + len, leftlen - len);
626 I32 dokeys = (op->op_type == OP_KEYS);
627 I32 dovalues = (op->op_type == OP_VALUES);
629 if (op->op_type == OP_RV2HV || op->op_type == OP_PADHV)
630 dokeys = dovalues = TRUE;
635 (void)hv_iterinit(hv); /* always reset iterator regardless */
637 if (GIMME != G_ARRAY) {
641 if (!SvRMAGICAL(hv) || !mg_find((SV*)hv,'P'))
646 while (entry = hv_iternext(hv)) {
654 /* Guess how much room we need. hv_max may be a few too many. Oh well. */
655 EXTEND(sp, HvMAX(hv) * (dokeys + dovalues));
657 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
658 while (entry = hv_iternext(hv)) {
661 XPUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
663 tmpstr = NEWSV(45,0);
665 sv_setsv(tmpstr,hv_iterval(hv,entry));
668 sprintf(buf,"%d%%%d=%d\n", HeHASH(entry),
669 HvMAX(hv)+1, HeHASH(entry) & HvMAX(hv));
670 sv_setpv(tmpstr,buf);
672 XPUSHs(sv_2mortal(tmpstr));