Front page | perl.beginners |
Postings from August 2009
Re: source code for builtin functions
Thread Previous
|
Thread Next
From:
heyi xiao
Date:
August 27, 2009 19:29
Subject:
Re: source code for builtin functions
Message ID:
220299.94479.qm@web62207.mail.re1.yahoo.com
Thanks a lot, guys,
for your help and the hot, informative discussion! I really got better idea at where/how
to look perl source code. Although I may not fully understand the C code itself.
Heyi
--- On Thu, 8/27/09, Randal L. Schwartz <merlyn@stonehenge.com> wrote:
From: Randal L. Schwartz <merlyn@stonehenge.com>
Subject: Re: source code for builtin functions
To: beginners@perl.org
Date: Thursday, August 27, 2009, 9:01 PM
>>>>> "heyi" == heyi xiao <xiaoheyiyh@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
<merlyn@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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Thread Previous
|
Thread Next