Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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

Replies are listed 'Best First'.
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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://934562]
Approved by Corion
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2018-02-20 15:49 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (271 votes). Check out past polls.