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

string plus length typemap hack

by patcat88 (Deacon)
on Oct 29, 2011 at 07:38 UTC ( [id://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 (Patriarch) 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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-03-29 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found