[php-src] Php扩展开发的琐碎注意点、细节

内容均以php-5.6.14为例.

函数中接收的字符串参数长度不包含结尾的0,在 zend_update_property 中,长度的参数是 int len,一般都使用 ZEND_STRL(NAME)自动填充字符串和长度,它的长度实现是 sizeof(NAME) - 1,它也不需要结尾的0;

#undef MIN
#undef MAX
#define MAX(a, b)  (((a)>(b))?(a):(b))
#define MIN(a, b)  (((a)<(b))?(a):(b))
#define ZEND_STRL(str)      (str), (sizeof(str)-1)
#define ZEND_STRS(str)      (str), (sizeof(str))
#define ZEND_NORMALIZE_BOOL(n)          \
    ((n) ? (((n)>0) ? 1 : -1) : 0)
#define ZEND_TRUTH(x)       ((x) ? 1 : 0)
#define ZEND_LOG_XOR(a, b)      (ZEND_TRUTH(a) ^ ZEND_TRUTH(b))

而在 zend_hash_exists, zend_hash_find, add_assoc_stringl_ex 这些

zend_hash_*,add_assoc_* 系列函数的参数 uint key_len 在调用时,参数长度都需要 + 1.

具体表现为:

ZEND_API int zend_hash_exists(const HashTable *ht, const char *arKey, uint nKeyLength)

将只检测 nKeyLength 长度的字符,UNKNOW 将忽略掉 W。

ZEND_API int add_assoc_stringl_ex(zval *arg, const char *key, uint key_len, char *str, uint length, int duplicate)

将只添加 key_len 长度的键,TEST 将忽略掉 T。

 PHP Manual, API Function and Macro reference:

https://inst.eecs.berkeley.edu/php/main/zendapi-reference.html

Link:https://www.cnblogs.com/farwish/p/5701198.html

猜你喜欢

转载自www.cnblogs.com/farwish/p/5701198.html