Advertisement
Tkap1

Untitled

Sep 24th, 2022
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. template <int N>
  2. struct s_str_sbuilder
  3. {
  4.     static_assert(N > 0);
  5.     int len = 0;
  6.     int max_chars = N;
  7.     char data[N + 1];
  8.  
  9.     char* cstr()
  10.     {
  11.         assert(len > 0);
  12.         return data;
  13.     }
  14.  
  15.     char* get_end_ptr()
  16.     {
  17.         return &data[len];
  18.     }
  19.  
  20.     void append(char* format, ...)
  21.     {
  22.         char* where_to_write = &data[len];
  23.         va_list args;
  24.         va_start(args, format);
  25.         int written = vsnprintf(where_to_write, max_chars + 1 - len, format, args);
  26.         assert(written > 0 && written < max_chars);
  27.         va_end(args);
  28.         len += written;
  29.         data[len] = 0;
  30.     }
  31.  
  32.     void remove_last_char()
  33.     {
  34.         assert(len > 0);
  35.         len -= 1;
  36.         data[len] = 0;
  37.     }
  38. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement