Std:string formatter


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  1. std::string format_arg_list(const char *fmt, va_list args)
  2. {
  3. if (!fmt) return "";
  4. int result = -1, length = 256;
  5. char *buffer = 0;
  6. while (result == -1)
  7. {
  8. if (buffer) delete [] buffer;
  9. buffer = new char [length + 1];
  10. memset(buffer, 0, length + 1);
  11. result = _vsnprintf(buffer, length, fmt, args);
  12. length *= 2;
  13. }
  14. std::string s(buffer);
  15. delete [] buffer;
  16. return s;
  17. }
  18.  
  19. std::string format(const char *fmt, ...)
  20. {
  21. va_list args;
  22. va_start(args, fmt);
  23. std::string s = format_arg_list(fmt, args);
  24. va_end(args);
  25. return s;
  26. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.