3 * Copyright (c) 1991-1997, 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 do_trans(SV *sv, OP *arg)
30 register I32 matches = 0;
31 register I32 squash = op->op_private & OPpTRANS_SQUASH;
34 if (SvREADONLY(sv) && !(op->op_private & OPpTRANS_COUNTONLY))
36 tbl = (short*)cPVOP->op_pv;
37 s = (U8*)SvPV(sv, len);
41 s = (U8*)SvPV_force(sv, len);
45 croak("panic: do_trans");
46 DEBUG_t( deb("2.TBL\n"));
47 if (!op->op_private) {
49 if ((ch = tbl[*s]) >= 0) {
57 else if (op->op_private & OPpTRANS_COUNTONLY) {
67 if ((ch = tbl[*s]) >= 0) {
69 if (matches++ && squash) {
78 else if (ch == -1) /* -1 is unmapped character */
79 *d++ = *s; /* -2 is delete character */
82 matches += send - d; /* account for disappeared chars */
84 SvCUR_set(sv, d - (U8*)SvPVX(sv));
91 do_join(register SV *sv, SV *del, register SV **mark, register SV **sp)
94 register I32 items = sp - mark;
97 register char *delim = SvPV(del, delimlen);
101 len = (items > 0 ? (delimlen * (items - 1) ) : 0);
102 if (SvTYPE(sv) < SVt_PV)
103 sv_upgrade(sv, SVt_PV);
104 if (SvLEN(sv) < len + items) { /* current length is way too short */
105 while (items-- > 0) {
112 SvGROW(sv, len + 1); /* so try to pre-extend */
123 s = SvPV(*mark, tmplen);
124 sv_setpvn(sv, s, tmplen);
134 for (; items > 0; items--,mark++) {
135 sv_catpvn(sv,delim,len);
140 for (; items > 0; items--,mark++)
147 do_sprintf(SV *sv, I32 len, SV **sarg)
150 char *pat = SvPV(*sarg, patlen);
151 bool do_taint = FALSE;
153 sv_vsetpvfn(sv, pat, patlen, Null(va_list*), sarg + 1, len - 1, &do_taint);
162 SV *targ = LvTARG(sv);
165 register unsigned char *s;
166 register unsigned long lval;
173 s = (unsigned char*)SvPV_force(targ, targlen);
174 lval = U_L(SvNV(sv));
175 offset = LvTARGOFF(sv);
176 size = LvTARGLEN(sv);
178 len = (offset + size + 7) / 8;
180 s = (unsigned char*)SvGROW(targ, len + 1);
181 (void)memzero(s + targlen, len - targlen + 1);
182 SvCUR_set(targ, len);
186 mask = (1 << size) - 1;
190 s[offset] &= ~(mask << size);
191 s[offset] |= lval << size;
196 s[offset] = lval & 255;
197 else if (size == 16) {
198 s[offset] = (lval >> 8) & 255;
199 s[offset+1] = lval & 255;
201 else if (size == 32) {
202 s[offset] = (lval >> 24) & 255;
203 s[offset+1] = (lval >> 16) & 255;
204 s[offset+2] = (lval >> 8) & 255;
205 s[offset+3] = lval & 255;
211 do_chop(register SV *astr, register SV *sv)
216 if (SvTYPE(sv) == SVt_PVAV) {
221 for (i = 0; i <= max; i++) {
222 sv = (SV*)av_fetch(av, i, FALSE);
223 if (sv && ((sv = *(SV**)sv), sv != &sv_undef))
228 if (SvTYPE(sv) == SVt_PVHV) {
231 (void)hv_iterinit(hv);
233 while (entry = hv_iternext(hv))
234 do_chop(astr,hv_iterval(hv,entry));
238 if (len && !SvPOK(sv))
239 s = SvPV_force(sv, len);
242 sv_setpvn(astr, s, 1);
248 sv_setpvn(astr, "", 0);
253 do_chomp(register SV *sv)
263 if (SvTYPE(sv) == SVt_PVAV) {
268 for (i = 0; i <= max; i++) {
269 sv = (SV*)av_fetch(av, i, FALSE);
270 if (sv && ((sv = *(SV**)sv), sv != &sv_undef))
271 count += do_chomp(sv);
275 if (SvTYPE(sv) == SVt_PVHV) {
278 (void)hv_iterinit(hv);
280 while (entry = hv_iternext(hv))
281 count += do_chomp(hv_iterval(hv,entry));
285 if (len && !SvPOKp(sv))
286 s = SvPV_force(sv, len);
293 while (len && s[-1] == '\n') {
301 char *rsptr = SvPV(rs, rslen);
312 if (memNE(s, rsptr, rslen))
327 do_vop(I32 optype, SV *sv, SV *left, SV *right)
329 dTHR; /* just for taint */
345 if (sv != left || (optype != OP_BIT_AND && !SvOK(sv) && !SvGMAGICAL(sv)))
346 sv_setpvn(sv, "", 0); /* avoid undef warning on |= and ^= */
347 lsave = lc = SvPV(left, leftlen);
348 rsave = rc = SvPV(right, rightlen);
349 len = leftlen < rightlen ? leftlen : rightlen;
351 if (SvOK(sv) || SvTYPE(sv) > SVt_PVMG) {
352 dc = SvPV_force(sv, na);
353 if (SvCUR(sv) < len) {
354 dc = SvGROW(sv, len + 1);
355 (void)memzero(dc + SvCUR(sv), len - SvCUR(sv) + 1);
359 I32 needlen = ((optype == OP_BIT_AND)
360 ? len : (leftlen > rightlen ? leftlen : rightlen));
361 Newz(801, dc, needlen + 1, char);
362 (void)sv_usepvn(sv, dc, needlen);
363 dc = SvPVX(sv); /* sv_usepvn() calls Renew() */
366 (void)SvPOK_only(sv);
368 if (len >= sizeof(long)*4 &&
369 !((long)dc % sizeof(long)) &&
370 !((long)lc % sizeof(long)) &&
371 !((long)rc % sizeof(long))) /* It's almost always aligned... */
373 I32 remainder = len % (sizeof(long)*4);
374 len /= (sizeof(long)*4);
383 *dl++ = *ll++ & *rl++;
384 *dl++ = *ll++ & *rl++;
385 *dl++ = *ll++ & *rl++;
386 *dl++ = *ll++ & *rl++;
391 *dl++ = *ll++ ^ *rl++;
392 *dl++ = *ll++ ^ *rl++;
393 *dl++ = *ll++ ^ *rl++;
394 *dl++ = *ll++ ^ *rl++;
399 *dl++ = *ll++ | *rl++;
400 *dl++ = *ll++ | *rl++;
401 *dl++ = *ll++ | *rl++;
402 *dl++ = *ll++ | *rl++;
417 *dc++ = *lc++ & *rc++;
421 *dc++ = *lc++ ^ *rc++;
425 *dc++ = *lc++ | *rc++;
429 sv_catpvn(sv, rsave + len, rightlen - len);
430 else if (leftlen > len)
431 sv_catpvn(sv, lsave + len, leftlen - len);
448 I32 dokeys = (op->op_type == OP_KEYS);
449 I32 dovalues = (op->op_type == OP_VALUES);
450 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
452 if (op->op_type == OP_RV2HV || op->op_type == OP_PADHV)
453 dokeys = dovalues = TRUE;
456 if (op->op_flags & OPf_MOD) { /* lvalue */
457 dTARGET; /* make sure to clear its target here */
458 if (SvTYPE(TARG) == SVt_PVLV)
459 LvTARG(TARG) = Nullsv;
466 (void)hv_iterinit(hv); /* always reset iterator regardless */
468 (void)avhv_iterinit((AV*)hv);
473 if (gimme == G_SCALAR) {
477 if (op->op_flags & OPf_MOD) { /* lvalue */
478 if (SvTYPE(TARG) < SVt_PVLV) {
479 sv_upgrade(TARG, SVt_PVLV);
480 sv_magic(TARG, Nullsv, 'k', Nullch, 0);
483 LvTARG(TARG) = (SV*)hv;
488 if (!SvRMAGICAL(hv) || !mg_find((SV*)hv,'P'))
493 while (entry = realhv ? hv_iternext(hv) : avhv_iternext((AV*)hv)) {
501 /* Guess how much room we need. hv_max may be a few too many. Oh well. */
502 EXTEND(sp, HvMAX(hv) * (dokeys + dovalues));
504 PUTBACK; /* hv_iternext and hv_iterval might clobber stack_sp */
505 while (entry = realhv ? hv_iternext(hv) : avhv_iternext((AV*)hv)) {
508 XPUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
510 tmpstr = sv_newmortal();
512 sv_setsv(tmpstr,realhv ?
513 hv_iterval(hv,entry) : avhv_iterval((AV*)hv,entry));
514 DEBUG_H(sv_setpvf(tmpstr, "%lu%%%d=%lu",
515 (unsigned long)HeHASH(entry),
517 (unsigned long)(HeHASH(entry) & HvMAX(hv))));