>>>>> "heyi" == heyi xiao <xiaohey...@yahoo.com> writes:

heyi> Hello all,

heyi> My linux system has a pre-installed perl. Is there a good way to check
heyi> the source code for builtin functions, like reverse etc. I want to check
heyi> the source code for better learning/understanding.

If you really think you can gain an understanding of how to better
use Perl's reverse by looking at this, then you are a far smarter
person than me:

    PP(pp_reverse)
    {
        dSP; dMARK;
        SV ** const oldsp = SP;

        if (GIMME == G_ARRAY) {
            MARK++;
            while (MARK < SP) {
                register SV * const tmp = *MARK;
                *MARK++ = *SP;
                *SP-- = tmp;
            }
            /* safe as long as stack cannot get extended in the above */
            SP = oldsp;
        }
        else {
            register char *up;
            register char *down;
            register I32 tmp;
            dTARGET;
            STRLEN len;

            SvUTF8_off(TARG);                           /* decontaminate */
            if (SP - MARK > 1)
                do_join(TARG, &PL_sv_no, MARK, SP);
            else
                sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
            up = SvPV_force(TARG, len);
            if (len > 1) {
                if (DO_UTF8(TARG)) {    /* first reverse each character */
                    U8* s = (U8*)SvPVX(TARG);
                    const U8* send = (U8*)(s + len);
                    while (s < send) {
                        if (UTF8_IS_INVARIANT(*s)) {
                            s++;
                            continue;
                        }
                        else {
                            if (!utf8_to_uvchr(s, 0))
                                break;
                            up = (char*)s;
                            s += UTF8SKIP(s);
                            down = (char*)(s - 1);
                            /* reverse this character */
                            while (down > up) {
                                tmp = *up;
                                *up++ = *down;
                                *down-- = (char)tmp;
                            }
                        }
                    }
                    up = SvPVX(TARG);
                }
                down = SvPVX(TARG) + len - 1;
                while (down > up) {
                    tmp = *up;
                    *up++ = *down;
                    *down-- = (char)tmp;
                }
                (void)SvPOK_only_UTF8(TARG);
            }
            SP = MARK + 1;
            SETTARG;
        }
        RETURN;
    }

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to