前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >#微码分享#C++变参字符串格式化函数format_string

#微码分享#C++变参字符串格式化函数format_string

作者头像
一见
发布2019-03-14 15:10:43
1.2K0
发布2019-03-14 15:10:43
举报
文章被收录于专栏:蓝天蓝天

在C和C++中,变参格式化函数虽然非类型安全,但却十分便利,因为得到广泛使用。对于常见的size_t类型要用“%zu”,ssize_t用”%zd“,int64_t用“% ”PRId64,uint64_t用“% ”PRIu64,long用"%ld",long long用"%lld",示例: const int64_t datetime = INT64_C(20190124144930); printf("datetime: %" PRId64"\n", datetime); 注意在PRId64前保留一个空格,以避免编译警告 format_string源代码链接: https://github.com/eyjian/r3c/blob/master/utils.cpp https://github.com/eyjian/libmooon/blob/master/src/utils/string_utils.cpp format_string源代码:

代码语言:javascript
复制
 // snprintf()第2个参数的大小,要求包含结尾符'\0'
  
 // snprintf()的返回值是期望大小,不包含结尾符'\0',
 
 // 下面假设snprintf()的第二个参数值为10,则:
 
 // 1) 当str为"abc"时,它的返回值的大小是3,"abc"的字符个数刚好是3;
 
 // 2) 当str为"1234567890"时,它的返回值大小是10,"1234567890"的字符个数刚好是10;
 
 // 3) 当str为"1234567890X"时,它的返回值大小是11,"1234567890X"的字符个数刚好是11。
 
 //
 
 // int asprintf(char **strp, const char *fmt, ...);
 
 			std::string format_string(const char* format, ...)
 
 {
 
 			    size_t size = 4096;
 
 			    std::string buffer(size, '\0');
 
 			    char* buffer_p = const_cast<char*>(buffer.data());
 
 int expected = 0;
 
 			    va_list ap;
 
 
 
 while (true)
 
 {
 
 			        va_start(ap, format);
 
 			        expected = vsnprintf(buffer_p, size, format, ap);
 
 
 
 			        va_end(ap);
 
 if (expected>-1 && expected<=static_cast<int>(size))
 
 {
 
 			            break;
 
 }
 
 else
 
 {
 
 /* Else try again with more space. */
 
 if (expected > -1) /* glibc 2.1 */
 
 			                size = static_cast<size_t>(expected + 1); /* precisely what is needed */
 
 else /* glibc 2.0 */
 
 			                size *= 2; /* twice the old size */
 
 
 
 			            buffer.resize(size);
 
 			            buffer_p = const_cast<char*>(buffer.data());
 
 }
 
 }
 
 
 
 // expected不包含字符串结尾符号,其值等于:strlen(buffer_p)
 
 			    return std::string(buffer_p, expected>0?expected:0);
 
 } 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-01-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档