计算c字符的长度,保证不超过2^30

计算字符串长度
本文介绍了一个来自SQLite3源码中的函数sqlite3Strlen30,该函数用于计算字符串长度,并确保计算结果不会超出32位有符号整数下30位所能表示的最大值。对于非常长的字符串(大于1GiB),返回的长度可能会小于实际长度。

来自sqlite3源码

/*
** Compute a string length that is limited to what can be stored in
** lower 30 bits of a 32-bit signed integer.
**
** The value returned will never be negative.  Nor will it ever be greater
** than the actual length of the string.  For very long strings (greater
** than 1GiB) the value returned might be less than the true string length.
*/
int sqlite3Strlen30(const char *z){
  if( z==0 ) return 0;
  return 0x3fffffff & (int)strlen(z);
}

转载于:https://www.cnblogs.com/huahuahu/p/ji-suanc-zi-fu-de-zhang-du-bao-zheng-bu-chao-guo23.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值