How to pass the length of a string argument to a CODE section in XS?

In XS, I can pass the length of the string argument to the C function with the keyword:

static int foo(const char *s, size_t len)
{
    return 1;
}

MODULE = Foo        PACKAGE = Foo

void
foo(char *s, STRLEN length(s))

However, how can I get the length of a string if I need it inside a CODE block?

void
foo(char *s, STRLEN length(s))
CODE:
    ...
    foo(s, ???);

I can use the variable STRLEN_length_of_sor XSauto_length_of_s, auto-generated by xsubpp, but it's a bit hard-coded. Is there a way (possibly a predefined macro) that I can use to get the variable name? Or can I assign my name to the length argument? Or do I need to resort to declaring the argument as SV *, and then get the length of c SvPVin the CODE section?

+4
source share
1 answer

, char* XS ( ), .

void
foo(SV* sv)
PREINIT:
    STRLEN len;
    char* s;
CODE:
    s = SvPVbyte(sv, len);
    foo(s, len);

void
foo(SV* sv)
PREINIT:
    STRLEN len;
    char* s;
CODE:
    s = SvPVutf8(sv, len);
    foo(s, len);

, Perl 32- 64- , C - 8- . [1] , , .

char s.

s ​​ Perl, utf8.


  • , char 8 , , , perl.
+2

Source: https://habr.com/ru/post/1694598/


All Articles