Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

string plus length typemap hack

by patcat88 (Deacon)
on Oct 29, 2011 at 07:38 UTC ( #934562=perlmeditation: print w/ replies, xml ) Need Help??

Doesn't it suck that if you want a string pointer, and its length, in XS, you need to get the SV * and do the SvPV manually each time? Fear no more. Below typemap will give you the char * pointer, and the length. Always.
void Foo( str ) char * str PPCODE: Foo(str, strLen);
Change the typemap entry to your liking. Also note caching SV * to avoid numerous ST macros in SvPV macro. Who wants to calculate the same thing half a dozen times? Also less noise in your debugger with the now useless SV * being gone.
T_STR_AND_LEN ".(unshift(@line, ( 'PREINIT:', ' unsigned long '.$var.'Len;', 'INPUT:')),''). "{ SV * TmpSV = $arg; $var = ($type) SvPV( TmpSV , ${\$var}Len); };
Warning, you must use PREINIT for your XSUBs. Lazy coders shall die.

edited: spelling

Comment on string plus length typemap hack
Select or Download Code
Re: string plus length typemap hack
by ikegami (Pope) on Oct 29, 2011 at 07:55 UTC

    The length is a STRLEN, not an unsigned long.

    There's also the matter that it's useless to use SvPV without using SvUTF8 too, so you might want to to return that too. Either that or use one of SvPVbyte or SvPVutf8 instead or SvPV.

Reaped: Re: string plus length typemap hack
by NodeReaper (Curate) on Oct 29, 2011 at 13:00 UTC
Reaped: Re: string plus length typemap hack
by NodeReaper (Curate) on Oct 30, 2011 at 13:17 UTC
Reaped: Re: string plus length typemap hack
by NodeReaper (Curate) on Nov 01, 2011 at 13:23 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://934562]
Approved by Corion
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-07-28 22:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (210 votes), past polls