Browse Source

core: implement String::Printf() for va_list arguments.

legacy
Sam Hocevar sam 12 years ago
parent
commit
c65b715e3a
2 changed files with 26 additions and 7 deletions
  1. +24
    -7
      src/base/string.cpp
  2. +2
    -0
      src/lol/base/string.h

+ 24
- 7
src/base/string.cpp View File

@@ -27,27 +27,44 @@ namespace lol
{ {


String String::Printf(char const *format, ...) String String::Printf(char const *format, ...)
{
String ret;

va_list ap;
va_start(ap, format);
ret = String::Printf(format, ap);
va_end(ap);

return ret;
}

String String::Printf(char const *format, va_list ap)
{ {
#if defined __CELLOS_LV2__ #if defined __CELLOS_LV2__
using std::vsnprintf; using std::vsnprintf;
#endif #endif


String ret; String ret;
va_list ap;

/* Visual Studio 2010 does not support it va_copy. */
#if defined _MSC_VER
# undef va_copy
# define va_copy(dst, src) (dst = src)
#endif
va_list ap2;
va_copy(ap2, ap);
#if defined _MSC_VER
# undef va_copy
#endif


/* vsnprintf() tells us how many character we need, and we need to /* vsnprintf() tells us how many character we need, and we need to
* add one for the terminating null byte. */ * add one for the terminating null byte. */
va_start(ap, format);
size_t needed = vsnprintf(NULL, 0, format, ap) + 1;
va_end(ap);
size_t needed = vsnprintf(NULL, 0, format, ap2) + 1;


((Super &)ret).Reserve(needed); ((Super &)ret).Reserve(needed);
ret.m_count = needed; ret.m_count = needed;


/* We don’t use va_copy because Visual Studio 2010 does not support it. */
va_start(ap, format);
vsnprintf(&ret[0], needed, format, ap); vsnprintf(&ret[0], needed, format, ap);
va_end(ap);


return ret; return ret;
} }


+ 2
- 0
src/lol/base/string.h View File

@@ -20,6 +20,7 @@
#include <lol/base/array.h> #include <lol/base/array.h>


#include <cstring> #include <cstring>
#include <cstdarg>


namespace lol namespace lol
{ {
@@ -140,6 +141,7 @@ public:
#endif #endif
static String Printf(char const *format, ...) LOL_FMT_ATTR(1, 2); static String Printf(char const *format, ...) LOL_FMT_ATTR(1, 2);
#undef LOL_FMT_ATTR #undef LOL_FMT_ATTR
static String Printf(char const *format, va_list ap);
}; };


} /* namespace lol */ } /* namespace lol */


Loading…
Cancel
Save