You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

70 line
1.4 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #include <lol/engine-internal.h>
  13. #include <cstdio>
  14. #if defined(_WIN32)
  15. # define WIN32_LEAN_AND_MEAN
  16. # include <windows.h>
  17. #endif
  18. #include <cstdarg>
  19. namespace lol
  20. {
  21. String String::format(char const *format, ...)
  22. {
  23. String ret;
  24. va_list ap;
  25. va_start(ap, format);
  26. ret = String::vformat(format, ap);
  27. va_end(ap);
  28. return ret;
  29. }
  30. String String::vformat(char const *format, va_list ap)
  31. {
  32. String ret;
  33. va_list ap2;
  34. #if defined va_copy || !defined _MSC_VER
  35. /* Visual Studio 2010 does not support va_copy. */
  36. va_copy(ap2, ap);
  37. #else
  38. ap2 = ap;
  39. #endif
  40. /* vsnprintf() tells us how many character we need, and we need to
  41. * add one for the terminating null byte. */
  42. size_t needed = vsnprintf(nullptr, 0, format, ap2) + 1;
  43. #if defined va_copy || !defined _MSC_VER
  44. /* do not call va_end() if va_copy() wasn't called. */
  45. va_end(ap2);
  46. #endif
  47. ((super &)ret).reserve(needed);
  48. ret.m_count = needed;
  49. vsnprintf(&ret[0], needed, format, ap);
  50. return ret;
  51. }
  52. } /* namespace lol */