gpt4 book ai didi

perl - sv_catpv() 和 sv_catpvs() 有什么区别?

转载 作者:行者123 更新时间:2023-12-04 18:40:52 25 4
gpt4 key购买 nike

根据 perlapi , sv_catpv()工作原理如下:

Concatenates the NUL-terminated string onto the end of the string which is in the SV. If the SV has the UTF-8 status set, then the bytes appended should be valid UTF-8. Handles 'get' magic, but not 'set' magic.

void sv_catpv(SV *const sv, const char* ptr)


我发现的大多数 XS 教程都使用 sv_catpvs() ,虽然,这样做:

Like sv_catpvn, but takes a literal string instead of a string/length pair.

void sv_catpvs(SV* sv, const char* s)


嗯,这不是很有帮助,所以让我们看看 sv_catpvn() :

Concatenates the string onto the end of the string which is in the SV. The len indicates number of bytes to copy. If the SV has the UTF-8 status set, then the bytes appended should be valid UTF-8. Handles 'get' magic, but not 'set' magic.

void sv_catpvn(SV *dsv, const char *sstr, STRLEN len)


所以, sv_catpvnsv_catpv 做同样的事情除了它将字符串长度作为单独的参数,和 sv_catpvssv_catpvn 相同除了它需要文字字符串。 sv_catpv 之间有什么细微的差别吗?和 sv_catpvs我失踪了,还是他们只是做同一件事的两种方法?

最佳答案

根据您引用的段落,sv_catpvs只需要一个字符串文字。

const char *str = "foo";

sv_catpvs(sv, "foo"); // ok
sv_catpvs(sv, str); // ERROR
sv_catpv ,另一方面,接受任何返回字符串的表达式。
sv_catpv(sv, "foo");    // ok
sv_catpv(sv, str); // ok

那么为什么 sv_catpvs存在吗?因为它更快。原因 sv_catpvs只需要一个字符串文字是它是一个扩展的宏
sv_catpvs(sv, "foo")

变成类似的东西
sv_catpvn_flags(sv, "foo", sizeof("foo")-1, SV_GMAGIC)

这解决了
sv_catpvn_flags(sv, "foo", 3, SV_GMAGIC)

在编译时。 sv_catpv ,另一方面,被迫使用较慢的 strlen .

关于perl - sv_catpv() 和 sv_catpvs() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26637496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com