in reply to Re: XS Prepending space to an SVs PV
in thread XS Prepending space to an SVs PV

Getting and setting the length isn't the problem, especially as it is normally done for you. The problem is manipulating the combination of the 3 fields so that although the reality is

malloc'd space [ | | | |s|o|m|e| |d|a|t|a| |h|e|r|e|\0] ^ SV--->PVX---------------| CUR-------------->|..........................| = 15 LEN------|.....................................| = 19

which is the reverse situation to the normal thing whereby the extra space is at the end rather than the beginning; the rest of the codebase will respect these settings.

I've tried setting it up like this, but I get traps. I need to know whether I am setting it up wrong, or whether the rest of the codebase will always assume that CUR and LEN will start at the same place (ie. The same place pointed at by PVX)?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^3: XS Prepending space to an SVs PV
by vkon (Curate) on Apr 26, 2006 at 16:20 UTC
    This - famous Perl guts illustrated, and $5, will make you a coffee.

    I suspect you get traps because you substitute Perl's allocated memory with your own?

    Allocating more place than needed for an SV is why SvGROW exists:

    Although Perl will automatically grow strings for you, if you need +to force Perl to allocate more memory for your SV, you can use the mac +ro SvGROW(SV*, STRLEN newlen)
    Then, reading further in perlguts gives all your required manipulations:
    Offsets Perl provides the function "sv_chop" to efficiently remove charact +ers from the beginning of a string; you give it an SV and a pointer to somewhere inside the PV, and it discards everything before the poi +nter. The efficiency comes by means of a little hack: instead of actuall +y removing the characters, "sv_chop" sets the flag "OOK" (offset OK) + to signal to other functions that the offset hack is in effect, and i +t puts the number of bytes chopped off into the IV field of the SV. It th +en moves the PV pointer (called "SvPVX") forward that many bytes, and adjusts "SvCUR" and "SvLEN".