Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
what's the issue with opcodes? what problem(s) are you trying to solve?

It's not really about "solving a problem"; more, 'providing some entertainment with the possible side effect of producing something useful'; and, perhaps, widening the pool of people with internals skills.

The perl opcodes are very clearly defined set of essentially stand-alone functions with a gazzilion existing tests. If their textual descriptions aren't yet readily available, they should be relatively easy to derive from the existing code.

But, why not throw one problem up here and test market the concept? ;)

I'm not sure this is the best way to go about this; but here goes. This is the latest relased version of substr:

PP(pp_substr) { dSP; dTARGET; SV *sv; STRLEN curlen; STRLEN utf8_curlen; SV * pos_sv; IV pos1_iv; int pos1_is_uv; SV * len_sv; IV len_iv = 0; int len_is_uv = 0; I32 lvalue = PL_op->op_flags & OPf_MOD || LVRET; const bool rvalue = (GIMME_V != G_VOID); const char *tmps; SV *repl_sv = NULL; const char *repl = NULL; STRLEN repl_len; int num_args = PL_op->op_private & 7; bool repl_need_utf8_upgrade = FALSE; if (num_args > 2) { if (num_args > 3) { if(!(repl_sv = POPs)) num_args--; } if ((len_sv = POPs)) { len_iv = SvIV(len_sv); len_is_uv = len_iv ? SvIOK_UV(len_sv) : 1; } else num_args--; } pos_sv = POPs; pos1_iv = SvIV(pos_sv); pos1_is_uv = SvIOK_UV(pos_sv); sv = POPs; if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) { assert(!repl_sv); repl_sv = POPs; } if (lvalue && !repl_sv) { SV * ret; ret = sv_2mortal(newSV_type(SVt_PVLV)); /* Not TARG RT#67838 */ sv_magic(ret, NULL, PERL_MAGIC_substr, NULL, 0); LvTYPE(ret) = 'x'; LvTARG(ret) = SvREFCNT_inc_simple(sv); LvTARGOFF(ret) = pos1_is_uv || pos1_iv >= 0 ? (STRLEN)(UV)pos1_iv : (LvFLAGS(ret) |= 1, (STRLEN)(UV)-pos1_iv); LvTARGLEN(ret) = len_is_uv || len_iv > 0 ? (STRLEN)(UV)len_iv : (LvFLAGS(ret) |= 2, (STRLEN)(UV)-len_iv); PUSHs(ret); /* avoid SvSETMAGIC here */ RETURN; } if (repl_sv) { repl = SvPV_const(repl_sv, repl_len); SvGETMAGIC(sv); if (SvROK(sv)) Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "Attempt to use reference as lvalue in substr" ); tmps = SvPV_force_nomg(sv, curlen); if (DO_UTF8(repl_sv) && repl_len) { if (!DO_UTF8(sv)) { sv_utf8_upgrade_nomg(sv); curlen = SvCUR(sv); } } else if (DO_UTF8(sv)) repl_need_utf8_upgrade = TRUE; } else tmps = SvPV_const(sv, curlen); if (DO_UTF8(sv)) { utf8_curlen = sv_or_pv_len_utf8(sv, tmps, curlen); if (utf8_curlen == curlen) utf8_curlen = 0; else curlen = utf8_curlen; } else utf8_curlen = 0; { STRLEN pos, len, byte_len, byte_pos; if (!translate_substr_offsets( curlen, pos1_iv, pos1_is_uv, len_iv, len_is_uv, &pos, &len )) goto bound_fail; byte_len = len; byte_pos = utf8_curlen ? sv_or_pv_pos_u2b(sv, tmps, pos, &byte_len) : pos; tmps += byte_pos; if (rvalue) { SvTAINTED_off(TARG); /* decontaminate */ SvUTF8_off(TARG); /* decontaminate */ sv_setpvn(TARG, tmps, byte_len); #ifdef USE_LOCALE_COLLATE sv_unmagic(TARG, PERL_MAGIC_collxfrm); #endif if (utf8_curlen) SvUTF8_on(TARG); } if (repl) { SV* repl_sv_copy = NULL; if (repl_need_utf8_upgrade) { repl_sv_copy = newSVsv(repl_sv); sv_utf8_upgrade(repl_sv_copy); repl = SvPV_const(repl_sv_copy, repl_len); } if (!SvOK(sv)) SvPVCLEAR(sv); sv_insert_flags(sv, byte_pos, byte_len, repl, repl_len, 0); SvREFCNT_dec(repl_sv_copy); } } if (PL_op->op_private & OPpSUBSTR_REPL_FIRST) SP++; else if (rvalue) { SvSETMAGIC(TARG); PUSHs(TARG); } RETURN; bound_fail: if (repl) Perl_croak(aTHX_ "substr outside of string"); Perl_ck_warner(aTHX_ packWARN(WARN_SUBSTR), "substr outside of str +ing"); RETPUSHUNDEF; }

Can it be improved?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^2: (Placeholder) Imagine! by BrowserUk
in thread (Placeholder) Imagine! by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-19 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found