Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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
[erix]: it could be interesting to know whether that 57M customer data breach at ueber was before or after the database switch
[erix]: or perhaps it is unrelated

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2017-11-22 14:01 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (324 votes). Check out past polls.