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

2 篇文章 0 订阅
2 篇文章 0 订阅

在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源代码:

// 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);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值