c_cpp 字符串缓冲区

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 字符串缓冲区相关的知识,希望对你有一定的参考价值。

#include <array>
#include <cassert>
#include <stdarg.h> // va_list
#define WIN32_LEAN_AND_MEAN
#include <windows.h> // _vsnprintf_s

// just to show that there's not a single heap allocation involved in this code...
void * operator new (size_t) { throw "Unexpected!"; }
void operator delete (void *) { throw "Unexpected!"; }

/// A fixed-size string buffer (typically stack-allocated).
template <size_t N>
class String_buf
{
public:

    /// Constructs from a C string if provided, or sets to "" if nullptr passed.
    String_buf(const char *cstr = nullptr)
    {
        static_assert(N > 0, "String_buf of zero size!");

        if(cstr)
        {
            assert(strlen(cstr) < N && "String_buf too small!");
            strcpy(_data.data(), cstr);
        }
        else
        {
            _data[0] = '\0';
        }
    }

    /// Constructs from a String_buf of a bigger or equal static size.
    template <size_t M>
    String_buf(const String_buf<M> &rhs)
    {
        static_assert(M <= N, "String_buf too small!");
        strcpy(_data.data(), rhs.cstr());
    }

    /// Copies from a C string if provided, or sets to "" if nullptr passed.
    String_buf & operator = (const char *cstr)
    {
        static_assert(N > 0, "String_buf of zero size!");

        if(cstr)
        {
            assert(strlen(cstr) < N && "String_buf too small!");
            strcpy(_data.data(), cstr);
        }
        else
        {
            _data[0] = '\0';
        }

        return *this;
    }

    /// Copies from a String_buf of a bigger or equal static size.
    template <size_t M>
    String_buf & operator = (const String_buf<M> &rhs)
    {
        static_assert(M <= N, "String_buf too small!");
        strcpy(_data.data(), rhs.cstr());
        return *this;
    }

    /// Returns a C string (always valid).
    const char * cstr() const { return _data.data(); }

    /// Formats a string in a good old printf style.
    void format(const char *format, ...)
    {
        va_list args;
        va_start(args, format);
        // if truncated, '\0' is automatically appended
        _vsnprintf_s(_data.data(), N, _TRUNCATE, format, args);
        va_end(args);
    }

private:

    std::array<char, N> _data;
};

int main()
{
    String_buf<8> str1 = "foo";
    String_buf<6> str2 = "bar";
    str1 = "foo2";
    str2 = "bar2";
    str1 = str2;
    //str2 = str1; // doesn't compile, static size of 'str1'<8> is bigger than 'str2'<6>!
    str2 = str1.cstr(); // this would assert if the actual size of 'str1'(4) is bigger than 'str2'<6>
    printf("%s %s\n", str1.cstr(), str2.cstr());

    String_buf<20> msg;
    msg.format("%s %s 0123456789", "Hello", "World!"); // truncated to 'Hello World! 012345'
    printf("'%s'\n", msg.cstr());

    return 0;
}

以上是关于c_cpp 字符串缓冲区的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 确定snprintf的缓冲区长度

c_cpp 将int复制到字节缓冲区

c_cpp c中简单的扩展char缓冲区

c_cpp 在C中使用静态缓冲区进行数字加载

c_cpp C中带有读指针的动态缓冲区示例

c_cpp 使用VAO(顶点数组对象)和顶点缓冲区定义和绘制矩形。